I added cookieyes to my blog not long ago and I wanted to try out adsense and google analytics which I have not been using for years. Using cookieyes you can obtain consent from your users and achieve GDPR or CCPA compliance. This guide will take you through combining Google tag manager, Cookie yes, Google adsense and google analytics.
This guide assumes you have already set up Google tag manager on your site. We still start by adding cookieyes.
You can use this guide with other analytics and ad networks than those of Google, but since those are the most popular it is written for those. I am using the free version of Cookieyes for this.
Adding Cookieyes in Google Tag Manager
You can easily add Cookieyes to your site by adding a new tag in your google tag manager container:
We then choose a template for the tag:
For cookie yes there is a template in the gallery:
As part of setting up cookieyes you need to add your website key:
In order to obtain your website key, login to cookieyes, go to advanced settings and click on "Get installation code" and you will see the following:
For this site the website key is "8766957a21b51278c623ea8a" for example. Remember to set up the settings for the tag:
Save this and lastly we need to set up a trigger, here I have gone with consent initialization:
Once published you will see the prompt for consent from cookie yes on your website. Next we want to add google analytics!
Setting up variables
Now we have cookie yes on our website. Next is setting up a custom trigger that we will be using for Google adsense and analytics. First we will create a variable:
We will then make a variable based on the cookie that cookie yes sets once a user has given consent:
The cookie name for this variable should be "cookieyes-consent", like blow:
Once you save this user-defined variable we are ready to make a custom trigger! We will make our trigger next:
Setting up custom triggers
First we open up the create trigger dialog:
In the dialog choose the "custom event" trigger and fill it out like this:
- Check "Use Regex matching"
- Set event name to ".*"
- Choose the previously created cookie-yes variable
- Set the condition to "contains" and value to "analytics:yes"
- Name it "cookieyes-analytics" (or something else)
After this, repeat this step but instead of setting the value at the end to "analytics:yes" set it to "advertisement:yes". Name it "cookieyes-ads". We have now created two triggers. One for when the user consents to analytics (google analytics) and one for when the user consents to ads (adsense). Next we will set up google analytics.
Adding Google analytics based on cookie yes consent
You can follow this guide to get started with Google Analytics. Use this guide to obtain your google tag id. For this page it is for example "G-PQQB5XFFKL".
Create a new tag in Google tag manager and select "Google tag" as the configuration:
Use your Google Analytics tag id:
For the trigger, use our recently created "cookieyes-analytics":
This will make this google tag get triggered when the user accepts analytic cookies in cookie yes. If you publish your changes you should now have google analytics enabled when your users consent to analytics.
Adding Adsense based on cookie yes consent
Create a new tag in Google tag manager and select "Custom HTML":
Put your script for adsense into the configuration
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2997413406307797" !!USE YOUR OWN ID HERE!!
crossorigin="anonymous">
</script>
To find your auto ads code for adsense go to www.google.com/adsense/ and:
- Go to Ads
- Edit your site
- Click Get code at the top.
Use this for the tag configuration of your adsense tag.
For the trigger, use our recently created "cookieyes-ads":
Save the tag.
After publishing this, you should see Google adsense ads if you consent to advertisement.
That is all
I hope you enjoyed this post, please post below if anything is unclear. This was a long post!