Data tracking in multiple Google Analytics properties | analytics.js VS gtag.js
By: Awanish Kumar
Tags: Google Analytics, Google Analytics Tips

Many websites use two or more Google analytics ID to track data. One is brand-specific and the other is the Global property where data for multiple sites is tracked under one GA-ID (Rollup).



In Analytics.JS if you want to track data in multiple ids then there is required to create multiple trackers.

What is a tracker?

Tracker object is used to send data to google analytics property.

Tracking object in Analytics.JS



You can’t send data to a GA property without creating a tracking object. In Analytics.js ‘create’ command is used to create a tracking object.

For example, the below lines of code create a tracking object.

ga(‘create’,’UA-XXXXX’,auto);

This line will create the default tracker name “t0”. You can check the tracker name by typing the below script in the console.

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

For multiple GA ID: Below line of code is used to create a tracker name for an additional GA ID.

ga(‘create’, ‘UA-12345–1’, ‘auto’);

ga(‘create’, ‘UA-12345–6’, ‘auto’, {‘name’: ‘newTracker’});

When you are using multiple tracker names on the page then you would need to add prefixes in every command like below.

ga(‘create’, ‘UA-12345–1’, ‘auto’);

ga(‘create’, ‘UA-12345–6’, ‘auto’, {‘name’: ‘newTracker’});

ga(‘require’,’linker’);

ga(‘newTracker.require’,’linker’);

ga(‘send’, ‘pageview’);

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

ga(‘send’, ‘event’, ‘event category’,’event action’,’eventLable’);

ga(‘newTracker.send’, ‘event’, ‘event category’,’event action’,’eventLable’);

If you use one tracker object name and send data to Multiple Google analytics ID then it will impact overall data tracking like inflated metrics and bounce rate.

The below line of code would not work.

ga(‘create’, ‘UA-12345–1’, ‘auto’);

ga(‘create’, ‘UA-12345–2’, ‘auto’);

ga(‘send’, ‘pageview’);

Multiple tracking in analytics.js is quite a heavy task because every GA configuration is written multiple times in code.

Multiple ID tracking in GTAG.JS

In gtag.js, there is no need to create tracking Object name. It gets created by default on the basis of GA-ID .

gtag(‘config’, ‘UA-12345–2’);

gtag(‘config’, ‘UA-12345–6’);

If you check tracker name object in console using below line of script, then it will show like below:

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

“gtag_UA_12345_2”

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

“gtag_UA_12345_6”

In GTAG.js you can route data to Group of property IDs.

Default Route

gtag(‘config’, ‘GA_MEASUREMENT_ID-1’, { ‘groups’: ‘group1’ });

gtag(‘config’, ‘GA_MEASUREMENT_ID-2’, { ‘groups’: ‘group1’ });

// Routes to ‘GA_MEASUREMENT_ID-1’ and ‘GA_MEASUREMENT_ID-2’

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

Route to Custom groups

// Configure a target

gtag(‘config’, ‘GA_MEASUREMENT_ID-1’);

gtag(‘config’, ‘GA_MEASUREMENT_ID-3’, { ‘groups’: ‘group1’ });

gtag(‘config’, ‘GA_MEASUREMENT_ID-9’, { ‘groups’: ‘group1’ });

// Routes only to ‘GA_MEASUREMENT_ID-3’ and ‘GA_MEASUREMENT_ID-9’ since they are both in the ‘group1’ group

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

In the above case, Sign_in event will only be sent to property ‘GA_MEASUREMENT_ID-3’and ‘GA_MEASUREMENT_ID-9’.