未分類

Google AdSense 非同步 async script 怎麼放?

Google AdSense 非同步 async script 到底怎麼放?

Adsense 新建廣告單元的指引不夠詳細,容易讓人但在放置多個廣告單元時多放了一部分程式碼,大家容易將 async script 跟著 push 程式碼一起放置,如果有 3個廣告單元的話 async script 也放了3份。

結論是 AdSense 非同步 async script 一個網頁中只要有一份就夠了,把它放在 head 之間就可以了。

在官網 Where to place the AdSense code in your HTML 這篇教學是這樣寫的,把非同步 async script 放置在 head 標籤之間,

1
2
3
4
5
6
7
8
9
10
<html>
<head>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234567890123456" crossorigin="anonymous"></script>
This is the head of your page.
<title>Example HTML page</title>
</head>
<body>
This is the body of your page.
</body>
</html>

非同步 async script 早期新增廣告單元時是顯示這個版本,

1
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

後來變成這個版本,但是現在新增廣告單元都是顯示這個版本,多了 client 參數,跟 crossorigin,

1
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234567890123456" crossorigin="anonymous"></script>

但是在 非同步載入廣告指令碼  |  Publisher Ads Audits for Lighthouse  |  Google Developers 這篇裡有列出自動廣告是這樣寫,

1
<script async data-ad-client="ca-pub-xxxxxxxxxxxxxxxx" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

但是 adsense 廣告頁面顯示如果已經放了自動廣告的版本就不用在改了,那 crossorigin 重不重要?真是讓人匪夷所思。

其他參考
Google Adsense 非同步廣告程式碼,省略重複的Async Script,加速網頁載入
他也是按照 adsense 官網的格式放的

分享到