// 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>
// 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 ที่ผ่านการ Minify คุณจะพบปัญหาการหยุดทำงานเกือบแน่นอน โปรดจำกัดการใช้งานไว้เฉพาะ API ที่ระบุไว้ในคู่มืออ้างอิง API เนื่องจากเราเปลี่ยนแปลงการทํางานภายในของ GPT อยู่อย่างต่อเนื่องเพื่อปรับปรุงอย่างต่อเนื่อง
เช่น ข้อกําหนดทั่วไปคือการตรวจหาเมื่อ PubAdsService โหลดเสร็จแล้วเพื่อเรียกใช้ 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();}
คำอธิบาย / คำอธิบายการแก้ไข
ซึ่งเชื่อถือได้เพียง API สาธารณะเท่านั้น ในกรณีที่ตรวจพบว่า PubAdsService
ที่โหลดอย่างสมบูรณ์เรามีค่าบูลีน
googletag.pubadsReady
สถานการณ์ 8: การย้ายคอนเทนเนอร์ช่องภายใน DOM หลังจากเรียกใช้การแสดงผล
คำอธิบายกรณีการใช้งานระดับสูง
การย้ายหรือแทรกคอนเทนเนอร์ช่องใน 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 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,[]]