廣告代碼

AdSense 廣告代碼可啟用廣告放送和收益分潤功能。您必須在每一個網頁上加入 AdSense 廣告代碼,以便使用者選擇採用 AdSense 透過內容營利。

這個步驟的導入方式可能取決於廣告設定控制項,以及您希望為使用者提供的廣告體驗: - 如果廣告設定的控制權在您,您可以決定要支援的廣告類型和刊登位置,然後將廣告代碼加進使用者網站,就像他們在自家網站一樣。您會在 AdSense 帳戶中建立廣告單元,但修改 data-ad-client 和 data-ad-host 參數 - 如果您要將廣告設定的控制權委派給使用者,可以向使用者詢問他們偏好的廣告類型和刊登位置 (左側、右側、頂端、底部等),然後使用這些偏好設定進行標記。請注意,為了支援原生廣告,廣告代碼需要一個要向使用者收集的廣告單元 ID。

廣告代碼參數

您必須修改廣告代碼中的兩個參數。這些參數會決定廣告放送和收益分潤,請設定這些參數,以指定平台的發布商 ID 和使用者的發布商 ID。視使用的廣告格式而定,您還可以修改其他參數。

參數 是必填欄位嗎? 說明和範例
data-ad-client 您使用者的 AdSense 內容廣告發布商 ID (字串)。這個值會透過註冊流程提供。是啟用收益分潤功能的必要值。

如果是 AdSense 內容廣告,請在此字串前面加上「ca-」。

請注意,這個名稱也會顯示在參數名稱 client 下的指令碼標記來源網址中。

範例:
data-ad-client="ca-pub-1234"
src=".../adsbygoogle.js?client=ca-pub-1234&host=ca-host-pub-5678"
data-ad-host 這是您平台的主機資源 ID (字串)。這個值可識別平台的 AdSense 帳戶,也是啟用廣告放送功能的必要值。

如要刊登 AdSense 內容廣告,請在此字串前面加上「ca-host-」。

請注意,這個名稱也會顯示在參數名稱 host 下方的指令碼標記來源網址中。

範例:
data-ad-host="ca-host-pub-5678"
src=".../adsbygoogle.js?client=ca-pub-1234&host=ca-host-pub-5678"
data-ad-slot 如為多媒體廣告單元,則為「否」

如為其他廣告單元格式,則為「是」
這是與廣告單元相關聯的 ID。如果是多媒體廣告單元,這個 ID 為選填欄位,但如果是其他廣告單元格式,則為必填欄位。如果有廣告單元 ID,廣告放送期間就會使用與廣告單元相關聯的設定。廣告放送期間,我們會在平台 (data-ad-host) 和子 (data-ad-client) 帳戶中尋找這個廣告單元 ID。這個參數也會在 AdSense 中為廣告單元報表填入資料 (該報表只會填入廣告單元所屬的帳戶)。
data-ad-host-channel 這會用於製作報表。詳情請參閱報表部分

重要事項:如果廣告程式碼中沒有 data-ad-host 參數,AdSense 會使用 data-ad-client 參數提供的值繼續放送廣告。但平台帳戶不會在帳戶中獲得任何曝光或收益分潤。或者,您也可以使用 google-adsense-platform 中繼標記,確保廣告請求中包含您的代管發布商 ID。

此外,請確認程式碼代碼中的 data-ad-client 發布商 ID 正確無誤,以免將收益歸因給錯誤的使用者。

廣告程式碼範例

以下是 AFP 支援的各種廣告類型的程式碼範例。進一步瞭解 AdSense 的廣告單元自動廣告

多媒體廣告單元程式碼 (回應式)

<script async
     src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234&host=ca-host-pub-5678"
     crossorigin="anonymous">
</script>
<ins class="adsbygoogle"
     style="display:block;"
     data-ad-client="ca-pub-1234"
     data-ad-host="ca-host-pub-5678"
     data-ad-format="auto">
</ins>
<script>
   (adsbygoogle = window.adsbygoogle || []).push({});
</script>

多媒體廣告單元程式碼 (固定大小)

<script async
     src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234&host=ca-host-pub-5678"
     crossorigin="anonymous">
</script>
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-1234"
     data-ad-host="ca-host-pub-5678">
</ins>
<script>
   (adsbygoogle = window.adsbygoogle || []).push({});
</script>

多媒體 AMP 廣告單元 (回應式)

您必須在頁面的 <head> 中放置以下程式碼:

<script async custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script>

以下是廣告單元的程式碼片段,放置在 <body> 中:

<amp-ad
  width="100vw"
  height="320"
  type="adsense"
  data-ad-client="ca-pub-1234"
  data-ad-host="ca-host-pub-5678"
  data-ad-slot=""
  data-auto-format="rspv"
  data-full-width="">
  <div overflow="">
</amp-ad>

多媒體 AMP 廣告單元 (固定大小)

您必須在頁面的 <head> 中放置以下程式碼:

<script async custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script>

以下是廣告單元的程式碼片段,放置在 <body> 中:

<amp-ad
  layout="fixed"
  width="300"
  height="600"
  type="adsense"
  data-ad-client="ca-pub-1234"
  data-ad-host="ca-host-pub-5678">
</amp-ad>

自動廣告

在理想情況下,請將下列廣告代碼放在 <head> 元素中,或盡可能提高網頁的上方位置。

<script async
    src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234&host=ca-host-pub-5678"
    crossorigin="anonymous">
</script>

透過 Ad Placement API 放送廣告

Ad Placement API 目前支援插頁式廣告和獎勵廣告。請參閱 Ad Placement API 開發人員說明文件,瞭解如何導入及自訂 API。

AdSense for Platforms 支援這些格式 (目前僅適用於網路流量)。唯一的條件是必須在主要廣告代碼中加入 data-ad-host 參數。請參閱下方的範例。

<script async
    src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234&host=ca-host-pub-5678"
    crossorigin="anonymous">
</script>
<script>
   window.adsbygoogle = window.adsbygoogle || [];
   var adBreak = adConfig = function(o) {adsbygoogle.push(o);}
</script>