有没有延迟加载 Adsense 广告的工作方法?
我在堆栈溢出和谷歌中检查了几个答案,所有这些方法都只是延迟加载,只有当用户在视口(viewport)中可见时才需要加载广告;就像延迟加载图像一样。
最佳答案
是的,可以延迟加载 Adsense,您需要使用开源库,例如 LazyHTML无需修改 Adcode 即可延迟加载 Adsense 广告。
在以下示例中,将 ca-pub-xxx 替换为您的 Google Adsense 客户端 ID,您还必须替换槽 ID。根据您的需要进行更改。
将以下代码添加到 头 .
<script async src="https://cdn.jsdelivr.net/npm/lazyhtml@1.2.3/dist/lazyhtml.min.js" crossorigin="anonymous"></script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxx" crossorigin="anonymous"></script>
将广告代码包裹在 中lazyhtml wrapper 。<div class="lazyhtml" data-lazyhtml onvisible>
<script type="text/lazyhtml">
<!--
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXX"
data-ad-slot="YYYY"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
-->
</script>
</div>
步骤和注意事项:帮助链接:
来源:Lazy Loading Adsense Ads
惰性 HTML 包装器:LazyHTML Converter
关于lazy-loading - 如何延迟加载 Adsense 广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69036877/