未分類

遮蔽特定網頁Google AdSense廣告的方法

本篇紀錄遮蔽特定網頁Google AdSense廣告的方法,方法就是在判斷當前網頁的網址是否是特定的那一頁,如果是的話就不展開 AdSense 廣告。

這是以前的同步廣告程式碼,現在基本上都建議使用非同步廣告程式碼,有空我另外改成非同步廣告程式碼版本,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script type="text/javascript"> 
var nowurl = location.href; // 讀入網址
if (nowurl == "https://freedommap01.github.io/xxx") { // 要遮蔽的網頁
document.write("本頁不符合AdSense AdSense 計劃政策,故隱藏廣告。");
} else { // 以下為 google adsense 的第一段程式碼
google_ad_client = "ca-pub-xxxxxxxxxxxxxxxx";
google_ad_slot = "1234567890";
google_ad_width = 300;
google_ad_height = 250;
}
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> // 此為google adsense的第二段程式碼
</script>

另外這邊附上更改 AdSense 廣告程式碼規範

A/B 測試

如果要 A/B 測試的話可以參考官網的範例,如下,這是非同步廣告程式碼,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-publisher-id" crossorigin="anonymous"></script>
<ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-publisher-id">
</ins>
<script>
if (Math.random() < .5) {
mySlotId = '1234567890';
} else {
mySlotId = '2345678901';
}
(adsbygoogle = window.adsbygoogle || []).push({
params: { google_ad_slot: mySlotId }
});
</script>

參考
設定特定網頁遮蔽google AdSense廣告的方法

分享到