如何检测我的 AdSense 广告是否被屏蔽?

发布于 2024-10-18 00:07:06 字数 507 浏览 3 评论 0原文

如果用户安装了某种广告拦截器,广告拦截器当然会删除我网站上的所有广告,并在广告所在的位置留下空白空间。我想通过在其中放置一些其他内容(例如指向我网站最重要页面的链接)来使用该空白空间,为此我需要检测 AdSense javascript 是否已加载。

到目前为止我尝试过的方法:

if (!document.getElementById("google_ads_frame1"))
{
}

并且:

if (typeof(window.google_render_ad) == "undefined")
{
}

在某些情况下,这两种方法似乎都会失败,例如,如果浏览器下载 AdSense javascript 文件的速度有点慢,它会在加载 AdSense 代码之前执行上述代码,并且我最终会隐藏广告甚至没有屏蔽广告的用户。

您对如何确保我的代码在 AdSense 之后运行有什么建议吗?或者其他检测 AdSense 脚本未加载的方法?

If user has some kind of ad blocker installed, ad blocker will of course remove all ads from my website, and it leaves empty spaces where the ads used to be. I would like to use that empty space by putting some other content in it, like links to most important pages of my website, to do that I need to detect if AdSense javascript is loaded.

Methods that I have tried so far:

if (!document.getElementById("google_ads_frame1"))
{
}

and:

if (typeof(window.google_render_ad) == "undefined")
{
}

Both of those seem to fail in certain situation, for example if browser downloads AdSense javascript files a bit slower, it will execute above mentioned code before AdSense code is loaded and I end up hiding ads for users that don't even have ads blocked.

Do you have any suggestions on how could I make sure that my code is run after AdSense? Or some other way of detecting that AdSense scripts are not loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

左耳近心 2024-10-25 00:07:06

如果使用新的 AdSense 代码,您可以进行简单的检查,而无需诉诸内容或 CSS 检查。

像往常一样将广告放置在标记中:

<ins class="adsbygoogle" style="display: block;"
   data-ad-client="ca-pub-######"
   data-ad-slot="#######"
   data-ad-format="auto"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

然后调用页面底部的 adsense 代码(请注意,在调用 >adsbygoogle.js 脚本):

<script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

然后在下面添加这一小段代码:

<script>
if (!adsbygoogle.loaded) {
   // do something to alert the user
}
</script>

当广告投放时,AdSense 始终创建/设置标记 adsbygoogle.loadedtrue已加载。您可以将检查放在 setTimeout 函数中,以将检查延迟几秒钟。

If using the new AdSense code, you can do an easy check, without resorting to content or css checks.

Place your ads as normal in your markup:

<ins class="adsbygoogle" style="display: block;"
   data-ad-client="ca-pub-######"
   data-ad-slot="#######"
   data-ad-format="auto"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

Then you call the adsense code at the bottom of your page (note do not use the "async" flag when calling the adsbygoogle.js script):

<script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

Then add this little snippet of code below that:

<script>
if (!adsbygoogle.loaded) {
   // do something to alert the user
}
</script>

AdSense always creates/sets the flag adsbygoogle.loaded to true when the ads are loaded. You could place the check in a setTimeout function to delay the check by a few seconds.

甜心小果奶 2024-10-25 00:07:06

这种超轻量级的方法似乎是绝对可靠的,并且对所有广告网络都是不可知的。

在您的根文件夹中放置一个名为 ads.js 的文件,并在其中添加以下行:

var can_run_ads = true;

Adblock 不允许加载名为 ads.js 的文件。因此,只需将此代码放在 之前即可。

<script src="/ads.js"></script>
<script>
if(window.can_run_ads === undefined) {
    alert('Please Disable Adblock, ****** ******!');
}
</script>';

我强烈建议将用户重定向到包含如何删除 Adblock 说明的页面。

This ultra-lightweight method seems to be infallible and is agnostic to all ad networks.

Place a file call ads.js in your root folder and add this line inside it:

var can_run_ads = true;

Adblock won't allow a file named ads.js to be loaded. So, just place this code before </body>

<script src="/ads.js"></script>
<script>
if(window.can_run_ads === undefined) {
    alert('Please Disable Adblock, ****** ******!');
}
</script>';

I strongly recommend to redirect the user to a page that has instructions on how to remove Adblock.

假面具 2024-10-25 00:07:06

window.onload 事件上运行此代码。页面加载完成后会触发 window.onload 事件。

window.onload = function() {
  // your checks
}

如果您使用 jQuery,请使用

$(window).load(function() {
  // your checks
});

Run this code on the window.onload event. window.onload event is fired when the page has completed loading.

window.onload = function() {
  // your checks
}

If you're using jQuery, use

$(window).load(function() {
  // your checks
});
饮惑 2024-10-25 00:07:06

我建议你使用FuckAdBlock来检测AdBlock是否被激活。如果是这种情况,您可以在页面中添加任意数量的 html。

请参阅文档以获取示例:https://github.com/sitexw/FuckAdBlock

I suggest you use FuckAdBlock to detect if AdBlock is activated. If this is the case, you are free to add as many html in your page you want.

See the documentation for examples: https://github.com/sitexw/FuckAdBlock

倥絔 2024-10-25 00:07:06

检测 ins.adsbygoogle 中的 data-ad-status="unfilled" 参数以了解广告位是否为空。

Detect data-ad-status="unfilled" parameter in ins.adsbygoogle to know if the adslot is empty.

聆听风音 2024-10-25 00:07:06

经过更多测试后,我意识到问题并不像我之前想象的那样与时间有关。问题是 AdSense 开始使用一些新代码,该代码没有“google_render_ad”功能,并且不会创建 ID 为“google_ads_frame1”的 iframe(新 ID 为“aswift_0”)。
考虑到 AdSense 现在至少对 iframe 使用两个不同的 ID,我制作了新的检测代码,仅检查 iframe 元素是否存在,而不管其 ID:

if (document.getElementsByTagName("iframe").length === 0)
{
    // ad is blocked
}

After some more tests I realized that issue was not about timing as I previously thought. Problem was that AdSense started to use some new code, this code doesn't have "google_render_ad" function and it doesn't create iframe with ID "google_ads_frame1" (new ID is "aswift_0").
Considering that AdSense now uses at least two different IDs for iframes I made new detection code that checks just for presence of iframe element regardless of its ID:

if (document.getElementsByTagName("iframe").length === 0)
{
    // ad is blocked
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文