简体   繁体   English

在同一个网站上使用多个Google Analytics(分析)帐户

[英]Using multiple Google Analytics accounts in the same website

My team is working on a set of tools and pages that require google analytics for us to analyze. 我的团队正在研究一组需要Google Analytics(分析)才能供我们分析的工具和页面。 We have an Analytics account for our team where we gather our data that we've used for a good while now. 我们有一个Google Analytics(分析)帐户供我们的团队使用,该帐户收集了我们已经使用了很长时间的数据。

Recently we've had to combine our website with another repository, under another team's website. 最近,我们不得不将我们的网站与另一个团队的网站下的另一个存储库合并。 Because of this, our analytics has ceased to send data to our analytics account. 因此,我们的分析已停止将数据发送到我们的分析帐户。 The data is instead sent to the other team's account. 而是将数据发送到另一个团队的帐户。 The framework their using injects certain code into all pages on the website. 他们使用的框架将某些代码注入网站上的所有页面。

We would like to instead send our event data to our account, and figure out how to properly separate our analytics object from theirs. 相反,我们想将事件数据发送到我们的帐户,并弄清楚如何正确地将我们的分析对象与它们的对象分开。 The code below is an example of what we would like to change in order to allow communication to 2 different accounts. 下面的代码是我们想要更改的示例,以便允许与2个不同的帐户进行通信。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

 ga('create', 'UA-XXXXXXXX-2', 'auto');

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-YYYYYYYY-2', {'name':'b'});


ga('send', 'event', 'TheirAnalytics', 'TheirEvent');
ga('send', 'event', 'MyAnalytics', 'MyEvent');

The two existing answers on this page are not correct and will actually not work. 此页面上现有的两个答案不正确,实际上将不起作用。 You definitely do not need to download the analytics.js script twice, and you definitely do not want to create two command queue functions. 你绝对不需要下载analytics.js脚本两次,你肯定希望创建两个命令队列功能。

What you do want to do is create two tracker objects , give at least one of them a name, and then send data using both. 您要做的是创建两个跟踪器对象 ,至少给它们一个名称,然后使用这两个对象发送数据。

Here's an example: 这是一个例子:

<script>

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

// Creates the first tracker for one of your accounts,
// and uses the default name.
ga('create', 'UA-XXXXX-1', 'auto');

// Creates the second tracker for the other account,
// and give it the name "secondary"
ga('create', 'UA-XXXXX-2', 'auto', 'secondary');

// Sends events from both trackers,
// i.e. one event hit will be sent to each account.
ga('send', 'event', 'MyAnalytics', 'MyEvent');
ga('secondary.send', 'event', 'TheirAnalytics', 'TheirEvent');

</script>

Take a look at the documentation on working with multiple trackers and using named trackers for more details. 查看有关使用多个跟踪器以及使用命名跟踪器的文档, 了解更多详细信息。

change the last (window,document,'script','//www.google-analytics.com/analytics.js','ga'); 更改最后一个(window,document,'script','//www.google-analytics.com/analytics.js','ga'); to (window,document,'script','//www.google-analytics.com/analytics.js','ga2'); (window,document,'script','//www.google-analytics.com/analytics.js','ga2');

and use ga2('send', 'event', 'MyAnalytics', 'MyEvent'); 并使用ga2('send', 'event', 'MyAnalytics', 'MyEvent');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM