In this article, I will talk about how to use two Google Analytics codes on one page.
In some cases, you might want to send data to multiple Google Analytics properties. This is very useful if your website has multiple owners (business function), and each owner would like to track the same website with different approaches.
This can be done in three different ways based on the method of implementation you have used (Analytics.js OR Global Site Tag OR Google Tag Manager).
To make it more convenient to read, I have divided this article into three sections based on the methods used.
1. Using Google Tag Manager
If you use Google Tag Manager as the implementation method, you can follow the steps below to send data to two separate Google Analytics properties.
Creating Google Analytics variables
To send data to multiple properties, you need to create separate Google Analytics settings variables as below.
Step-1: Log in to your GTM console and click on ‘Variables’.
Step-2: Under ‘User-defined variable’, Click on ‘New’.
As you can see, I already have my one Google Analytics property defined as ‘GA Property 1’. Now let’s create another one.
Step-3: Give a proper name to the new analytics settings variable, let’s say ‘GA Property 2’ and then click on ‘Variable configuration’.
Step-4: Select ‘Google Analytics Settings’ from the list.
Step-5: In the ‘Tracking ID’ input box, paste your analytics tracking ID for the second GA property. Note: You can find the tracking ID in the ‘Admin’ section of Google Analytics under ‘Tracking code’.
Step-6: Now click on ‘Save’.
There are now two different Google Analytics settings variables, and each of them will send data to their separate GA property.
You have to create separate GTM tags for each property. You cannot use a single tag to send data to multiple properties.
If you want to send page views to two different properties, you will have to create two pageview tags. The same applies to events as well. For example, if you want to send an event, e.g. download event, you will need to create two event tags, one for each property.
The process of creating a tag is similar to normal tag creation. You just need to be careful while selecting the property ID. The best way to do this is by setting up tags for one property first and then copying the tag and renaming it after changing the property.
Let’s follow the below steps to copy a tag.
Note: I am assuming that you already have a basic pageview tag for at least one property
Step-1: Go to the ‘Tags’ tab in GTM, identify your first property pageview tag, and click on it.
Step-2: Now, click on the three vertical dots on the right-hand side.
Step-3: A small pop-up will come. Click on ‘Copy’.
The tag will get copied with a name starting with ‘Copy of’ and the original tag name (e.g. Copy of GA Property 1 Pageview).
Step-4: Change this name to distinguish it from the earlier one. For example, I am going to name it GA property 2 Pageview. Once done, click on ‘Tag configuration’ to edit it.
Step-5: Now click on the drop-down under ‘Google Analytics Settings’.
Step-6: You will see a pop up with a list of created properties and variables. Select the second property to which you want to send data.
Step-7: Your tag will look like below. Now click on ‘Save’.
Congratulations! You have successfully configured tags for sending data to two different properties.
Now, you might be wondering, what about triggers and why is copying tags the best way to configure the second property. Well, to answer this, you need to understand using GTM triggers.
In the above process, we have created separate variables for each Google Analytics property and separate tags. But in the case of triggers, they are just conditions for firing the tags and have nothing to do with the Google Analytics property.
This means you can use the same trigger for both tags. Since we are copying the tags, their associated trigger will also get copied. As you can see from the image below, pageview has two separate tags, but the trigger is the same.
Debugging and validation
Now let’s debug the implementation we had carried out to send data to two different analytics properties.
Click on the ‘Preview’ button on the GTM console.
It will open up a new window, enter the URL of your website and click on ‘Start’.
Switch back to the GTM debug window, and you can see that the two pageview tags are firing.
You can also see this in the Google Tag Assistance Chrome extension.
So, that is how you can send data to two different analytics properties on one page.
2. Using global site tag (gtag.js)
If you use the global site tag (gtag.js) for implementation, you can send data to two different analytics properties with some code modifications to the gta.js library.
Introduction to the global site tag
gtag.js is the new version of analytics implementation. gtag.js is not limited to only Google Analytics but all Google products, like Google Ads, Campaign Manager, Display & Video 360, and Search Ads 360.
You can use gtag.js to send event data to any of these products. Google recommends using the global site tag for all the new Google Analytics accounts.
You just need to add the global snippet on your website, which looks like below.
<!-- Global site tag (gtag.js) - Google Analytics -->
Here, gtag.js uses the ‘config’ command to send data to the Google Analytics property. For example, the following line of code creates a GA property instance.
gtag('config', 'UA-XXXXXX-1');
You can send data to multiple GA properties with minimum configuration changes. Let’s see how it is done.
Configuration of an additional GA property
By default, the global site tag (snippet) sends data to a single analytics property. If you want to add a second property, you can achieve this by adding the following command into the main snippet of code.
gtag('config', 'UA-XXXXXX-2');
After adding the command, your code will look like below.
<!-- Global site tag (gtag.js) - Google Analytics -->
Here, gtag(‘config’, ‘UA- XXXXXX -1’); sends data to the first analytics property and gtag(‘config’, ‘UA- XXXXXX -2’); sends data to the second analytics property.
The overall code sends pageview to both analytics properties, and there is no need to send it using separate commands.
Sending data
To send event data, you can use the following command.
This command will send the ‘sign in’ event to both properties we defined in the global snippet. You do not need any special command to distinguish the analytics property and send the event.
If you want to send data to a particular property or group of properties, you need to route it.
Let’s understand more about routing data to groups and properties.
Route data to groups and properties
By default, the global snippet uses the ‘config’ command to handle the routing of events.
If you have configured multiple properties in the global snippet, then event data will be sent to all the properties you have defined, and no additional configuration is required.
In some cases, if you want to send event data to a particular property only, then the global site tag (gtag.js) gives you the ability to send (or route) data to groups of accounts or other Google products.
The above command will send the ‘sign in’ event only to the second property.
If you are sending data to more than two properties and the business requirement is to send certain information to a set of accounts or Google products and send other pieces of information to another set of accounts or Google products, then you can group them using the following command.
This will send data to both analytics properties defined under group1.
Let’s take an example; suppose you have three analytics properties configured on one page to which you want to send analytics data. However, there are some events which you only want to send to two properties and not to the third one. You have also classified two of the properties in ‘group1’
In this case, your global snippet will look like below.
<!-- Global site tag (gtag.js) - Google Analytics -->
If you want to send data to multiple web properties from a single web page,you need to understand what trackers are and how they can be created and used.
A tracking object (also known as a tracker) is used to send hit data (pageviews, screenviews, events, transactions, etc.) to Google Analytics (GA) web property:
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 associates 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 associates the tracking object with the web property whose id is ‘UA-12345-12’:
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 the default tracker by this name. To refer to a default tracker, just don’t use any tracker name.
You need to create two tracking objects if you want to send hit data to two web properties from a single web page:
Similarly, if you want to send website usage data to 3 web properties (3 websites/apps) from a single web page,you need to create and use three trackers.
Use the following similar code to send hit data to two web properties from a single web page:
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 using multiple trackers on a web page, you need to prefix the 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 a default tracker, we use the get method in a ready callback function.
A quick recap of the Callback function
The callback function is simply a function that 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 them:
hit callback
ready callback
Hit callback function
Use the 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:
Use the 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:
You can retrieve the 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:
To retrieve tracking id, use the following code:
ga(function(tracker) {
var trackingId = tracker.get('trackingId');
console.log(trackingId);
});
To see what the default tracking object looks like, use the following code:
ga(function(tracker) {
console.log(tracker);
});
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 a 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. Hence, you need to reference them inside a ready callback function.
Note: you can’t use the get method directly on a user-defined tracker (also known as a named tracker).
For example, if you want to retrieve the name of the user-defined tracker, then the 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 a user-defined tracker’s name and other data, you need to use either the getByName method or the getAll method.
getByName method
If you want to retrieve the name of a 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 a 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 a 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 the name, client id and tracking id of a 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 the developer console to validate it:
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:
But since I use only two trackers, any attempt to retrieve data from the 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:
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'));
});
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 starts with a forward slash ‘/’.
Following is an example of using the set method on the default tracker itself in order to update the default tracker data:
Google Analytics refer to 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:
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'));
});
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 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 to send event data via the default 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 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 to send event data via the named tracker:
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 a roll-up property.
A quick recap of a 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 a marketing agency/consultant, you are most likely to 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 contains multiple reports:
A roll up property is the web property whose hit data comes from other Google Analytics properties.
These 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:
Like any other property in GA, a roll-up property can also contain one or more views.
These views are called roll-up views:
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 subdomains as different websites, then a roll-up property could be useful for you.
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 roll-up property.
Similarly, in the 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 roll-up property.
At the same time, we will also implement cross-domain tracking between abc.com and xyz.com to track users’ activities across domains.
Follow the steps below to implement cross-domain tracking between two primary domains (abc.com and xyz.com) via multiple tracking objects:
Step-1: Create a new web property in your Google Analytics account, which you will use as a roll-up property (if you already don’t have a roll-up property)
This roll-up property will store hit data from both abc.com and xyz.com websites.
Both websites, abc.com and xyz.com, have to share all of their hit data with the roll-up property for this to happen.
If you don’t use roll-up property and simply share abc.com and xyz.com hit data with each other by using multiple trackers, then both websites’ web properties will contain data from two websites and thus make individual analysis of the websites very difficult.
Let us assume that the roll-up property ID is: UA-12345-21
Step-2: Apply filter to your roll-up view, which adds hostname to the 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 to separate the traffic coming from two different websites in your roll-up reports.
Step-3: Share abc.com hit data with roll-up property
To share abc.com hit data with the roll-up 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 the roll-up web property.
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 roll-up property
To share xyz.com hit data with the roll-up 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 the roll-up web property.
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 to maintain one unfiltered view.
# Apply a custom advanced filter to the new view, which adds hostname (domain name) to the request URI.
Frequently asked questions about how to use two Google Analytics codes on one page
Can I use two Google Analytics codes on one page?
If you want to send data to multiple web properties from a single web page, you need to understand what trackers are and how they can be created and used.
You need to create two tracking objects if you want to send hit data to two web properties from a single web page:
Similarly, if you want to send website usage data to 3 web properties (3 websites/apps) from a single web page, you need to create and use three trackers.
What is a tracking object?
A tracking object (also known as a tracker) is used to send hit data (pageviews, screenviews, events, transactions, etc.) to a Google Analytics (GA) web property:
Without first creating and using a tracking object, you can not send hit data to a GA property.
Google Analytics ‘create’ command is used to create a tracking object. This command also associates 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 associates the tracking object with the web property whose id is ‘UA-12345-12’:
There are two types of tracking objects: default and user-defined tracking objects.
Google refers to user-defined trackers as named trackers. So named trackers and user-defined trackers are the same things.
What is a roll-up property?
A roll-up property is a web property whose hit data comes from other Google Analytics properties.
Since each property in Google Analytics represents a website/mobile application, a roll-up property contains data from multiple websites/mobile applications.
Register for the FREE TRAINING...
"How to use Digital Analytics to generate floods of new Sales and Customers without spending years figuring everything out on your own."
Here’s what we’re going to cover in this training…
#1 Why digital analytics is the key to online business success.
#2 The number 1 reason why most marketers are not able to scale their advertising and maximize sales.
#3 Why Google and Facebook ads don’t work for most businesses & how to make them work.
#4 Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.
#5 The number 1 reason why conversion optimization is not working for your business.
#6 How to advertise on any marketing platform for FREE with an unlimited budget.
#7 How to learn and master digital analytics and conversion optimization in record time.
My best selling books on Digital Analytics and Conversion Optimization
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 BeyondSECOND EDITION OUT NOW!
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.
Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.
About the Author
Himanshu Sharma
Founder, OptimizeSmart.com
Over 15 years of experience in digital analytics and marketing
Author of four best-selling books on digital analytics and conversion optimization
Nominated for Digital Analytics Association Awards for Excellence
Runs one of the most popular blogs in the world on digital analytics
Consultant to countless small and big businesses over the decade
Learn and Master Google Analytics 4 (GA4) - 126 pages ebook
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.