// Incorrect: Accessing these files from an unofficial source
<scriptasyncsrc="https://www.example.com/tag/js/gpt.js"></script>
修正錯誤的建議方式
// Correct: Access these files from a Google domain
<scriptsrc="https://securepubads.g.doubleclick.net/tag/js/gpt.js"crossorigin="anonymous"async></script>
// Also correct, if using Limited Ads
<scriptsrc="https://pagead2.googlesyndication.com/tag/js/gpt.js"async></script>
情境 2:依賴 gpt.js 指令碼標記事件監聽器
應用實例概要說明
假設 GPT API 在 JavaScript 檔案 gpt.js 載入時已準備好供呼叫,這項假設是錯誤的,因為 API 的部分內容是由 pubads_impl.js 檔案提供。以任何方式 (包括架構) 仰賴 API
,因此不正確。
// Make sure that googletag.cmd exists.window.googletag=window.googletag||{};googletag.cmd=googletag.cmd||[];// Correct: Queueing the callback on the command queue.googletag.cmd.push(callback);
如果您遵循最小化 GPT 程式庫程式碼的語法
隨時可能發生故障我們持續不斷變更,因此請限制您僅能使用 API 參考指南中所述的 API 用途
GPT 內部運作方式的持續改進
例如,常見的要求是偵測
呼叫 refresh()。
含有錯誤的程式碼片段範例
// Incorrect: Relying on an obfuscated property.if(googletag.pubads().a!=null){functionProcessingGPT();}
錯誤修正建議
// Correct: Relying on public GPT API methods// (i.e. googletag.pubadsReady in this case).if(window.googletag&&googletag.pubadsReady){functionProcessingGPT();}
在呼叫顯示後,在 DOM 移動或插入運算單元容器,可能導致
GPT 中的非預期自動重排和無法預測的行為
含有錯誤的程式碼片段示例
// Incorrect: Moving slot containers after calling displaygoogletag.defineSlot("/1234/travel/asia",[728,90],"div-gpt-ad-123456789-0");googletag.enableServices();googletag.display("div-gpt-ad-123456789-0");...// Inserting another element before the slot container, pushing the slot container down the page.document.body.insertBefore(someOtherElement,document.getElementById("div-gpt-ad-123456789-0"));
錯誤修正建議
// Correct: Make any DOM order changes before calling displaydocument.body.insertBefore(someOtherElement,document.getElementById("div-gpt-ad-123456789-0"));...googletag.defineSlot("/1234/travel/asia",[728,90],"div-gpt-ad-123456789-0");googletag.enableServices();googletag.display("div-gpt-ad-123456789-0");
[[["容易理解","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\u003eAvoid unofficial copies of GPT JavaScript libraries, always access them from an official Google domain.\u003c/p\u003e\n"],["\u003cp\u003eUtilize \u003ccode\u003egoogletag.cmd.push\u003c/code\u003e to queue functions and ensure they execute when GPT is ready, rather than relying on script tag listeners or checking the \u003ccode\u003egoogletag\u003c/code\u003e object directly.\u003c/p\u003e\n"],["\u003cp\u003eStrictly adhere to the documented GPT API and refrain from relying on obfuscated code or overwriting any GPT functions or variables to prevent breakages.\u003c/p\u003e\n"],["\u003cp\u003eMaintain the correct order of GPT calls, like defining page-level settings and slots before enabling services and displaying ads, to avoid race conditions.\u003c/p\u003e\n"],["\u003cp\u003eBe mindful of JavaScript variable scoping and closures, especially when using \u003ccode\u003egoogletag.cmd.push\u003c/code\u003e, to prevent unexpected behavior due to delayed execution.\u003c/p\u003e\n"],["\u003cp\u003eEnsure slot containers are positioned correctly in the DOM before calling \u003ccode\u003edisplay\u003c/code\u003e to avoid reflows and unpredictable rendering.\u003c/p\u003e\n"],["\u003cp\u003eRefrain from overwriting browser APIs, as it can negatively impact the functionality of third-party scripts like GPT.\u003c/p\u003e\n"]]],["The content outlines unsupported methods of implementing GPT (Google Publisher Tag) that may cause unpredictable ad serving issues. Key actions to avoid include: using unofficial GPT JavaScript libraries, relying on script tag listeners or the `googletag` object to determine API readiness, using obfuscated code syntax, overwriting GPT functions/variables, mis-ordering GPT calls, and misusing JavaScript variable scoping. Correct implementations involve using Google-hosted libraries, leveraging `googletag.cmd.push`, respecting API timing, and modifying the DOM before calling display. Also, avoid overwriting browser APIs.\n"],null,[]]