How to use two Google Analytics codes on one page

In this article, I will talk about how to use two Google Analytics 4 codes on one page.

In some cases, you might want to send data to multiple Google Analytics 4 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 two different ways based on the method of implementation you have used (Global Site Tag OR Google Tag Manager).

To make it more convenient to read, I have divided this article into two 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 constant variables as below.

Step-1: Log in to your GTM console and click on ‘Variables’.

Log in to your GTM console and click on ‘Variables

Step-2: Under ‘User-defined variable’, Click on ‘New’.

Under ‘User defined variable Click on ‘New

As you can see, I already have my one Google Analytics property defined as ‘GA4 Property 1’. Now let’s create another one.

Step-3: Give a proper name to the new analytics settings variable, let’s say ‘GA4 Property 2’ and then click on ‘Variable configuration’.

lets say ‘GA4 Property 2 and then click on ‘Variable configuration

Step-4: Select ‘Constant’ from the list.

Select ‘Constant from the list

Note: In Step 4, we are not selecting the ‘Google Analytics Settings’ variable as this was specially designed only for Universal Analytics and not Google Analytics 4.

Step-5: Paste your measurement ID for the second GA4 property in the’ Value‘ input box.

Paste your analytics tracking ID for the second GA property

Note: You can find the measurement ID in the ‘Admin’ section of Google Analytics 4 under ‘Data Streams’.

Step-6: Now click on ‘Save’.

Now click on ‘Save

There are now two different Google Analytics constant variables, and each of them will send data to their separate GA4 property.

Now, let’s create the tags.

Do you want expert help in setting up/fixing GA4 and GTM?

If you are not sure whether your GA4 property is setup correctly or you want expert help migrating to GA4 then contact us. We can fix your website tracking issues.

Creating separate Google Analytics tags

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.

create two event tags one for each property
ga4 property 1 configuration
ga4 property 2 configuration

The process of creating a tag is similar to normal tag creation. You just need to be careful while selecting the measurement 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.

Go to the ‘Tags tab in GTM identify your first property pageview tag

Step-2: Now, click on the three vertical dots on the right-hand side.

click on the three vertical dots on the right hand side

Step-3: A small pop-up will come. Click on ‘Copy’.

Click on ‘Copy

The tag will get copied with a name starting with ‘Copy of’ and the original tag name (e.g. Copy of GA4 Property 1 Configuration).

Copy of GA4 Property 1 Configuration

Step-4: Change this name to distinguish it from the earlier one.

For example, I am going to name it ‘GA4 property 2 Configuration’.

GA4 property 2 Configuration 2

Once done, click on ‘Tag configuration’ to edit it.

Step-5: Now click on the drop-down under ‘Measurement ID’. And delete the current property variable selected.

click on the drop down under ‘Measurement ID

Step-6:  Now click on the on the castle icon with “+” sign on it and select the second property variable that you created.

select the second property variable

Step-7: Your tag will look like below. Now click on ‘Save’.

Now click on ‘Save 2

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.

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. 

same triggers

Debugging and validation

Now let’s debug the implementation we carried out to send data to two different analytics properties.

Click on the ‘Preview’ button on the GTM console.

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’.

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.

Switch back to the GTM debug window

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 latest 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 the one below.

add the global snippet on your website

<!– Google tag (gtag.js) –>

<script async src=”https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX”></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag(‘js’, new Date());

  gtag(‘config’, ‘G-XXXXXXXX’);

</script>

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’, ‘G-XXXXXXXX’);

You can send data to multiple GA properties with minimum configuration changes.

Let’s see how it is done.

Configuration of an additional GA4 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 Google Analytics command to the main snippet of code.

gtag(‘config’, ‘G-YYYYYYYY’);

After adding the command, your code will look like below.

<!– Google tag (gtag.js) –>

<script async src=”https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX”></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag(‘js’, new Date());

  gtag(‘config’, ‘G-XXXXXXXX’);

  gtag(‘config’, ‘G-YYYYYYYY’);

</script>

Here,  gtag(‘config’, ‘G-XXXXXXXX’); sends data to the first analytics property and gtag(‘config’, ‘G-YYYYYYYY’); 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.

gtag(‘event’, ‘sign in’, {‘method’: ‘Using Password’});

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.

This is done by using the following command.

gtag(‘event’, ‘sign_in’, { ‘send_to’: ‘G-YYYYYYYY’ });

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.

gtag(‘config’, ‘G-XXXXXXXX’, { ‘groups’: ‘group1’ });

gtag(‘config’, ‘G-YYYYYYYY’, { ‘groups’: ‘group1’ });

gtag(‘event’, ‘sign_in’, { ‘send_to’: ‘group1’ });

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 –>

<script async src=‘https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID’></script>

<script>

  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag(‘js’, new Date());

  gtag(‘config’, ‘G-XXXXXXXX’, { ‘groups’: ‘group1’ });

gtag(‘config’, ‘G-YYYYYYYY’, { ‘groups’: ‘group1’ });

gtag(‘config’, ‘G-ZZZZZZZ’);

</script>

Now, if you want to send analytics data to all three properties, you can use the following command:

gtag(‘event’, ‘sign in’, {‘method’: ‘Using Password’});

If you want to send event data to only two properties classified in ‘group1’

You will use the following command.

gtag(‘event’, ‘sign_in’, { ‘send_to’: ‘group1’ });

This will send data to only ‘G-XXXXXXXX’ and ‘G-YYYYYYYY’. It won’t send any data to the third property, which is G-ZZZZZZZ

So this is how you can use the global site tag (gtag.js) to send data to two Google Analytics codes on one page.

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

  1. How to see Organic Search Keywords in GA4 (Google Analytics 4)
  2. Google Analytics Ecommerce Tracking Tutorial
  3. Google Tag Manager Event Tracking Tutorial
  4. Google Analytics Event Tracking Tutorial
  5. Google Analytics Store Visits Tracking Tutorial
  6. Offline Conversion Tracking in Google Analytics – Tutorial
  7. Ecommerce Tracking Google Tag Manager (GTM) – Tutorial
  8. Tracking Virtual Pageviews in Google Tag Manager – Tutorial
  9. Google Tag Manager YouTube Video Tracking
  10. Google Analytics Virtual Pageviews Tutorial
  11. Google Analytics YouTube Integration & Analysis Tutorial
  12. Google Analytics for Facebook Tutorial
  13. Cross Domain Tracking in Google Analytics – Complete Guide
  14. How to use two Google Analytics codes on one page
  15. How to correctly use referral exclusion list in Google Analytics
  16. Google Analytics Calculated Metrics – Tutorial
  17. Creating your own Google Analytics Tag Auditing System
  18. Google Tag Manager Search Tracking without Query Parameter
  19. Tracking Google Analytics Paypal Referral and other payment gateways
  20. How to Track Phone Calls in Google Analytics 4 – Call Tracking Tutorial
  21. How to track leads in Google Analytics via CRM
  22. Postbacks in Google Analytics Explained
  23. Subscription & Recurring Revenue Analytics in Google Analytics
  24. Track the Impact of Google Analytics Cookie Consent on Website Traffic
  25. Tracking Offline Conversions in Google Ads
  26. Implementing Scroll Tracking via Google Tag Manager
  27. Scroll Depth Tracking in Google Tag Manager – Tutorial
  28. Site Search Tracking In Google Analytics Without Query Parameters
  29. Google Tag Manager Youtube Video Tracking via YouTube Video Trigger
  30. How to Correctly Measure Conversion Date & Time in Google Analytics
  31. Google Analytics Social Tracking – Twitter, Facebook, Google Plus and LinkedIn
  32. Cross Domain Tracking in Google Analytics – Complete Guide
  33. Google Analytics Linkedin & Twitter Tracking
  34. Creating Content Group in Google Analytics via tracking code using gtag.js
  35. Google Analytics Site Search Tracking via Query Parameters
  36. Google Analytics Site Search Tracking Tutorial
  37. Creating and Using Site Search Funnel in Google Analytics
  38. How to add Facebook Pixel to Google Tag Manager
  39. AMP Google Analytics Tracking – Learn to track AMP pages
  40. Setting up Sales Funnel across websites in Google Analytics
  41. Google Analytics 4 Regex (Regular Expressions) Tutorial

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 ‘G-XXXXXXXX’:

gtag(‘config’, ‘G-XXXXXXXX’);

What are the different types of tracking objects?

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.

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