Google Analytics Gtag 多个分析帐户跟踪 ID [英] Google Analytics Gtag Multiple Analytics Account Tracking IDs

查看:49
本文介绍了Google Analytics Gtag 多个分析帐户跟踪 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Google 现在似乎正在逐步淘汰 analytics.js,转而使用他们的标签管理器.

From what I can see Google seem to be phasing out analytics.js now in favor of their tag manager.

如何为多个分析帐户触发谷歌分析新的 gtag 跟踪代码?

How do I fire google analytics new gtag tracking code for multiple analytics accounts?

像这样:

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

推荐答案

简而言之:

,您可以通过调用gtag('config', ...) 对于您拥有的每个 Google 帐户 + 媒体资源 ID.

Yes, you can add the same type of product multiple times by calling gtag('config', ...) for each respective Google account + property ID you have.

详情:

现在是 2021 年,我有同样的问题,但我太偏执了,无法相信这个帖子的最高投票答案,因为它在测试其工作原理时描述了与我自己不同的体验.不过,首先,为了在 2021 年回答 OP 的问题,我们必须查看 Google 官方文档中的两个条目,因为这两个条目都没有完全回答问题,但是将它们放在一起后可以让我们更有信心如何解决这个问题:

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', ...)添加多个类型的产品吗?(答案:是.)(文档)
  • 我可以使用 gtag('config', ...)添加多个相同类型的产品吗??(答案:是.)(文档)
  • 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)

这是我如何使用 JavaScript 完成 OP 场景的示例片段.如果您在浏览器的控制台中尝试此操作,您应该会看到为您在以下代码段的 googleIds 数组中设置的每个 ID 添加了一个唯一的脚本.

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.

注意事项:

  • 请注意,代码段的 googleIds 数组包含五个 ID.
  • 请注意,在浏览器控制台中运行代码段后,五个 <script> 标签被设置为页面,但代码段本身仅显式构建并将其中一个标签设置为.
  • 其余标签在它们各自的 ID 被推送到数据层之后,以及在第一个脚本初始化之后(即元素被构造 + 设置为 )被添加.这两个步骤的顺序无关紧要(即您可以先初始化然后将您的 ID 推送到数据层,或者将您的 ID 推送到数据层然后再初始化).
  • 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;
    }
  }
}

这篇关于Google Analytics Gtag 多个分析帐户跟踪 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆