Forum Moderators: martinibuster

Message Too Old, No Replies

Deferring Adsense Ad Execution For Better Page Experience

         

allhearts

12:40 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Google Page Speed Insights rates my recipes pages in the mid 30's, I looked for a way to improve the rating and found this script:

(Script defers execution of the JavaScript until there is a user interaction like scrolling or on a time period. This means the script does not download and execute until after page load)

It improved my rating from mid 30's to upper 90's, the issue I have is, is this legal according to adsense?

I can't find any clarification on it, any feedback is appreciated.

Thank you.

[edited by: martinibuster at 1:03 pm (utc) on Sep 12, 2021]
[edit reason] Removed JS and added a description of the what the WP Plugin does [/edit]

NickMNS

2:57 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



is this legal according to adsense?

Most likely, it really depends on the implementation.

The bigger issue is that the script seems to be poorly thought out. You cannot wait for a scroll or even worse a timer (unless it is really short), the reason is that your users probably wont see any ads. If the user reads the content before scrolling there will be no ad, if the user then scrolls, the ads will begin to load but wont be seen except maybe the last ad on the page. If you wait on a timer, the timers delay, plus load latency will result in far fewer ads being seen.

Moreover inserting ads dynamically will cause CLS. This wont be a problem in terms of page speed metrics but may cause invalid clicks, so you need to be sure that your CSS is such that the ad's parent div has predefined height to prevent the reflow (CLS).

In my implementation I do not lazy load the above the fold ad, it is loaded normally. Then ads load progressively, the first BTF ad loads after initial scroll, then the second when a specific position on the page is reached but the ad is still out of view and so on. This has the added advantage of improving AVV as ads are only loaded if there is a high probability of being seen. The challenge with this method is that you need to experiment with where you place the triggers, and if your ads are close together it wont work either.

allhearts

3:10 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



The problem with this script @NickMNS is that this script effects all ads within a page, my top ad (above the fold) receives the same amount of impressions but less clicks once I have the code in place.

I tried contacting google for clarification but no luck, so I’m stuck and unsure of what to do.

allhearts

3:15 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



So what you use is not against google tos?

With the script I found, it is not possible to load one ad (top) normally and defer the rest within that page,

Now to make this script work, I also had to modify the ad code that calls for the google Java file, and isn’t it against Adsense tos to modify that code as well?

NickMNS

4:17 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So what you use is not against google tos?

FWIW, I have been using my script for years, and there has never been an issue.

I also had to modify the ad code that calls for the google Java file,

It is not a Java file, it is Javascript. Java and Javascript are two distinct and unrelated programming languages.

isn’t it against Adsense tos to modify that code

Yes and no. It is absolutely against the ToS to modifiy the JS code. But I assume that isn't what you are doing. What you are doing is separating the different parts of the html to prevent the JS from being called multiple times.

The way the ad code works is that this line calls the ad code, but is triggered only once on page load.
(adsbygoogle = window.adsbygoogle || []).push({})

So if you only copy the code to the page when a user scrolls, then nothing would happen because the line of code calling the ad wouldn't be itself be called. So the lazy load script has to break that line of code out and then call it once the ad code is added to the page. But really nothing has changed in the code, only the timing of the calls. This isn't against the ToS, and is required for any site that uses an infinite scroll pattern.

londrum

5:11 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Mod: why cant we see the actual script, how is anyone else supposed to comment on it.

allhearts

5:28 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



I posted the script but it was removed.

allhearts

5:41 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



@NickMNS

So the way this script works, you only load googles JavaScript file once per page no matter how many ads, but instead of the code to call for the JavaScript file that google provides, I have to use this one for the refer script:

<script data-ad-client='ca-pub-XXXXXXXXXXXXXXXX' data-src='https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' data-type='lazy'></script>

I ended up removing the script because I didn’t want my Adsense account banned, is that code above legit? It does work, but do you think I could be banned for it?

Thanks

londrum

6:26 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You only have to include Google’s JavaScript file once anyway. You don’t have to include the <script src> bit for every single ad.

allhearts

7:06 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



I have only included it once @londrum, but the code above is not the one from google, it’s the one for the defer script (it’s a bit different), just wondering if that code goes against Adsense tos.

londrum

7:14 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would just use the code they give you, with the 'async' in it, which shouldn't delay the rest of the page loading.
and they've also updated the script tag now with some other stuff like crossorigin="anonymous" as well, which you might want to include
if it's giving you worries then just switch to the proper one.
the official code doesn't give me any speed issues, so there's probably some other stuff on your page that you can improve which will make a bigger difference

allhearts

7:20 pm on Sep 12, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Actually, I am using the original now, and it’s what gives my pages a mid 30’s rating, while when I use the script above, the pages get a upper 90’s rating.

allhearts

8:54 pm on Sep 13, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



By adding "defer" to "<script async defer src=..." does it help anything and is that against the adsense tos?

Thanks

londrum

9:34 pm on Sep 13, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The async is supposed to stop it blocking other parts of your page downloading, so you shouldn’t need defer.
There must be something else going on with your page that is making it slow, otherwise everyone who uses adsense would be having the same problem. My page speed is all right and I use the plain code that google give you.

You don’t want to delay the adsense loading too much, because users might scroll past the ads before they get a chance to appear and that will mess with your earnings

NickMNS

9:57 pm on Sep 13, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



By adding "defer" to "<script async defer src=..."

does it help anything

No.

and is that against the adsense tos?

No.

What async and defer do, is allow the resource to be downloaded without blocking the main thread. The difference between async and defer is that async will add the code execution back into the main thread as soon as the resource is available, whereas defer will further delay code execution until after the main thread has completed what was already in the queue. The delay with Adsense is mostly due to wating for the resources to load, once on the user's device rendering takes a few milliseconds. So using defer doesn't really provide any benefit, but it could cause ads to be rendered a lot later on if there are render blocking resources queued before it.

There must be something else going on with your page that is making it slow, otherwise everyone who uses adsense would be having the same problem.

This is a common problem that affects most people using Adsense.

You don’t want to delay the adsense loading too much, because users might scroll past the ads before they get a chance to appear and that will mess with your earnings

This is absolutely correct, but it is a balancing act, done right the user will be none the wiser that it is happening and the whole page experience will be improved, done wrong and the ads will never be seen. It isn't a one size fits all type of thing.

not2easy

10:30 pm on Sep 13, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



In the opening post it was mentioned that this is a WP site, known to be bloated generally and loading lots of (often unnecessary) scripts in the header. For example, not every site needs a pile of emojis but they seem to be included by default - just in case. WP keeps working on it but they are not known for speed or resource management.

allhearts

10:47 pm on Sep 13, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



@NickMNS

The way you do it, do you modify the adsense code?

NickMNS

12:13 am on Sep 14, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The way you do it, do you modify the adsense code?

As described in my first post.

yaashul

5:09 am on Sep 14, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



I am using lazy load adsense using a wordpress plugin but Even in all ads are in content active view ratio is very poor especially ad just below title (heading).

Site speed average is 2.14 second according to google analytics speed for last 30 days.

allhearts

12:00 pm on Sep 14, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



I don't have a word press website and my site speed averages over 6 seconds and this is mostly because of javascript.

I'm just trying to find a way to lazy load the site without modifying the adsense code, I know there are quite a few WP plugins to do this, but I haven't been able to find anything for my site, I'm not very good with web stuff.

not2easy

1:40 pm on Sep 14, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I don't have a word press website
I apologize. The mention of a WP plugin is the reason I thought is was a WP site. I did not see that clarified, so it seemed the most likely reason for loading issues.

A non-WP site that takes over 6 seconds to load might have other issues such as graphics management. I would start with a free evaluation at w3 - https://validator.w3.org/ to eliminate other possibilities.

allhearts

2:00 pm on Sep 14, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Thank you, the site does good on the speed test when I’m not running any JavaScript, it’s when I run even just 1 ad from Adsense that kills it.

NickMNS

2:08 pm on Sep 14, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it’s when I run even just 1 ad from Adsense that kills it.

That is not normal. It is true that Adsense performs poorly but it's normally not that bad.

allhearts

2:37 pm on Sep 14, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Yeah I know, when I tried that script to defer Adsense ads, I was getting a score on the upper 90’s for the recipe pages.

I just have to find anyway to lazy load my website, so far no luck.

allhearts

4:33 pm on Sep 14, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



@NickMNS

I have replied to your message but not sure if you got it, I cannot see my reply on it.

londrum

5:50 pm on Sep 14, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



are you putting the <script src> tag in the head? or maybe at the end

allhearts

6:36 pm on Sep 14, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



In the head, when I put it in the footer I still got the same amount of impressions but a lot less clicks.

Niresh12495

7:54 pm on Sep 14, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



@allhearts you should not defer Adsense codes, it may induce further delay in rendering ads. Try lazy load.

allhearts

3:15 am on Sep 15, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Been doing some tests to help figure out why this is happening and:

When I have no ads on the pages, it gets a score of 100 on Google's Page Speed Insights.

When I have just 1 ad on the pages, it gets a score of about 50.

I don't know what to do, I really need ads on the pages.

londrum

7:21 am on Sep 15, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What does it do when you include the <script src> line, but remove all the code for the ads.
Maybe it has to redraw the layout when the ads appear