Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics Gtag Multiple Analytics Account Tracking IDs

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>
like image 356
Amy Neville Avatar asked Oct 17 '17 11:10

Amy Neville


People also ask

Can you have multiple GTAG?

Yes. You can add multiple accounts, and send to all of them or send individually.

Can I have 2 Google Analytics accounts?

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.

Can a website have multiple Google Analytics accounts?

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.

What is the difference between GTAG and 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.


2 Answers

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:

  • Can I add more than one type of product using gtag('config', ...)? (Answer: yes.) (Docs)
  • Can I add more than one of the same type of product using 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:

  • Notice that the snippet's googleIds array contains five IDs.
  • Notice that, after running the snippet in your browser console, five <script> tags get set to the page, but that the snippet itself only explicitly built and set one of the tags to the .
  • The rest of the tags get added after their respective IDs are pushed into the dataLayer, and after the first script is initialized (i.e. the element is constructed + set to the ). The order of these two steps doesn't matter (i.e. You can initialize first and then push your IDs to the dataLayer, or push your IDs to the dataLayer and then initialize).

// 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;
    }
  }
}
like image 122
Luke Boris Avatar answered Sep 18 '22 06:09

Luke Boris


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.

like image 44
brianfit Avatar answered Sep 18 '22 06:09

brianfit