使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
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>
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[null,null,[],[[["\u003cp\u003eAdSense ad tags are used to enable ad serving and correctly attribute traffic to sub-accounts within AdSense for Platforms (AFP).\u003c/p\u003e\n"],["\u003cp\u003eAd tags require \u003ccode\u003edata-ad-client\u003c/code\u003e and \u003ccode\u003edata-ad-host\u003c/code\u003e parameters to identify the sub-account and platform, respectively, ensuring proper ad delivery and traffic attribution.\u003c/p\u003e\n"],["\u003cp\u003eVarious ad formats, including display, AMP, and Auto ads, have specific code implementations utilizing these parameters for seamless integration into webpages.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003edata-ad-slot\u003c/code\u003e parameter is essential for ad units other than Display, associating them with specific settings and reporting data within AdSense.\u003c/p\u003e\n"],["\u003cp\u003eAdSense for Platforms supports the Ad Placement API for interstitial and rewarded ads on web traffic, requiring the inclusion of the \u003ccode\u003edata-ad-host\u003c/code\u003e parameter.\u003c/p\u003e\n"]]],["Ad tags use parameters to control ad serving and attribute traffic to sub-accounts. `data-ad-client` (sub-account ID) and `data-ad-host` (platform ID) are required, with prefixes for AdSense. `data-ad-slot` (ad unit ID) is required for some formats and `data-ad-host-channel` for reporting. The document provides code examples for display ads, AMP ads, Auto ads, and the ad placement API, showing how to include these parameters for different ad types. Ensuring `data-ad-client` is correct is crucial for traffic attribution.\n"],null,["Ad tags\n-------\n\nThe AdSense ad tag enables ad serving, and attributes traffic correctly to your sub-accounts.\n\n### Ad tag parameters\n\nThere are two required parameters in the ad tag that you need to modify. These parameters determine ad serving settings, and the sub-account used for the ad request. Set these parameters to specify your platform publisher ID and the sub-account publisher ID. There are other additional parameters that can be modified depending on the ad format used.\n\n| Parameter | Is required? | Description and example |\n|------------------------|--------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `data-ad-client` | Yes | The sub-account property ID (string). This value is provided via the APIs in the `displayName` field, or the last segment of the `name` field. This value is required to attribute traffic correctly to the sub-accounts. \u003cbr /\u003e For AdSense for content ads, prepend this string with \"ca-\". \u003cbr /\u003e Note that it also appears in the source URL of the script tag under the parameter name `client`. \u003cbr /\u003e Example: `data-ad-client=\"`\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e`\"` `src=\".../adsbygoogle.js?client=`\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e`&host=ca-host-pub-5678\"` |\n| `data-ad-host` | Yes | This is your platform's host property ID (string). This value identifies your platform's AdSense account and is required to initiate ad serving. \u003cbr /\u003e For AdSense for content ads, prepend this string with \"ca-host-\". \u003cbr /\u003e Note that it also appears in the source URL of the script tag under the parameter name `host`. \u003cbr /\u003e Example: `data-ad-host=\"`\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e`\"` `src=\".../adsbygoogle.js?client=ca-pub-1234&host=`\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e`\"` |\n| `data-ad-slot` | No for Display ad units \u003cbr /\u003e Yes for other ad unit formats | This is the ID associated with an ad unit. For Display ad units, this ID is optional, but for other ad unit formats this is a required field. When the ad unit ID is present, settings associated with the ad unit are used during ad serving. During serving, we look for this ad unit ID in both the platform (`data-ad-host`) and (`data-ad-client`) sub-accounts. This parameter is also used to populate the Ad units report in AdSense (the report will only be populated in the account that the ad unit exists in). |\n| `data-ad-host-channel` | No | This is used for reporting purposes. See the [reporting section](/adsense/platforms/transparent/reporting) for more information. |\n\n**Important:** If the `data-ad-client` parameter is not present in your ad code, AdSense will continue ad serving using the value provided by the `data-ad-host` parameter. However, the traffic won't be attributed to the sub-account. Make sure that you place the correct `data-ad-client` publisher ID in the code tag to avoid attributing traffic to the wrong account.\n\n### Ad code examples\n\nBelow you'll find a code example for each ad type that AFP supports. Learn more about [ad units](https://support.google.com/adsense/answer/9183549) and [Auto ads](https://support.google.com/adsense/answer/9261805) in AdSense.\n\n#### Display ad unit code (responsive)\n\n \u003cscript async\n src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e&host=\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e\"\n crossorigin=\"anonymous\"\u003e\n \u003c/script\u003e\n \u003cins class=\"adsbygoogle\"\n style=\"display:block;\"\n data-ad-client=\"\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e\"\n data-ad-host=\"\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e\"\n data-ad-format=\"auto\"\u003e\n \u003c/ins\u003e\n \u003cscript\u003e\n (adsbygoogle = window.adsbygoogle || []).push({});\n \u003c/script\u003e\n\n#### Display ad unit code (fixed size)\n\n \u003cscript async\n src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e&host=\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e\"\n crossorigin=\"anonymous\"\u003e\n \u003c/script\u003e\n \u003cins class=\"adsbygoogle\"\n style=\"display:inline-block;width:728px;height:90px\"\n data-ad-client=\"\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e\"\n data-ad-host=\"\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e\"\u003e\n \u003c/ins\u003e\n \u003cscript\u003e\n (adsbygoogle = window.adsbygoogle || []).push({});\n \u003c/script\u003e\n\n#### Display AMP ad unit (responsive)\n\nThe following code needs to be placed in the `\u003chead\u003e` of the page: \n\n \u003cscript async custom-element=\"amp-ad\" src=\"https://cdn.ampproject.org/v0/amp-ad-0.1.js\"\u003e\u003c/script\u003e\n\nAnd this is the snippet for the ad unit, placed in the `\u003cbody\u003e`: \n\n \u003camp-ad\n width=\"100vw\"\n height=\"320\"\n type=\"adsense\"\n data-ad-client=\"\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e\"\n data-ad-host=\"\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e\"\n data-ad-slot=\"\"\n data-auto-format=\"rspv\"\n data-full-width=\"\"\u003e\n \u003cdiv overflow=\"\"\u003e\n \u003c/amp-ad\u003e\n\n#### Display AMP ad unit (fixed size)\n\nThe following code needs to be placed in the `\u003chead\u003e` of the page: \n\n \u003cscript async custom-element=\"amp-ad\" src=\"https://cdn.ampproject.org/v0/amp-ad-0.1.js\"\u003e\u003c/script\u003e\n\nAnd this is the snippet for the ad unit, placed in the `\u003cbody\u003e`: \n\n \u003camp-ad\n layout=\"fixed\"\n width=\"300\"\n height=\"600\"\n type=\"adsense\"\n data-ad-client=\"\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e\"\n data-ad-host=\"\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e\"\u003e\n \u003c/amp-ad\u003e\n\n#### Auto ads\n\nIdeally, the following ad tag is placed in the `\u003chead\u003e` element, or as high up the page as possible. \n\n \u003cscript async\n src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e&host=\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e\"\n crossorigin=\"anonymous\"\u003e\n \u003c/script\u003e\n\n### Ads via the ad placement API\n\nThe ad placement API currently supports interstitial and rewarded ads. Read the [Ad Placement API developer documentation](/ad-placement) to learn about how to implement the API and customise it.\n\nAdSense for Platforms is supported for these formats (currently only for web traffic). The only requirement is that the `data-ad-host` parameter is included in the main ad tag. See the example below. \n\n \u003cscript async\n src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=\u003cvar translate=\"no\"\u003eca-pub-1234\u003c/var\u003e&host=\u003cvar translate=\"no\"\u003eca-host-pub-5678\u003c/var\u003e\"\n crossorigin=\"anonymous\"\u003e\n \u003c/script\u003e\n \u003cscript\u003e\n window.adsbygoogle = window.adsbygoogle || [];\n var adBreak = adConfig = function(o) {adsbygoogle.push(o);}\n \u003c/script\u003e"]]