// 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 スクリプトタグ リスナーに依存している
ユースケースの概要
JavaScript ファイル gpt.js が読み込まれたときに GPT API を呼び出す準備ができていると想定するのは誤りです。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);
// 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();}
修正の説明
信頼できるのは公開 API のみです。PubAdsService が完全に読み込まれているかどうかを検出する場合は、ブール値 googletag.pubadsReady があります。
display を呼び出した後に DOM でスロット コンテナを移動または挿入すると、
望ましくないリフローや予測不能な動作が発生する場合もあります。
エラーのあるコード スニペットの例
// 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");
シナリオ 9: ブラウザ API の上書き
ユースケースの概要
GPT では、ブラウザ API の上書き(モンキーパッチ、ポリフィル)はサポートされていません。
この方法では、GPT などのサードパーティ スクリプトが予期しない方法で破損する可能性があります。
[[["わかりやすい","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 UTC。"],[[["\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,[]]