DDMSEO - Full Stack Developer based in London

Optimize your website SEO with X-RAY SEO Auditor

Published: 2nd May 2017 | Words: 698 | Reading Time: 4 minutes

Categories / Tags:

Marketing
Google Tag Manager
Google Analytics

Storing your Google Analytics tracking ID in Google Tag Manager

After you create an account on Tag Manager for a specific website, the first step to creating the analytics.js tracking code using Google Tag Manger v2 (version 2) is to store your analytics tracking ID in Google Tag Manager. This ID is used no matter what kind of analytics tracking your use whether it be diretly on your webpage through JavaScript or through Tag Manager and is specific to a particular view (website) within an Analytics account. You can find this in Google Analytics under:

Admin > Tracking Info > Tracking Code

It will take the format UA-XXXXXXXX-X. Copy this code, go to Google Tag Manager and navigate to:

Variables > New > Constant

A constant in Google Tag Manager is the same as a constant in PHP. It stores a constant value that doesn't change and is the perfect thing to store our tracking ID within. Click "Untitled Variable" and change the name to "Tracking ID".

Next under "2. Configure Variable" > "value", paste or type in the UA-XXXXXXXX-X tracking ID and click "Create Variable". This tracking ID is now available within this account in Google Tag Manager. When you create a Tag you need to provide the tracking ID, and storing it this way saves you having to type it out over and over. If the tracking ID ever changes you also won't have to change it in multiple places, only in the variable. The Tracking ID variable is always available under "Variables" in the left navigation.

Creating the Google Analytics tracking code

In the left sidebar click "Tags" and create a new tag. Creating this tag is a 4 stage process. Firstly you select the product; which in this case is Google Analytics.
Next you choose the tag type which should always be "Universal Analytics" which is Google latest version of Analytics.
When you reach step 3 "Configure Tag" you are straight away presented with an area to put your Tracking ID. If you type {{ within this field a list of variables will appear and you should see "Tracking ID" listed. Variable names are listed here alphabetically. Click your variable and the field should auto-fill with {{Tracking ID}}. Take note of this method as its how you can call variables within fields within your tag. We returned a constant here; a set value, but variables can also store complex functionality and we could populate this field or any other field with any return value we want through a variable. Below the tracking field you should see "Track Type" and it should already be set to "Page View". For the Analytics tracking code tag this is exactly what we want; we want to track a single page view. We don't need to change anything here and we can move onto step 4.
This step contains our trigger / what our tag will fire on. For the analytics.js code its extremely easy to configure this, we simply need it to fire on "All pages". Save your tag by clicking "Create Tag".
Now we have a tag that fires on every page and sends 1 pageview, which is what the Google Analytics tracking code does. The below JavaScript code is usually what we use to set up basic Google Analytics tracking and although it looks complicated, basically all it does is set our tracking ID, load analytics.js (the main Analytics library), and send a page view.

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXXXXX-X', 'auto'); // load the analytics.js library and set our Tracking ID
ga('send', 'pageview'); // send a page view
Rate this article

Average: 0.00 out of 5 / Votes: 0

Comments

Please login or register to post comments

Login | Register