廣告代碼

AdSense 廣告代碼可啟用廣告放送,並將流量正確歸給子帳戶。

廣告代碼參數

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

參數 是否必要? 說明和範例
data-ad-client 子帳戶資源 ID (字串)。這個值是透過 displayName 欄位中的 API 或 name 欄位的最後一個片段提供。這個值是正確歸因流量至子帳戶的必要條件。

如要放送 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-client 參數,AdSense 仍會使用 data-ad-host 參數提供的值繼續放送廣告,不過,流量不會歸給子帳戶。為避免將流量歸給錯誤的帳戶,請確認程式碼標記中的 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>