广告代码

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 目前支持插页式广告和激励广告。如需了解如何实现该 API 并对其进行自定义,请参阅 Ad Placement 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>