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.
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.
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.
Split the Adsense Ad Unit Code
After you create an ad unit in Adsense, you’ll get a code which looks similar to this:
<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>
What you need to do is to split it into two codes. The first one consists only of scripts:
<script async src="https//forms.omnisrc.com//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
Also, let’s add an additional line of code which will instruct the Adsense script to display personalized ads (or not).
<script async src="https//forms.omnisrc.com//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds =1; (adsbygoogle = window.adsbygoogle || []).push({}); </script>
And the second one should consist of everything between the <ins> and </ins> tags.
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-xxxxxxxxxxxx" data-ad-slot="xxxxxxxxxx" data-ad-format="auto"></ins>
Now, we’ll implement the first code via GTM and you should add the second one directly into the website’s source code.
If you have, say, 5 ad units, you should prepare 5 <ins> codes. They are all different because they contain different information, like ad slot id. As for the <script> codes, all of them are the same on all ad units, therefore we will implement only one code in GTM (which will serve all the ad units).
P.S. I haven’t tested this solution on websites with infinite scroll or websites which dynamically load ad units after browsing for a while so keep that in mind. This solution works properly with regular smaller websites which load all ad units at once.
Install Google Adsense with Google Tag Manager
Step 1. Implement a Cookie consent mechanism on your website via GTM
This blog post will not dive deep into the cookie consent topic because it is already covered here. If you wish, you can use other alternatives, for example, Cookiebot (here’s a guide by Matteo Zambon).
STEP 2. Create a Lookup table Variable
I’ll explain all the further steps if you have implemented the cookie consent mentioned in my guide. If you chose another solution, you might need to do some modifications on your end.
Anyway, after you implement my cookie consent solution (powered by OneTrust), you’ll get a bunch of variables and triggers. The first thing we’ll need is a variable “Marketing cookies allowed” which returns either true or false (depending on visitor’s/user’s consent). In my solution, that variable is called Custom JS – Marketing Cookies Allowed.
The issue that we need to solve is the output of this variable, it returns either true or false. Unfortunately, this is not very useful for us because the modified Adsense <script> code requires to pass either a 1 or 0 (I’m talking about that requestNonPersonalizedAds).
<script async src="https//forms.omnisrc.com//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds = 1; (adsbygoogle = window.adsbygoogle || []).push({}); </script>
Therefore, we need to do some modifications in GTM. If a visitor accepts marketing cookies (when the value of our Custom JS – Marketing Cookies Allowed equals to true), we need to set requestNonPersonalizedAds to 0. Otherwise, to 1.
And that’s where the Lookup Table variable becomes very handy. It will check the value of the Custom JS – Marketing Cookies Allowed variable and will return 0 or 1 respectively.
Here are its settings:
STEP 3. Create A Custom HTML tag for Adsense code
Let’s create the Custom HTML tag where you need to paste this code:
<script async src="https//forms.omnisrc.com//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds = 1; (adsbygoogle = window.adsbygoogle || []).push({}); </script>
But instead of a “1” we should insert the aforementioned Lookup table variable. If marketing cookies are accepted, it will insert “1”, if they are declined, then “0”.
<script async src="https//forms.omnisrc.com//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds ={{Lookup - Adsense - Personalized Ads}}; (adsbygoogle = window.adsbygoogle || []).push({}); </script>
If you’re new to inserting variables in the Custom HTML tag, you might find this guide useful.
STEP 4. Assign triggers to the Adsense TAg
If you have installed my GDPR cookie consent solution, here are the triggers you should assign to this Adsense tag:
If you have some other cookie consent solution, let me explain what do these triggers mean.
- First of all, let’s take a closer look at the blocking trigger. It blocks the tag on any interaction as long as the visitor/user hasn’t given the consent to Functional cookies. This means that if a person has declined functional cookies, Adsense banners will not be displayed at all.
- Now, the Custom trigger called Optanon Consent Updated. This trigger fires the tag when a visitor gives any consent to be tracked. But if the consent was not given to Functional cookies, the blocking trigger will be activated, therefore, in the end, the Adsense tag will not fire. But if the consent of functional cookies was given, the tag will fire properly.
- If the consent to Functional cookies was already given in one of the previous pageviews/sessions, then the tag will be fired by the Pageview trigger.
If you want to take a closer look at what are the exact configuration of these triggers, download this GTM recipe, import into an empty GTM container and have a closer look.
Time to test
The final step required to successfully install Google Adsense with Google Tag Manager is … drumroll please… testing! Here is the expected result of this entire implementation:
- A visitor lands on a page and sees the cookie consent notification. No ads are displayed yet because the Adsense tag in GTM hasn’t fired yet (the blocking trigger Blocking – Functional Cookies are Not Allowed is active).
- A user gives a consent to functional cookies. The ads are now displayed (thanks to the Custom – Optanon Consent Updated trigger). If you refresh the page, the ads will be displayed immediately because the consent is already given (and the Pageview – All Pages – Functional Cookies Allowed trigger knows that).
- If a user gives a consent to marketing cookies, personalized ads will be served. If they are declined, then non-personalized ads are displayed.
How can we check whether ads are personalized or not? We’ll do that by checking network requests in a browser (#LetsDiveIntoDeveloperZone).
- In your browser, open developer tools and go to the Network tab (here are the instructions how you can do that with Chrome)
- In the filter field, enter ads? and refresh the page with Adsense ads on it.
- You’ll see at least one filtered request made to the address http://googleads.g.doubleclick.net… Click it.
- In Headers tab you’ll see a bunch of parameters (Query String Parameters) passed along. Start looking for the npa parameter. If it exists (with the value of 1), ads are served as non-personalized. If npa does not exist, the ads are personalized.
By the way, be very careful if you switch from Network to Elements tab, and when you switch from Headers to Preview or Response. The content of those tabs usually include the AdSense ad link, and click on that link is – click on your own ad.
Install Google Adsense with Google Tag Manager: Final words
So now you know how to install Google Adsense with Google Tag Manager and configure it to respects visitor’s privacy preferences.
- If a visitor agrees to all the tracking cookies, your ads will be displayed normally and as personalized (therefore, more profitable).
- If the consent to marketing cookies is not given, ads are still displayed but as non-personalized (less profitable but better than no ads).
- If no consent is given, ads are blocked.
In order to achieve such result, you have to install a GDPR cookie consent mechanism via GTM (just like this one), then split Adsense ad unit codes into two parts: add one directly to the website’s code and the other one should be implemented as a Custom HTML tag in GTM.
Finally, that Custom HTML tag is implemented to respect visitor’s privacy settings.
That’s it for this time! Have you tried this method to install Google Adsense with Google Tag Manager? Have anything to add? Let me know in the comments below.
The post How to Install Google Adsense with Google Tag Manager (GDPR ready) appeared first on Analytics Mania.
Source: analyticsmania
Wow…
I was Searching for a solution to deliver adsense using GTM…
No solution seems working… but now I came to understand this one will work…
Let me try it…
Thanks for sharing, waiting to see more…
Good day…
Thanks, Sibi. We will post more articles related to this so keep in touch 🙂