如何检测我的 AdSense 广告是否被屏蔽?
如果用户安装了某种广告拦截器,广告拦截器当然会删除我网站上的所有广告,并在广告所在的位置留下空白空间。我想通过在其中放置一些其他内容(例如指向我网站最重要页面的链接)来使用该空白空间,为此我需要检测 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果使用新的 AdSense 代码,您可以进行简单的检查,而无需诉诸内容或 CSS 检查。
像往常一样将广告放置在标记中:
然后调用页面底部的 adsense 代码(请注意,在调用
>adsbygoogle.js
脚本):然后在下面添加这一小段代码:
当广告投放时,AdSense 始终创建/设置标记
adsbygoogle.loaded
为true
已加载。您可以将检查放在 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:
Then you call the adsense code at the bottom of your page (note do not use the
"async"
flag when calling theadsbygoogle.js
script):Then add this little snippet of code below that:
AdSense always creates/sets the flag
adsbygoogle.loaded
totrue
when the ads are loaded. You could place the check in a setTimeout function to delay the check by a few seconds.这种超轻量级的方法似乎是绝对可靠的,并且对所有广告网络都是不可知的。
在您的根文件夹中放置一个名为 ads.js 的文件,并在其中添加以下行:
Adblock 不允许加载名为 ads.js 的文件。因此,只需将此代码放在
之前即可。
我强烈建议将用户重定向到包含如何删除 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:
Adblock won't allow a file named ads.js to be loaded. So, just place this code before
</body>
I strongly recommend to redirect the user to a page that has instructions on how to remove Adblock.
在
window.onload
事件上运行此代码。页面加载完成后会触发window.onload
事件。如果您使用 jQuery,请使用
Run this code on the
window.onload
event.window.onload
event is fired when the page has completed loading.If you're using jQuery, use
我建议你使用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
检测 ins.adsbygoogle 中的 data-ad-status="unfilled" 参数以了解广告位是否为空。
Detect data-ad-status="unfilled" parameter in ins.adsbygoogle to know if the adslot is empty.
经过更多测试后,我意识到问题并不像我之前想象的那样与时间有关。问题是 AdSense 开始使用一些新代码,该代码没有“google_render_ad”功能,并且不会创建 ID 为“google_ads_frame1”的 iframe(新 ID 为“aswift_0”)。
考虑到 AdSense 现在至少对 iframe 使用两个不同的 ID,我制作了新的检测代码,仅检查 iframe 元素是否存在,而不管其 ID:
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: