philschmid blog

How to use Google Tag Manager and Google Analytics without Cookies

Philipp Schmid
June 6th, 2020 · 5 min read
#Analytics
#Web

Photo by Mnz on Unsplash

Introduction

“Web analytics is the measurement, collection, analysis, and reporting of web data for purposes of understanding and optimizing web usage. However, Web analytics is not just a process for measuring web traffic but can be used as a tool for business and market research, and to assess and improve the effectiveness of a website.” Source

so web analytics enables you to:

  • connect your user behavior with technical insights.
  • improve your customer experience, by understanding your users and where they might get stuck.
  • track the value of expenses through user conversions.
  • learn to know your target group and how you can reach them.

Due to these facts, more than 50 million websites/web apps around the world use analytics tools like Google Analytics. Most of these tools use cookies to track user behaviors. If you live in Europe you probably have heard of the GDPR, the regulation in EU law on data protection and privacy. Due to the GDPR, it is no longer easy to use cookies for web analytics. I am neither a lawyer nor I want to go into detail here. If you want to know more about it and be secure you have to talk to a lawyer. Google also provides a website with information about it.

But I can help you learn how to use Google Tag Manager and Google Analytics without cookies.


Google Tag Manager

Google Tag Manager Logo

Google Tag Manager is a free tool, which allows you to manage and deploy marketing/analytics tags (snippets of code) on your website/web app. These Tags can be used to share information from one data source (e.g. your website) to another data source (e.g. Google Analytics).

The key components of Google Tag Manager are Tags, Triggers and Variables.

Tags are snippets of code, which tell Google Tag Manager what to do. Examples of Tags are Google Analytics, Google Adwords, Facebook Pixel.

Triggers are the way events are handled. They tell Google Tag Manager what to do and when to do it. Examples of Triggers are page view, window.loaded, clicks, Javascript errors, or custom events (Javascript functions).

Variables are additional information for your Tags and Triggers to work. Examples are DOM elements, click classes, click text.

Google even has a video series on how to get started with Google Tag Manager.


Purpose

I write this tutorial because I saw a gap in the documentation of Google Tag Manager and how to use it without cookies. The Google Analytics documentation already includes examples on how to use Google Analytics without cookies but not for Google Tag Manager.

Google Tag Manager has different attribute keys when using the Tag Google Analytics than using Google Analytics standalone. In Google Analytics you have the attribute clientId and in Google Tag Manager you have the attribute client_id.

Google is providing a list of these field mappings but the list is missing the important attribute mapping for storage, which is needed to prevent the creation of cookies.


Tutorial

In this tutorial, we use Google Analytics as a Tag. Before we get started make sure you have a valid GA_MEASUREMENT_ID. I assume that you have already worked before with Google Analytics and you are therefore familiar with the terminologies used.

In their ”getting started with Google Tag Manager” Gooogle provides a snippet, with which you initialize Google Tag Manager with Google Analytics as Tag. But this would create cookies for it.

1<!-- Global site tag (gtag.js) - Google Analytics -->
2<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
3<script>
4 window.dataLayer = window.dataLayer || [];
5 function gtag(){dataLayer.push(arguments);}
6 gtag('js', new Date());
7
8 gtag('config', 'GA_MEASUREMENT_ID');
9</script>

To initialize Google Tag Manager without cookies, we only need to add two attributes to 'config'.

1<!-- Global site tag (gtag.js) - Google Analytics with out cookies -->
2<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
3<script>
4 window.dataLayer = window.dataLayer || [];
5 function gtag(){dataLayer.push(arguments);}
6 gtag('js', new Date());
7
8 gtag('config', 'GA_MEASUREMENT_ID', {
9 client_storage: 'none',
10 client_id: CLIENT_ID,
11});
12
13</script>

The only issue we face without cookies is that we need to save the client_id somewhere. Normally the client_idis saved in the cookie. To overcome this we save the client_id in window.localstorage. We also need to create the values of the client_id. The client_id is basically the device id.

We can use any value for the client_idbut to be sure we wont get any duplicates we use a uuid` for this.

Below, you find a complete example, which generates a uuid for the client_id save it in window.localstorage and initializes Google Tag Manager.

1<!-- Global site tag (gtag.js) - Google Analytics with out cookies -->
2<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"
3></script>
4<script async src="https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/uuidv4.min.js"
5></script>
6
7<script>
8 // https://developers.google.com/tag-manager/devguide
9 window.dataLayer = window.dataLayer || []
10 function gtag() {
11 dataLayer.push(arguments)
12 }
13 gtag('js', new Date())
14
15 // defines window.localstorage key
16 const GA_LOCAL_STORAGE_KEY = 'ga:clientId'
17
18 // checks if localstorage is available
19 if (window.localStorage) {
20 // checks if user was already connected and loads client_id from localstorage
21 if (localStorage.getItem(GA_LOCAL_STORAGE_KEY)) {
22 // creates new tracker with same client_id as the last time the user visited
23 gtag('js', new Date())
24 gtag('config', 'GA_MEASUREMENT_ID', {
25 send_page_view: true,
26 client_storage: 'none',
27 client_id: localStorage.getItem(GA_LOCAL_STORAGE_KEY),
28 })
29 } else {
30 // creates client_id and saves it in localStorage -> currently random number better would be a uuid
31 window.localStorage.setItem(GA_LOCAL_STORAGE_KEY, uuidv4())
32 // creates new tracker with the new client_id
33 gtag('js', new Date())
34 gtag('config', 'GA_MEASUREMENT_ID', {
35 send_page_view: true,
36 client_storage: 'none',
37 client_id: localStorage.getItem(GA_LOCAL_STORAGE_KEY),
38 })
39 }
40 }
41</script>

Thanks for reading. If you have any questions, feel free to contact me or comment on this article. You can also connect with me on Twitter or LinkedIn.

More articles from philschmid

BERT Text Classification in a different language

Build a non-English (German) BERT multi-class text classification model with HuggingFace and Simple Transformers.

May 22nd, 2020 · 7 min read

Scaling Machine Learning from ZERO to HERO

Scale your machine learning models by using AWS Lambda, the Serverless Framework, and PyTorch. I will show you how to build scalable deep learning inference architectures.

May 8th, 2020 · 11 min read

Join our email list and get notified about new content

Be the first to receive our latest content with the ability to opt-out at anytime. We promise to not spam your inbox or share your email with any third parties.

© 2020 philschmid|Imprint|Privacy Policy
Link to $https://twitter.com/_philschmidLink to $https://github.com/philschmidLink to $https://instagram.com/schmid_philippLink to $https://www.linkedin.com/in/philipp-schmid-a6a2bb196/