Problem/Motivation

Google recently launched a new version of the AdSense code. The new AdSense code allows AdSense to trigger optimization features earlier which can improve the performance of the ads on your site. The new ad code works for both Auto ads and ad units.

Proposed resolution

Auto ads code example:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234" crossorigin="anonymous"</script>

Ad unit code example:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234" crossorigin="anonymous"</script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1234"
     data-ad-slot="5678"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

Source

Google Announcements from July 19, 2021

Issue fork adsense-3226234

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bataboza created an issue. See original summary.

  • jcnventura committed 28a8615 on 8.x-1.x
    Issue #3226234 by jcnventura: Improving ad performance with the new...
jcnventura’s picture

Status: Active » Fixed
it-cru’s picture

@jcnventura: Would it not be better to use hook_library_info_alter() to add dynamic configured client=ca-pub-1234 URL parameter to adsense.adsbygoogle JS library, use library than via TWIGs attach_library('adsense/adsense.adsbygoogle') and get rid of defer variant?

I currently testing hard-coded client=ca-pub-1234 URL parameter with async on one of my sites and it brings better results in Google's Lighthouse reporting than my previous provided defer solution.

What do you think?

jcnventura’s picture

Hard to understand what you mean here. So the new code is better than with defer, and that option should be removed?

it-cru’s picture

@jcnventura: What I currently did is to add my ca-pub-1234 as client URL parameter manually to adsense.adsbygoogle library for first testings and switched from defer back to async, but with usage of TWIGs attach_library() calls. So I got new Adsense script into head.

As far as I understand we don't need this script added to each Adsense ad anymore. See also https://support.google.com/adsense/answer/9274516?hl=en

I haven't tested new Adsense script with client URL parameter together with defer option. I will try to test this and if I have time I will also try to provide a patch with usage of hook_library_info_alter() to add client URL parameter dynamically to the adsense.adsbygoogle library. Maybe also with an optional switch from async to defer.

it-cru’s picture

@jcnventura: I've opened a MR related to this issue https://git.drupalcode.org/project/adsense/-/merge_requests/1 which use hook_library_info_alter() to add client URL parameter and set defer if it is enabled.

I've attached 2 Google Lighthouse reports which currently show no big difference between usage of async or defer in Adsense JS included in head on my site. So no clue if defer option is really required anymore or not.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.