广告代码

AdSense 广告代码用于启用广告投放和收益分成。您需要在用户可以选择通过 AdSense 通过其内容创收的每个网页上放置 AdSense 广告代码。

执行此步骤的方式可能取决于广告设置控制和您想要向用户提供的广告体验: - 如果广告设置由您控制,您可以决定要支持的广告类型和展示位置,还可以向用户的网站添加广告代码,就像它们是您自己的网站一样。您可以在 AdSense 帐号中创建广告单元,但要修改 data-ad-client 和 data-ad-host 参数 - 如果您将广告设置控制权委派给用户,则可能需要询问用户他们对广告类型和展示位置(左侧、右侧、顶部、底部等)的偏好,然后根据这些偏好设置进行代码植入。请注意,为了支持原生广告,广告代码需要一个广告单元 ID,您需要向用户收集此 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 为必填字段。如果存在广告单元 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>