网站实现
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
注意:无论有多少个屏蔽设置,每个网页都只能发出一个广告请求。
重要提示:请先阅读条款及条件,然后再使用此代码
要使用此代码,您必须拥有一个 AdSense 账号,且该账号拥有使用 AdSense 自定义搜索广告的有效权限。
概览
在您的网站上投放 AdSense 自定义搜索广告需要完成三个步骤:
第 1 步:在 AdSense 中创建自定义搜索广告样式
登录后,展开搜索广告,然后点击搜索广告样式。在这里,您可以
创建新样式或更新现有样式
第 2 步:配置代码
点击获取代码
。
配置您的广告容器,使其与搜索页上的广告容器相匹配。
第 3 步:植入代码
复制代码生成器中的代码并将其粘贴到您的网页中,同时别忘了更新 query
参数。在 head 标记中生成的代码应放置在网站的 head 标记中,广告才能正常加载。广告单元 1 的代码将包含页面上所有广告单元的设置。对于其他广告单元,只需在应该展示广告的网页上放置相应的广告单元 <div>
。
代码生成器中提供了最常用的功能,而
“参考”部分中还提供了其他一些参数。参考部分包含所有
网页级选项(适用于网页上的所有广告单元)以及所有
广告单元级选项(适用于单个广告单元)。
示例代码
异步广告加载
生成的代码是异步的。异步广告加载的完整运行示例如下所示。您可以在新标签页中打开此示例。请务必输入您自己的客户端 ID。
<html>
<head>
<script async="async" src="https://www.google.com/adsense/search/ads.js"></script>
<script type="text/javascript" charset="utf-8">
(function(g,o){g[o]=g[o]||function(){(g[o]['q']=g[o]['q']||[]).push(
arguments)},g[o]['t']=1*new Date})(window,'_googCsa');
</script>
</head>
<body>
<div id="afscontainer1"></div>
<script type="text/javascript" charset="utf-8">
var pageOptions = {
'pubId' : 'test client ID', // Enter your own client-ID here
'query' : 'flowers', // User query for this page
'styleId': '7824176615' // Enter your own style ID here
};
var adblock1 = {
'container' : 'afscontainer1',
'width' : 700
};
var adblock2 = {
'container' : 'afscontainer2',
'width' : 700
};
_googCsa('ads', pageOptions, adblock1, adblock2);
</script>
<p>Search result 1</p>
<p>Search result 2</p>
<p>Search result 3</p>
<div id="afscontainer2"></div>
</body>
</html>
搜索页上的相关搜索
Google 搜索上的相关搜索是一个自然单元,其中包含与当前搜索字词相关的搜索字词。此广告单元上的链接会指向新的搜索结果页。
<html>
<head>
<script async="async" src="https://www.google.com/adsense/search/ads.js"></script>
<script type="text/javascript" charset="utf-8">
(function(g,o){g[o]=g[o]||function(){(g[o]['q']=g[o]['q']||[]).push(
arguments)},g[o]['t']=1*new Date})(window,'_googCsa');
</script>
</head>
<body>
<div id="afscontainer1"></div>
<script type="text/javascript" charset="utf-8">
var pageOptions = {
'pubId': 'test client ID', // Enter your own client-ID here
'relatedSearchTargeting': 'query', // Must use 'query' for Related Search on Search pages
'query': 'flowers', // User query for this page
'styleId': '1234567890', // Enter your own style ID here
'resultsPageBaseUrl': '//www.example.com/search', // Enter your own base URL here
'resultsPageQueryParam': 'query' // Enter your own query parameter here
};
var adblock = {
'container': 'afscontainer1',
'number': 3,
'width': 700
};
var rsblock = {
'container': 'afscontainer2',
'relatedSearches': 6,
'width': 500
};
_googCsa('ads', pageOptions, adblock, rsblock);
</script>
<p>Search result 1</p>
<p>Search result 2</p>
<p>Search result 3</p>
<div id="afscontainer2"></div>
</body>
</html>
内容页上的相关搜索
“内容相关搜索”可为用户提供与网页内容相关的搜索字词。
这样,用户就可以与搜索广告或网站导航互动,从而探索相关主题。
请勿使用“query”参数参数的值,因为它会导致
结果。
注意:Google 需要先抓取相关搜索字词,然后相关搜索字词才能显示在您的网站上
确保搜索字词与内容相关。当网页尚未被抓取时,或
被 robots.txt 禁止抓取
AdSense 抓取工具、
我们不会显示您向 Google 提供的建议相关搜索字词。
“内容相关搜索”产品依靠 Google 的内容抓取系统将网页编入索引
并生成相关搜索字词。包含查询参数(例如跟踪网址)的内容页网址
变量、用户 ID、会话 ID 或其他动态标识符可能会干扰抓取工具的
评估网页的核心内容的能力,可能会导致相关搜索不准确或不相关
条款。请使用“ignoredPageParams”参数来标识这些不重要的参数,
确保将内容编入索引并减少不必要的抓取负载。这样,Google 就能更好地
将网页识别为已被抓取并开始显示建议的字词(Google 和
(如果符合条件)。
<html>
<head>
<script async="async" src="https://www.google.com/adsense/search/ads.js"></script>
<script type="text/javascript" charset="utf-8">
(function(g,o){g[o]=g[o]||function(){(g[o]['q']=g[o]['q']||[]).push(
arguments)},g[o]['t']=1*new Date})(window,'_googCsa');
</script>
</head>
<body>
<p>Page content</p>
<div id="afscontainer1"></div>
<script type="text/javascript" charset="utf-8">
var pageOptions = {
'pubId': 'test client ID', // Enter your own client-ID here
'relatedSearchTargeting': 'content', // Must use 'content' for Related Search on Content pages
'hl': 'en', // The preferred language for related terms (default to 'en' if not specified)
'styleId': '1234567890', // Enter your style ID
'resultsPageBaseUrl': '//www.example.com/search', // Enter the base URL of your results page
'resultsPageQueryParam': 'query', // Specify the query parameter on your results page
'terms': 'term a,term b', // Optionally provide your own related terms in a comma-delimited list
'referrerAdCreative': 'example ad title' // Only used when a user clicked an ad to arrive here;
// required when 'terms' is present.
};
var rsblock1 = {
'container': 'afscontainer1',
'relatedSearches': 6
};
_googCsa('relatedsearch', pageOptions, rsblock1);
</script>
<p>More page content</p>
</body>
</html>
需要避免的事项
我们建议所有发布商都遵守 AdSense 自定义搜索广告实施指南,
并避免使用可能会干扰广告呈现并导致
无法达到预期的广告效果。
- 除非要实现无限滚动,否则请勿在一个网页上进行多次广告调用
- 无论广告呈现之前还是之后,都不要隐藏广告展示区域
- 请勿在网页上四处移动广告展示区域
- 请求的广告代码片段数量不要超过您希望在网页上展示的广告数量
- 请勿操纵广告展示区域的 DOM
- 不要实现自己的延迟加载(改为依靠 Google 的延迟加载)
- 不要在您的网站上缓存 Google 的 JS 资源
- 请勿使用“query”参数参数,因为它会导致意外的结果。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eImplementing AdSense Custom Search Ads involves creating a custom search style in AdSense, configuring the code, and implementing the code on your webpage.\u003c/p\u003e\n"],["\u003cp\u003eEnsure you only make one ad request per page, and avoid practices like hiding or moving ad blocks, or manipulating their DOM.\u003c/p\u003e\n"],["\u003cp\u003eRelated Search on Search pages uses the \u003ccode\u003equery\u003c/code\u003e parameter while Related Search on Content pages uses the \u003ccode\u003econtent\u003c/code\u003e parameter and requires Google to crawl your pages for contextual relevance.\u003c/p\u003e\n"],["\u003cp\u003eFamiliarize yourself with the terms and conditions and obtain the necessary permissions before using AdSense Custom Search Ads.\u003c/p\u003e\n"],["\u003cp\u003eYou can customize ad containers, specify the number of ads, and provide your own related search terms for content pages to enhance user engagement.\u003c/p\u003e\n"]]],[],null,["# Web Implementation\n\n**Note:** Only ever make one ad request per page no matter how many blocks may be present.\n\n**Important:** Read the [Terms and Conditions](https://support.google.com/adsense/answer/7003954) before using this code\n\nTo use this code you must have an [AdSense account](https://www.google.com/adsense) with [active permission](/custom-search-ads/request-permission) to use AdSense Custom Search Ads.\n\nOverview\n--------\n\nImplementing AdSense Custom Search Ads on your site is a 3 step process:\n\n**Step 1: Create your custom search style in [AdSense](https://www.google.com/adsense/start/)**\n\n\nAfter signing in, expand **Ads for search** then click **Search styles**. From here you can\ncreate a new style or update an existing one.\n\n**Step 2: Configure the code**\n\n\nClick **Get Code**\n.\nConfigure your ad containers to match the ad containers on your search page.\n\n\n**Step 3: Implement the code**\n\nCopy the code from the code generator and paste it on your page, remembering to update the `query` parameter. The code generated in the head tag should be placed in the head tag on your site for the ads to load properly. The code for ad unit 1 will contain the settings for all ad units on the page. Additional ad units will only require placing the corresponding ad unit `\u003cdiv\u003e` on the page where ads should appear.\nThe most commonly used features are supplied in the code generator, but some additional parameters are available in the [reference section](/custom-search-ads/web/reference). The reference section contains all [page level options](/custom-search-ads/web/reference#pageparams), which apply to all ad units on the page, as well as all [unit level options](/custom-search-ads/web/reference#unitparams), which apply to individual ad units.\n\n\u003cbr /\u003e\n\nSample code\n-----------\n\n### Asynchronous ad loading\n\nThe generated code is asynchronous. A full working example of asynchronous ad loading is shown below. You may [open this example in new tab](/custom-search-ads/s/docs/sample). Remember to put your own client-Id. \n\n```\n\u003chtml\u003e\n\u003chead\u003e\n\n\u003cscript async=\"async\" src=\"https://www.google.com/adsense/search/ads.js\"\u003e\u003c/script\u003e\n\n\u003cscript type=\"text/javascript\" charset=\"utf-8\"\u003e\n (function(g,o){g[o]=g[o]||function(){(g[o]['q']=g[o]['q']||[]).push(\n arguments)},g[o]['t']=1*new Date})(window,'_googCsa');\n\u003c/script\u003e\n\n\u003c/head\u003e\n\n\u003cbody\u003e\n\n\u003cdiv id=\"afscontainer1\"\u003e\u003c/div\u003e\n\u003cscript type=\"text/javascript\" charset=\"utf-8\"\u003e\n\nvar pageOptions = {\n 'pubId' : 'test client ID', // Enter your own client-ID here\n 'query' : 'flowers', // User query for this page\n 'styleId': '7824176615' // Enter your own style ID here\n};\n\nvar adblock1 = {\n 'container' : 'afscontainer1',\n 'width' : 700\n};\n\nvar adblock2 = {\n 'container' : 'afscontainer2',\n 'width' : 700\n};\n\n_googCsa('ads', pageOptions, adblock1, adblock2);\n\u003c/script\u003e\n\n\u003cp\u003eSearch result 1\u003c/p\u003e\n\u003cp\u003eSearch result 2\u003c/p\u003e\n\u003cp\u003eSearch result 3\u003c/p\u003e\n\n\u003cdiv id=\"afscontainer2\"\u003e\u003c/div\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Related Search on Search Pages\n\nRelated Search on Search is an organic unit with search terms related to the current search term. Links on this unit go to a new search result page. \n\n```\n\u003chtml\u003e\n\u003chead\u003e\n\n\u003cscript async=\"async\" src=\"https://www.google.com/adsense/search/ads.js\"\u003e\u003c/script\u003e\n\n\u003cscript type=\"text/javascript\" charset=\"utf-8\"\u003e\n (function(g,o){g[o]=g[o]||function(){(g[o]['q']=g[o]['q']||[]).push(\n arguments)},g[o]['t']=1*new Date})(window,'_googCsa');\n\u003c/script\u003e\n\n\u003c/head\u003e\n\n\u003cbody\u003e\n\n\u003cdiv id=\"afscontainer1\"\u003e\u003c/div\u003e\n\u003cscript type=\"text/javascript\" charset=\"utf-8\"\u003e\n\nvar pageOptions = {\n 'pubId': 'test client ID', // Enter your own client-ID here\n 'relatedSearchTargeting': 'query', // Must use 'query' for Related Search on Search pages\n 'query': 'flowers', // User query for this page\n 'styleId': '1234567890', // Enter your own style ID here\n 'resultsPageBaseUrl': '//www.example.com/search', // Enter your own base URL here\n 'resultsPageQueryParam': 'query' // Enter your own query parameter here\n};\n\nvar adblock = {\n 'container': 'afscontainer1',\n 'number': 3,\n 'width': 700\n};\n\nvar rsblock = {\n 'container': 'afscontainer2',\n 'relatedSearches': 6,\n 'width': 500\n};\n\n_googCsa('ads', pageOptions, adblock, rsblock);\n\u003c/script\u003e\n\n\u003cp\u003eSearch result 1\u003c/p\u003e\n\u003cp\u003eSearch result 2\u003c/p\u003e\n\u003cp\u003eSearch result 3\u003c/p\u003e\n\n\u003cdiv id=\"afscontainer2\"\u003e\u003c/div\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Related Search on Content Pages\n\nRelated Search on Content provides search terms for the user related to the content of the page.\nThis enables users to explore relevant topics, engaging with search ads or site navigation.\nDon't use the 'query' parameter for Related Search on Content Pages as it will cause unexpected\nresults.\n\n\n**Note:** Before related search terms can appear on your site, Google needs to crawl\nyour pages to ensure the terms are contextually relevant. When a page has not yet been crawled or\nis blocked from crawling (eg. by a robots.txt) by the\n\n[AdSense crawler](/search/docs/crawling-indexing/overview-google-crawlers#adsense),\n\nwe will not show suggested Related Search terms you provide to Google.\n\nThe Related Search on Content product relies on Google's content crawling system to index pages\nand generate relevant search terms. Content page URLs containing query parameters such as tracking\nvariables, user IDs, session IDs, or other dynamic identifiers can interfere with the crawler's\nability to assess the page's core content and may lead to inaccurate or irrelevant related search\nterms. Please use the 'ignoredPageParams' parameter to identify these nonessential parameters,\nensuring proper content indexing and reducing unnecessary crawl load. This allows Google to better\nrecognize a page as having already been crawled and start showing suggested terms (both Google and\nthe partner's, if eligible). \n\n```\n\u003chtml\u003e\n\u003chead\u003e\n\n\u003cscript async=\"async\" src=\"https://www.google.com/adsense/search/ads.js\"\u003e\u003c/script\u003e\n\n\u003cscript type=\"text/javascript\" charset=\"utf-8\"\u003e\n (function(g,o){g[o]=g[o]||function(){(g[o]['q']=g[o]['q']||[]).push(\n arguments)},g[o]['t']=1*new Date})(window,'_googCsa');\n\u003c/script\u003e\n\n\u003c/head\u003e\n\n\u003cbody\u003e\n\n\u003cp\u003ePage content\u003c/p\u003e\n\n\u003cdiv id=\"afscontainer1\"\u003e\u003c/div\u003e\n\n\u003cscript type=\"text/javascript\" charset=\"utf-8\"\u003e\n\nvar pageOptions = {\n 'pubId': 'test client ID', // Enter your own client-ID here\n 'relatedSearchTargeting': 'content', // Must use 'content' for Related Search on Content pages\n 'hl': 'en', // The preferred language for related terms (default to 'en' if not specified)\n 'styleId': '1234567890', // Enter your style ID\n 'resultsPageBaseUrl': '//www.example.com/search', // Enter the base URL of your results page\n 'resultsPageQueryParam': 'query', // Specify the query parameter on your results page\n 'terms': 'term a,term b', // Optionally provide your own related terms in a comma-delimited list\n 'referrerAdCreative': 'example ad title' // Only used when a user clicked an ad to arrive here;\n // required when 'terms' is present.\n};\n\nvar rsblock1 = {\n 'container': 'afscontainer1',\n 'relatedSearches': 6\n};\n\n_googCsa('relatedsearch', pageOptions, rsblock1);\n\u003c/script\u003e\n\n\u003cp\u003eMore page content\u003c/p\u003e\n\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nThings to Avoid\n---------------\n\nWe advise that all publishers adhere to our guidelines for implementing AdSense Custom Search Ads,\nand avoid certain implementation methods that might interfere with ads rendering and cause\nunexpected results or suboptimal performance.\n\n- Don't make multiple ad calls from a page unless for an infinite scroll implementation\n- Don't hide ad blocks either before or after ads are rendered\n- Don't move ad blocks around the page\n- Don't request more ad blocks than you want to show on the page\n- Don't manipulate the DOM of the ad blocks\n- Don't implement your own lazy-loading (rely on Google's lazy-loading instead)\n- Don't cache Google's JS resources on your site\n- Don't use the 'query' parameter for Related Search on Content Pages as it will cause unexpected results."]]