From what I can see Google seem to be phasing out analytics.js now in favor of their tag manager.
How do I fire google analytics new gtag tracking code for multiple analytics accounts?
Something like this:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-108285779-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-108285779-2');
gtag('config', 'ANOTHER CODE');
gtag('config', 'ANOTHER CODE');
</script>
Yes. You can add multiple accounts, and send to all of them or send individually.
You might also set up different Analytics accounts for different groups or stakeholders. For example, if you administer Analytics tracking for two companies, you would set up a separate Analytics account for the websites owned by each company.
If you have multiple Google Analytics accounts where you track different websites separately, you will need to decide which of them will be a master account and secondary accounts. Then, from within each of your secondary accounts, you'll need to give the master account access to their properties in Google Analytics.
Google Analytics is the tracking tool, while Google Tag Manager is the mediator between your website and the tracking tool. In other words, Google Analytics collects, stores, and analyzes data.
In short:
Yes, you can add the same type of product multiple times by calling
gtag('config', ...)
for each respective Google account + property ID you have.
Details:
It's 2021 and I had the same question but was too paranoid to trust this thread's top voted answer because it described a different experience than my own when testing how this works. Firstly, though, in order to answer OP's question in 2021 we have to look at two entries in Google's official docs, because neither entry fully answers the question but when brought together they can give us a bit more confidence in how to solve this:
gtag('config', ...)
? (Answer: yes.) (Docs)gtag('config', ...)
? (Answer: yes.) (Docs)Here's an example snippet of how I accomplished OP's scenario using JavaScript. If you try this in your browser's console you should see a unique script get added for each ID you set in the below snippet's googleIds
array.
Notes:
googleIds
array contains five IDs.<script>
tags get set to the page, but that the snippet itself only explicitly built and set one of the tags to the .// An array of IDs I want to load on the same page(s) at the same time
var googleIds = ["AW-00000000", "AW-00000001", "AW-00000002", "DC-00000000", "UA-00000000-1"];
// Setting dataLayer & gtag to window because I'm using a custom code text field in a tag management system
window.dataLayer = window.dataLayer || [];
window.gtag =
window.gtag ||
function() {
window.dataLayer.push(arguments);
};
window.gtag("js", new Date());
// Flag used to ensure script only set with first ID, and rest of IDs are pushed to dataLayer
var gtagScriptExists = false;
// ID validation regex. Only tested with AW-*, but DC & UA are also valid prefixes
var validIdStructure = new RegExp(/(AW|DC|UA)-[0-9]{8,}(-[0-9]{1,})?/);
// Push IDs into dataLayer and set initial gtag/js?id= script to page using first ID in googleIds array
for (var i = 0; i < googleIds.length; i++) {
var gtagId = googleIds[i];
// Validate that the ID being passed isn't a big weirdo
var idIsValid =
typeof gtagId === "string" && gtagId.match(validIdStructure);
if (idIsValid) {
window.gtag("config", gtagId);
// NOTE: gtag script only needs to be set to page once, but each gtag('config', <ID>) that's pushed to the dataLayer will add subsequent gtag/js?id=<ID> scripts to the page
if (!gtagScriptExists) {
// Set initial gtag/js?id=<first ID> script to <head>
var script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = "//www.googletagmanager.com/gtag/js?id=" + gtagId;
document.getElementsByTagName("head")[0].appendChild(script);
// Update gtag/js?id= script status flag so this initialization script is only set for the first ID, and not all the IDs in the array
gtagScriptExists = true;
}
}
}
Yes, that is correct according to documentation. But it generated no data for me on the subsequent codes until I added
<script async src="https://www.googletagmanager.com/gtag/js?id=ANOTHER_CODE"></script>
Immediately above the code block. Either I stumbled on a working kludge or Google needs to update their documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With