How to setup Google analytics and adsense with cookieyes using Google tag manager

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:

google tag manager add new tag

We then choose a template for the tag:

google tag manager choose tag type

For cookie yes there is a template in the gallery:

google tag manager use cookie yes tag template

As part of setting up cookieyes you need to add your website key:

google tag manager cookie yes configuration 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:

finding the cookie yes website key

For this site the website key is "8766957a21b51278c623ea8a" for example. Remember to set up the settings for the tag:

google tag manager cookie yes setup settings

Save this and lastly we need to set up a trigger, here I have gone with consent initialization:

google tag manager setup trigger

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:

google tag manager make user defined variable.jpg

We will then make a variable based on the cookie that cookie yes sets once a user has given consent:

google tag manager first party cookie.jpg

The cookie name for this variable should be "cookieyes-consent", like blow:

google tag manager setup first party cookie

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:

google tag manager create trigger

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)

google tag manager make custom trigger

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:

google tag manager set up tag with google tag

Use your Google Analytics tag id:

Google tag manager create tag

For the trigger, use our recently created "cookieyes-analytics":

setup google tag trigger

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":

google tag manager custom html tag

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":

Google tag trigger

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!