How to Install Google Adsense with Google Tag Manager (GDPR ready)

If you run a website and want to earn some beer money (or more, if your traffic is more significant), Google Adsense is one of the options for how to generate some passive income. Once you sign up, generate codes for ad placements and add them to your website.

When a visitor lands on your page (and if he/she is not using adblockers) that piece of code will load an ad/banner. After that banner is displayed/clicked, your account will get some revenue share from Google. Easy as that.

If you want, you can add more ad placements to a website but every additional ad will require an additional code snippet added to a website. Anyway, no big deal.

Now, after GDPR went into effect (on May 25th, 2018), Adsense ads also became affected by this EU regulation (because those ads deal with personal data). In this guide, I’ll show you how to install Google Adsense with Google Tag Manager and to make it GDPR compliant (meaning that it will adapt to user’s privacy settings).

Before we continue, once again, here’s disclaimer that I’m not a lawyer and this guide is my interpretation of GDPR and how Adsense should be configured in its context. If you disagree with some statements, feel free to post a comment. I’m always willing to find the middle ground or to learn something new.

Google Tag Manager Course - Learn More

Quick refresher: how GDPR affects Adsense ads

General Data Protection Regulation (GDPR) is a regulation created by the European Union which aims to protect personal data. If you wish to collect and process personally identifiable information (PII), you need to get the consent first.

To be more precise, you need to get the following consent (here’s more info):

  • to use cookies or other local storage where legally required
  • and to collect, share and use personal data for the personalization of ads

Adsense ads embedded on your site are related to both consent groups, therefore you’ll need to get both consents. Let’s take a look at how we can do that with Google Tag Manager.

 

GDPR Cookie Consent (with Google Tag Manager)

In this guide, I presume that you already are familiar with how to install GDPR cookie consent with GTM, therefore I’ll skip all the details related to this topic. If you’re new to it, read this guide.

In a nutshell, here’s how it works:

  • A visitor lands on a page and he/she sees a cookie banner which asks for permission to track.
  • Once a permission is given, it is stored in a cookie and all the tags in GTM are configured to respect that preference. This means that if a visitor declines to be tracked for marketing purposes, all marketing-related tags must not fire.
  • If the visitor declines to be tracked at all, all the tracking tags will be blocked inside the GTM container.

By using this mechanism we will also adapt the Adsense ad code snippets.

  • If a visitor declines marketing cookies, your Adsense ads will be non-personalized.
  • If a visitor declines functional cookies, Adsense ads will not be displayed at all.

 

A quick look at the Adsense code

Before we learn how to install Google Adsense with Google Tag Manager, let’s take a quick look at some basics. Once you signup to Adsense and create a new ad unit (placement), you’ll a code which might look like this (just with different ids):

<script async src="https//forms.omnisrc.com//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Ad unit name -->
<ins class="adsbygoogle"
 style="display:block"
 data-ad-client="ca-pub-xxxxxxxxxxxx"
 data-ad-slot="xxxxxxxxxx"
 data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

If you create another one, it will look identically (except for IDs and, probably, some other settings). The code consists of 3 parts:

  • Top <script>
  • <ins> element
  • Bottom <script>

All of them are required for ads to be displayed. The <ins> element is responsible for storing some ad-unit-related settings, like ad slot ID (data-at-slot) or ad format (data-ad-format).

By default, this code will load personalized ads, unless you turn off personalization in your Adsense account. But that does not sound like a good option because you will disable personalization to all visitors, even if they agree to be tracked for marketing purposes.

Ad personalization settings

Non-personalized ads drive lower revenue to businesses, therefore, they need to find another solution how to adjust ad personalization and disable it only for those who do not want that.

Luckily, this can be achieved via Adsense code snippet by adding one additional line:

(adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds = 1;

If requestNonPersonalizedAds equals 1, then a visitor will see non-personalized ads. In the case of 0, ads will be personalized.

 

So what’s the plan?

Here’s what needs to be done in order to install Google Adsense with Google Tag Manager:

  • First, we need to take the initial Adsense ad unit code (that Adsense gives to publishers) and split it into two parts. Once must be added to the website’s code (directly) and the second one should be implemented via GTM (at least that’s the way how I do it). If you want to go next level and inject the full Adsense code via GTM, read this guide by Simo (and that is much more advanced technique).
  • Even if the part of the ad unit code is added to the website, it won’t display any ads unless GTM fires the actual script. In fact, you won’t see anything at all. There will be nothing visible instead of that ad, no placeholder or empty space.
  • We’ll configure the script in the GTM to respect user’s privacy preferences:
    • If a user agrees to functional and marketing cookies, Adsense will serve personalized ads.
    • If a user agrees only to functional cookies, Adsense will serve non-personalized ads. Even though non-personalized ads don’t use cookies or mobile ad identifiers for ad targeting, they still use cookies or mobile ad identifiers for frequency capping, aggregated ad reporting and to combat fraud and abuse. Therefore, you must obtain consent. I interpret that this type of consent falls under the “functional cookies” definition.
    • If a user does not agree to any cookies at all, Adsense script will not be loaded at all, therefore no ads will be visible.

Julius Fedorovicius

avatar
  Subscribe  
Notify of