廣告代碼

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>