广告代码

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 平台广告支持这些格式(目前仅适用于网络流量)。唯一的要求是主广告代码中必须包含 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>