How to use Google Tag Manager and Google Analytics without Cookies
"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 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.
To initialize Google Tag Manager without cookies, we only need to add two attributes to 'config'
.
The only issue we face without cookies is that we need to save the client_id
somewhere. Normally the client_id
is
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_id
but 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.
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.