網站導入
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
注意:無論存在多少封鎖條件,每個網頁都只能發出一個廣告請求。
重要事項:使用這個代碼前,請先詳閱條款及細則
如要使用這個程式碼,您必須擁有具備有效權限的 AdSense 帳戶,才能使用 AdSense 自訂搜尋廣告。
總覽
在網站上導入 AdSense 自訂搜尋廣告共有 3 個步驟:
步驟 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>
內容網頁上的相關搜尋
內容的相關搜尋可為使用者提供與網頁內容相關的搜尋字詞。
藉此探索相關主題,與搜尋廣告或網站瀏覽功能互動。
請勿使用「查詢」參數,因為這會導致
也就是預測結果
注意:Google 必須先檢索
確保字詞與內容相關。如果網頁尚未檢索,或
禁止檢索 (例如遭到 robots.txt 封鎖)
AdSense 檢索器
我們不會顯示您提供給 Google 的建議相關搜尋字詞。
「內容的相關搜尋」產品依賴 Google 的內容檢索系統為網頁建立索引
並產生相關搜尋字詞包含查詢參數 (例如追蹤) 的內容網頁網址
變數、使用者 ID、工作階段 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 資源
- 請勿使用「查詢」參數,否則會導致非預期的結果。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間: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"]],["上次更新時間: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."]]