I've been testing Ad Unit Lazy Loading with AdSense and found that while Ads Manager correctly loads the ad markup when the ad unit reaches the viewport, the AdSense ad itself doesn't render. The <ins class="adsbygoogle"> is inserted along with the normal AdSense initialization script, but the dynamically inserted script isn't executed.
I fixed it with a small helper that watches lazy-loaded Ads Manager units and initializes AdSense after the markup has been inserted. I'm posting it here both for Siropu and in case anyone else using AdSense wants to implement lazy loading for their ad units:
JavaScript:
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.samItem[data-ll="true"]').forEach(unit => {
const observer = new MutationObserver(() => {
const ads = unit.querySelectorAll('ins.adsbygoogle:not([data-adsbygoogle-status])');
if (!ads.length) {
return;
}
ads.forEach(ad => {
try {
(window.adsbygoogle = window.adsbygoogle || []).push({});
} catch (error) {
console.error('AdSense lazy-load initialization failed:', error);
}
});
observer.disconnect();
});
observer.observe(unit, {
childList: true,
subtree: true
});
});
});
This has been working reliably with AdSense code ads.
I've also been tracking the results in AdSense, and so far the results have been very encouraging. Before enabling Ad Unit Lazy Loading, my Active View viewability was averaging 40%. It is now averaging 57%, while impressions per page have dropped 29%. Despite serving nearly 30% fewer ad impressions, Page RPM has not declined and has actually been running slightly higher more recently.
It's much too early to know whether that increase is related to the improved viewability or simply normal variation, but there has been no indication so far that eliminating those low-viewability impressions has hurt revenue, just the opposite it has gone up.
The potential longer-term benefit is even more interesting. Viewability is a signal advertisers use when evaluating inventory and making bidding decisions, and campaigns can target inventory based on expected viewability. Eliminating impressions that are unlikely to be seen therefore improves the measured quality of the remaining inventory and potentially makes it more competitive for advertiser demand and higher bids.
Whether that is already contributing to the recent increase in Page RPM is impossible to say yet, again it may simply be normal variation. But the immediate result is clear: I've substantially improved the viewability of the inventory without sacrificing revenue.
I also experimented with having my helper preload lazy ad units shortly before they reached the viewport. After testing both ways, I removed the preload and now let Ads Manager handle the lazy-loading trigger normally. Since the ads are loading quickly enough without it, I don't currently see a benefit to the additional preload.
Some individual units showed just how many unnecessary impressions were previously being generated. My above-footer unit, for example, averaged 8% Active View before lazy loading and is currently averaging 52%.
Thought I'd pass this along in case you want to handle the AdSense initialization directly in Ads Manager when Ad Unit Lazy Loading is enabled, or in case anyone else using AdSense wants to use the helper in the meantime.