Menu

Gray Matter @ rtcx.net

Close

Fix Core Web Vitals Slow LCP and Google AdSense

RT Cunningham | May 21, 2021 (UTC) | Web Development

Fix Core Web Vitals Slow LCP and Google AdSenseCore Web Vitals is a report available at the Google Search Console. It tells you which URLs need improvement and which ones do not. One of the metrics is LCP, which stands for Largest Contentful Paint. If you advertise on your website with Google AdSense and you use Google Search Console, you may have seen this report. What does LCP really mean?

According to Google, the measurement of LCP is the time it takes for the browser to render the largest visible element in the viewport. A good score is 2.5 seconds or less.

The Problem Is Caused by Auto Ads and Matched Content

But only on mobile devices. When I used the standard ad units, without any Matched Content units, my LCP was always under 2.5 seconds. As soon as I added one Matched Content ad unit, it went above that threshold. There was nothing I could do to lower it using what Google gave me to work with. Auto ads, without the Matched Content unit, behaved in much the same way. I did a lot of testing over the past couple of months.

Desktop LCP performance wasn’t affected like mobile devices and never went above 2.5 seconds with the LCP. Even so, I’m sure the performance could have been better.

The Fix

I found the fix at GulshanBlog. More specifically, in a post titled: “How to Setup Lazy Load for Google AdSense Ad units?

I’m pretty sure Gulshan didn’t invent the code I’m sharing here (in case his blog post ever disappears) because I found it at several older sources when searching for the specific function he provided. I can give him credit, however, because this is the only source that told me how to use it with Google AdSense.

It only works with standard ad units, including the Matched Content ad units (which I have to mention because auto ads also include Matched Content units). It’s very simple and here are the steps:

1. Add this script before the closing body tag in the footer area:

<script type="text/javascript">
    function downloadJSAtOnload() {
        var element = document.createElement("script");
        element.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
        document.body.appendChild(element);
    }
    if (window.addEventListener)
        window.addEventListener("load", downloadJSAtOnload, false);
    else if (window.attachEvent)
        window.attachEvent("onload", downloadJSAtOnload);
    else window.onload = downloadJSAtOnload;
</script>

2. Place an AdSense ad unit.

3. Remove the first script line of the ad code.

4. Repeat steps 2 and 3 for every ad unit you place.

That’s all there is to it. I held off from publishing this until I had tested every combination of standard and auto ads I could think of. My LCP now hovers around 1.6 seconds and the ads are truly deferred until a page loads completely in every web browser I’ve tested them with.

Share: Facebook | Twitter

These Posts May Also Be Interesting:


Gray Matter @ rtcx.net
Copyright © 2020, 2021
RT Cunningham

Blogroll and Other Links: