Using multiple Google Analytics tracking codes on web pages

 

This article is in conjunction with the article ‘Introduction to Google Analytics Library – analytics.js’ and ‘Introduction to Google Analytics Commands’. If you want to send data to multiple web properties from a single web page then you need to understand what trackers are and how they can be created and used. Trackers are commonly used for roll up reporting (reporting of data in aggregate form, from multiple websites/mobile apps) and for implementing cross domain tracking.

Introduction to trackers in Google Analytics

Tracking object (also known as tracker) is used to send hit data (pageviews, screenviews, events, transactions etc) to Google Analytics (GA) web property:

tracking object

Without first creating and using a tracking object, you can not send hit data to a GA property.

 

Creating a tracking object

Google Analytics ‘create’ command is used to create a tracking object. This command also associate the tracking object with a web property.

For example, the following line of code, creates a default tracking object through Google Analytics ‘create’ command and then associate the tracking object with the web property whose id is ‘UA-12345-12’:

ga(‘create’, {‘trackingId’: ‘UA-12345-12’},’auto’);

You can also write this line of code, without using field name, like the one below:

ga(‘create’, ‘UA-12345-12′, ‘auto’);

You can see this line of code in your Google Analytics tracking code:

<script>

(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-12345-12′, ‘auto’);

ga(‘send’, ‘pageview’);

</script>

Following fields are commonly used with tracker(s) and can only be set when using the ‘create’ command:

#1 Tracking id (also known as property id). You can set tracking id by using the trackingId field object. This field object accept values of type text.

ga(‘create’, {‘trackingId’: ‘UA-12345-21’});

Note(1): I use and recommend field names, to make the code easier to understand and debug.

Note(2):You have to supply tracking id whenever you use the ‘create’ command method.

#2 Tracker name – it is the name of the tracking object. The name of the default tracker is ‘t0’. So following line of code:

ga(‘send’, ‘pageview’);

is same as:

ga(‘t0.send’, ‘pageview’);

You set tracker name only when you are using a non default tracker. You can set your own tracker name by setting the ‘name’ field:

ga(‘create’, ‘UA-12345-1’,{‘name’:’testProperty’});

 The name field accept values of type text.

#3 Client id – it is a unique random number which is created and assigned by google analytics cookie _ga. This id is used to uniquely identify a user. You can set your own client id by setting the clientId field:

ga(‘create’, ‘UA-12345-1’, { ‘clientId’: ‘8fjfdurehi47-du57ujfdr-379ru307’});

The clientId field accept values of type text.

 

Types of trackers

There are two types of tracking objects: default and user defined tracking objects.

The name of the default tracker is ‘t0’. But we do not refer to default tracker by this name. To refer to a default tracker, just don’t use any tracker name. For example:

ga(‘create’, ‘UA-12345-12?, ‘auto’); // default tracker

ga(‘create’, ‘UA-12345-12′, {‘name’:’testProperty’},‘auto’); // user defined tracker ‘testProperty’

Note: Google refers to user defined trackers as named trackers. So named trackers and user defined trackers are same thing.

 

Using multiple trackers

Each tracking object can send data to only a single GA property.

So following code won’t work, where we are trying to send hit data to two web properties from a single default tracker:

<script>

(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-12345-12?, ‘auto’);

ga(‘create’, ‘UA-1244524-22′, ‘auto’);

ga(‘send’, ‘pageview’);

</script>

single tracker two properties

You need to create two tracking objects, if you want to send hit data to two web properties from a single web page:

two trackers two properties

Similarly, if you want to send website usage data to 3 web properties (3 websites/apps) from a single web page then you need to create and use 3 trackers.

Use the following similar code to send hit data to two web properties from a single web page:

<script>

(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-12345-12′, ‘auto’); // create default tracking object

ga(‘create’, ‘UA-1244524-22′, ‘auto’,{‘name’: ‘WebProperty2’}); // create new tracking object named ‘webProperty2’

ga(‘send’, ‘pageview’); // send pageview hit to web property 1 via default tracker

ga(‘WebProperty2.send’, ‘pageview’); // send pageview hit to web property 2 via  tracker ‘WebProperty2’

</script>

Here ‘WebProperty2’ is the name of the second tracking object. I selected this name for easy reference. You can name the tracking object whatever you want.

 

Running Google Analytics Commands for specific trackers

Pay special attention to the line of code below which sends pageview data to the web property via the tracking object named ‘WebProperty2’:

ga(‘WebProperty2.send‘, ‘pageview’);

When you are using multiple trackers on a web page, you would need to prefix Google Analytics command (like ‘send’, ‘require’, ‘linker’ etc) you use with the tracker name. Otherwise you won’t be able to send hit data to the second web property via the user defined tracker.

Following are the examples of using Google Analytics commands with the second tracking object:

ga(‘create’, ‘UA-1244524-22’, ‘auto’,{‘name’: ‘WebProperty2′,’allowLinker’: true });// allow second web property to pass/accept cookie information

ga(‘WebProperty2.require’, ‘linker’); // load the auto link plugin for the second web property.

ga(‘WebProperty2.linker:autoLink’, [‘xyz.com’] ); // auto link to xyz website

ga(‘WebProperty2.send’, ‘pageview’); // send pageview data to second web property.

ga(‘WebProperty2.send, ‘event’, ‘videos’, ‘predators’); // send event data to second web property

 

Retrieving data stored on a default tracker

To retrieve data stored on default tracker, we use the get method in a ready callback function

Quick Recap of the Callback function

Callback function is simply a function which is passed as an argument to another function. The ga() command queue function can accept another function as an argument.

For example:

ga(function_name);

This function executes once the anaytics.js library has fully loaded. There can be two types of callback functions in GA depending upon how you use it:

#1 hit callback

#2 ready callback

Hit callback function

Use hit callback function when you want to make sure that a hit is being fired.

This function is called after a hit is processed. No arguments are passed to this function. For example:

ga(‘send’, {

‘hitType’: ‘event’,

‘eventCategory’: ‘videos’,

‘eventAction’: ‘Play’,

‘eventLabel’: ‘chappy’,

‘eventValue’: 100,

‘nonInteraction’: true,

‘hitCallback’: function() {alert(‘hit sent’);}

});

Ready callback function

Use ready callback function to retrieve default tracker data.

This function is called after a default tracker has been created. The default tracker is passed as the first and only argument to this function. This argument is ‘undefined’ if no default tracker has been created.

For example, if you want to retrieve Client Id using the default tracker, then you can do that by using the following code:

ga(function(tracker) {

var clientId = tracker.get(‘clientId’); // get client ID from default tracker

console.log(clientId);

});

To validate this code, just copy paste it into the Google developer console and then press enter:

consoleOutput-1

You can retrieve default tracker name by using the following code:

ga(function(tracker) {

var trackerName = tracker.get(‘name’); // get default tracker name

console.log(trackerName);

});

To validate this code, just copy paste it into the Google developer console and then press enter:

consoleOutput-2

To retrieve tracking id, use the following code:

ga(function(tracker) {

var trackingId = tracker.get(‘trackingId’);

console.log(trackingId);

});

output tracking id

To see how the default tracking object actually looks like, use the following code:

ga(function(tracker) {

 console.log(tracker);

});

default tracking object

 

Retrieving data stored on a user defined tracker

If you are not using a default tracker or if you are using multiple trackers on a page, then you can retrieve data from user defined tracker by using getByName and getAll methods in a ready callback function.

You need to use the ready callback function because ga object methods (like getByName and getAll) are only available when analytics.js has fully loaded and ready, so you need to reference them inside a ready callback function.

Note: you can’t use get method directly on a user defined tracker (also known as named tracker). For example if you want to retrieve the name of the user defined tracker, then following code won’t work:

ga(function() {

var trackerName = testProperty.get(‘name’); // can’t use get method directly on named tracker

console.log(trackerName);

});

To retrieve the name and other data of user defined tracker, you need to use either the getByName method or getAll method.

 

getByName method

If you want to retrieve the name of user defined tracker, you can use the following code:

ga(function() {

var nonDefaultTracker = ga.getByName(‘testProperty’);

var trackerName = nonDefaultTracker.get(‘name’); // get the name of user defined tracker

console.log(trackerName); // write tracker name to developers console

});

If you want to retrieve the client ID of user defined tracker, you can use the following code:

ga(function() {

var nonDefaultTracker = ga.getByName(‘testProperty’);

var clientID = nonDefaultTracker.get(‘clientId’); // get the client id of user defined tracker

console.log(clientID); // write client id to developers console

});

If you want to retrieve the tracking ID of user defined tracker, you can use the following code:

ga(function() {

var nonDefaultTracker = ga.getByName(‘testProperty’);

var trackingID = nonDefaultTracker.get(‘trackingId’); // get the tracking id of the user defined tracker

console.log(trackingID); // write tracking id to developers console

});

You can also retrieve name, client id and tracking id of user defined tracker in one go, by using the following code:

ga(function() {

var nonDefaultTracker = ga.getByName(‘testProperty’);

var trackerName = nonDefaultTracker.get(‘name’); // get the name of user defined tracker

var clientID = nonDefaultTracker.get(‘clientId’); // get the client id of user defined tracker

var trackingID = nonDefaultTracker.get(‘trackingId’); // get the tracking id of the user defined tracker

console.log(trackerName); // write tracker name to developers console

console.log(clientID); // write client id to developers console

console.log(trackingID); // write tracking id to developers console

});

Copy paste this code into developer console to validate it:

output named tracker

 

getAll method

This method is used to get an array of all created trackers.

The ga.getAll()[0] array element refers to the default tracker.

The ga.getAll()[1] array element refers to the first non default tracker

The ga.getAll()[2] array element refers to the second non default tracker

For example, if you want to retrieve the name of the default tracker, you can do that by using the following code:

ga(function() {

console.log(ga.getAll()[0].get(‘name’));

});

The output of this code would be: ‘t0’ which is the name of the default tracker.

If you want to retrieve the name of the first non-default tracker, you can do that by using the following code:

ga(function() {

console.log(ga.getAll()[1].get(‘name’));

});

The output of this code would be: ‘testProperty’, because that is the name of the second tracker I use:

second tracker name

But since I use only two trackers, any attempt to retrieve data from third tracker will not work and I will get ‘undefined’ as output. For example:

ga(function() {

console.log(ga.getAll()[2].get(‘name’));

});

Here I am trying to retrieve the name of the third tracking object, which does not exist, so I will get ‘undefined’ as output:

third tracker

To retrieve the client Id and property ID of the first non-default tracker (or named tracker), you can use the following code:

ga(function() {

console.log(ga.getAll()[1].get(‘clientId’));

console.log(ga.getAll()[1].get(‘trackingId’));

});

client tracking id

 

Updating the default tracker data

You can update the default tracker data by using the set command method in the ga() command queue function or by using the set method on the default tracker itself.

Following is an example of using the set command method in the ga() command queue function, in order to update the default tracker data:

ga(‘set’, ‘page’, ‘/virtual/download-seo-guide’);

Here, page is the path portion of the url whose value start with forward slash ‘/’.

Following is an example of using the set method on the default tracker itself, in order to update the default tracker data:

ga(function(tracker) {

tracker.set(‘page’, ‘/virtual/download-seo-guide’);

var page = tracker.get(‘page’);

console.log(page);

});

tracker page output

 

Updating the named tracker data

Google Analytics refer the user defined tracker as named tracker.

You can update the named tracker data by using the set command method in the ga() command queue function or by using the set method on the named tracker itself.

Following is an example of using the set command method in the ga() command queue function, in order to update the named tracker data:

ga(‘testProperty.set’, ‘page’, ‘/virtual/download-seo-guide’);


Following is an example of using the set method on the named tracker itself, in order to update the named tracker data:

ga(function() {

var namedTracker = ga.getByName(‘testProperty’);

namedTracker.set(‘page’, ‘/download-seo-guide’);

console.log(namedTracker.get(‘page’));

});

named tracker data

 

Sending data via default tracker

You can send data via the default tracker data by using the set command method in the ga() command queue function or by using the send method on the default tracker itself.

Following is an example of using the send command method in the ga() command queue function, in order to send pageview data via the default tracker:

ga(‘send’, ‘pageview’);

Following is an example of using the send method on the default tracker itself, in order to send pageview data via the default tracker:

ga(function(tracker) {

tracker.send(‘pageview’);

});

Following is an example of using the send method on the default tracker itself, in order to send event data via the default tracker:

ga(function(tracker) {

tracker.send(‘event’,’videos’,’Play’,’chappy’,100);

});

 

Sending data via named tracker

You can send data via the named tracker data by using the set command method in the ga() command queue function or by using the send method on the named tracker itself.

Following is an example of using the send command method in the ga() command queue function, in order to send pageview data via the named tracker:

ga(‘testProperty.send’, ‘pageview’);

Following is an example of using the send method on the named tracker itself, in order to send pageview data via the named tracker:

ga(function() {

var namedTracker = ga.getByName(‘testProperty’);

namedTracker.send(‘pageview’);

});

Following is an example of using the send method on the named tracker itself, in order to send event data via the named tracker:

ga(function(tracker) {

var namedTracker = ga.getByName(‘testProperty’);

namedTracker.send(‘event’,’videos’,’Play’,’chappy’,100);

});

Enough of theory now. Let us use the concept of trackers to solve real work life problem.

 

Using multiple trackers for Cross domain tracking in Google Analytics

Let us suppose you are using multiple trackers for roll up reporting and you need to implement cross domain tracking on top of that. Now how you will do that?

Before I start, let us do a quick recap of roll up property.

 

Quick Recap of Roll up property

In Google Analytics, a website or mobile application is called a property (also known as web property or digital property).

Unless you are marketing agency/consultant, you are most likely have access to only one Google Analytics account and only one Web property.

In Google Analytics each property is identified by a unique ID known as property ID (or tracking ID).

This tracking ID is in the format: UA-1235-23

Each property contains one or more views and each view contain multiple reports:

properties and views

 

A roll up property is the web property whose hit data comes from other Google Analytics properties.

google-analytics-rollup-propertyThese other properties are known as ‘Source Properties‘ in Google Analytics lingo.

For example in the chart above, property-1, property-2 and property-N are source properties. The roll up property contains hit data from property-1, property-2 and property-N

Since each property in Google Analytics represents a website/mobile application, a roll up property contains data from multiple websites/mobile applications.

Following is another way of looking at the roll up property:

google-analytics-rollup-property2

Just like any other property in GA, a roll up property can also contain one or more views.

These views are called as roll up views:

roll up web property

These roll up views contain multiple reports. The reports contain hit data from two or more websites/apps.

So if you own multiple websites or treat your sub domains as different websites then roll up property could be useful for you.

Note: To learn more about roll up properties, checkout this article: Implementing rollup reporting in Google Analytics.

 

Cross domain tracking via multiple trackers

crossDomainTracking

What we are going to do here, is use two tracking objects for each web property.

So in the case of abc.com, the first tracking object (or default tracking object) is used to send hit data to abc.com. The second tracking object is used to share abc.com hit data with the rollup property.

Similarly, in case of xyz.com the first tracking object (or default tracking object) is used to send hit data to xyz.com. The second tracking object is used to share xyz.com hit data with the rollup property. At the same time we will also implement cross domain tracking between abc.com and xyz.com, so that we can track users’ activities across domains.

Follow the steps below to implement cross domain tracking between two primary domains (abc.com and xyz.com) via mutiple tracking objects:

Step-1: Create a new web property in your Google Analytics account which you will use as a rollup property (if you already don’t have a rollup property)

This rollup property will store hit data from both abc.com and xyz.com websites. For this to happen, both websites abc.com and xyz.com has to share all of its hit data with the rollup property.

If you don’t use rollup property and simply share abc.com and xyz.com hit data with each other by using multiple trackers then both websites’ web property will contain data from two websites and thus make individual analysis of the websites very difficult.

Let us assume that the rollup property ID is: UA-12345-21

 

Step-2: Apply filter to your rollup view which adds hostname to the request URI:

add host name to request URI

Following is the configuration for the filter:

Filter Type: Custom filter > Advanced

Field A: Hostname

Extract A: (.*)

Field B: Request URI

Extract B: (.*)

Output To: Request URI

Constructor: $A1$B1

You need this filter so that you can separate the traffic coming from two different websites in your rollup reports.

 

Step-3: Share abc.com hit data with rollup property – Now in order to share abc.com hit data with the rollup property, you need to create a new tracker in the GA tracking code of abc.com and use the new tracker to send hit data to rollup web property. For example:

<script>

(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-12343-1′, ‘auto’);

ga(‘create’, ‘UA-12345-21′, {‘name’:’rollupProperty’}); // create new tracking object and name it rollupProperty

ga(‘send’, ‘pageview’);  // send pageview data to xyz.com

ga(‘rollupProperty.send’, ‘pageview’); // send pageview data to rollup property

</script>

 

Step-4: Share xyz.com hit data with rollup property – Now in order to share xyz.com hit data with the rollup property, you need to create a new tracker in the UA tracking code of xyz.com and use the new tracker to send hit data to rollup web property. For example:

<script>

(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-155467-2′, ‘auto’);

ga(‘create’, ‘UA-12345-21′, {‘name’:’rollupProperty’}); // create new tracking object and name it rollupProperty

ga(‘send’, ‘pageview’);  // send pageview data to xyz.com

ga(‘rollupProperty.send’, ‘pageview’); // send pageview data to rollup property

</script>

 

Step-5:

# Create a copy of the main view of the website abc.com. Name the new view ‘ABC Cross Domain Tracking View’. We have created this view in order to maintain one unfiltered view.

# Apply custom advanced filter to the new view which add host name (domain name) to the request URI.

# Add ‘abc.com’ in the ‘referral exclusion’ list to avoid self referral issues.

 

Step-6:

# Create a copy of the main view of the website xyz.com. Name the new view ‘XYZ Cross Domain Tracking View’.

# Apply custom advanced filter to the new view which add host name to the request URI.

# Add ‘xyz.com’ in the ‘referral exclusion’ list to avoid self referral issues.

 

Step-7: Setup cross domain tracking between abc.com and xyz.com

The auto link plugin is used to track cross domain traffic in Google Analytics. Google recommends implementing cross domain tracking via this plugin.

In order to use this plugin you need to modify the GA tracking code of both websites abc.com and xyz.com.

Modify the Google analytics tracking code of the website abc.com as shown below:

<script>

(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-12343-1′, ‘auto’,{‘allowLinker’: true }); // set allowLinker to true

ga(‘create’, ‘UA-12345-21′, {‘name’:’rollupProperty’,’allowLinker’: true}); // set allowLinker to true for rollupProperty

ga(‘require’, ‘linker’); // setup autoLink plugin

ga(‘rollupProperty.require’, ‘linker’); // setup autoLink plugin for rollup property

ga(‘linker:autoLink’, [‘xyz.com’]); // Auto link to xyz website

ga(‘rollupProperty.linker:autoLink’, [‘xyz.com’]); // Auto link to xyz website

ga(‘send’, ‘pageview’);

ga(‘rollupProperty.send’, ‘pageview’); // send pageview data to rollup property

</script>

 

Similarly, modify the Google analytics tracking code of the website xyz.com as shown below:

<script>

(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-155467-2′, {‘allowLinker’: true }); // set allowLinker to true

ga(‘create’, ‘UA-12345-21′, {‘name’:’rollupProperty’,’allowLinker’: true}); // set allowLinker to true for rollupProperty

ga(‘require’, ‘linker’); // setup autoLink plugin

ga(‘rollupProperty.require’, ‘linker’); // setup autoLink plugin for rollup property

ga(‘linker:autoLink’, [‘abc.com’]); // Auto link to abc website

ga(‘rollupProperty.linker:autoLink’, [‘abc.com’]); // Auto link to abc website

ga(‘send’, ‘pageview’);

ga(‘rollupProperty.send’, ‘pageview’); // send pageview data to rollup property

</script>

To learn more about cross domain tracking, check out the following articles

  1. Google & Universal Analytics Cookies
  2. Google Analytics Cross Domain Tracking
  3. Cross Domain Tracking in Universal Analytics
  4. Cross domain tracking in Google Tag Manager

 Other article you will find useful: Creating your own Google Analytics Tag Auditing System

Announcement about my new books

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books:

error: Content is protected !!