javascript 如何使用 NEW analytics.js 跟踪多个帐户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18997966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:00:46  来源:igfitidea点击:

How to track multiple accounts using NEW analytics.js?

javascriptgoogle-analyticsanalytics.js

提问by Frodik

I need to track pageview for two accounts on one page, using Google's new analytics.js. There is plenty of tutorials and examples how to do it with older ga.js. But all I have found was this Analytics documentation page. I have written my code to suit the given example, but it only tracks views for first (default) tracker, but not for the second one.

我需要使用 Google 的新 analytics.js 在一个页面上跟踪两个帐户的综合浏览量。有很多教程和示例如何使用较旧的 ga.js 进行操作。但我发现的只是这个Analytics 文档页面。我已经编写了适合给定示例的代码,但它只跟踪第一个(默认)跟踪器的视图,而不跟踪第二个。

<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');
  ga('create', 'UA-XXXXXXXX-3', 'domain.com');
  ga('create', 'UA-ZZZZZZZZ-1', {'name':'b'});
  ga('send', 'pageview');
  ga('b.send', 'pageview');
</script>

Anyone has any idea what is wrong with my code ? Looks good to me according to Google's example.

任何人都知道我的代码有什么问题吗?根据谷歌的例子,我觉得不错。

采纳答案by tony m

Your code for multiple accounts tracking using analytics.js is correct, I have successfully tested a similar code in my site. So you need to check the following for any possible error:

您使用 analytics.js 进行多个帐户跟踪的代码是正确的,我已在我的网站中成功测试了类似的代码。因此,您需要检查以下任何可能的错误:

1, Confirm if both tracking data are sent. For example in chrome, use GA debugger plugin for chrome and then in javascript console, see if you are getting the below details for both your tracking ids

1, 确认是否发送了两个跟踪数据。例如,在 chrome 中,使用 GA 调试器插件进行 chrome,然后在 javascript 控制台中,查看您是否获得了以下两个跟踪 ID 的详细信息

adSenseId        (&a)   425734287 
apiVersion       (&v)   1 
clientId         (&cid) xx.xx
encoding         (&de)  UTF-8 
flashVersion     (&fl)  11.8
hitType          (&t)   pageview
javaEnabled      (&je)  1 
language         (&ul)  en-us 
location         (&dl)  domain.com 
referrer         (&dr)
screenColors     (&sd)  24-bit
screenResolution (&sr)  1366x768
title            (&dt)  yourdomaintitle 
trackingId       (&tid) UA-XXXXXXXX-3 
viewportSize     (&vp)  1364x361 

Ideally you should see this as your code is correct and this means your website is correctly sending 2 tracking signals.

理想情况下,您应该看到这一点,因为您的代码是正确的,这意味着您的网站正确发送了 2 个跟踪信号。

2, For your second tracking id, ensure the tracking idis exactly same as the one in your GA web property

2, 对于您的第二个跟踪 ID,请确保跟踪 ID与您的 GA网络媒体资源中的完全相同

3, Ensure you have not applied any filtersto the corresponding viewinside your web property which may filter out the traffic . Incase you are using some filters, take an unfiltered view and see if you are seeing hits in the realtime overview

3,确保您没有对您的网络资产内的相应视图应用任何过滤器,这可能会过滤掉流量。如果您正在使用一些过滤器,请查看未过滤的视图,看看您是否在实时概览中看到点击

回答by Dylan Wood

Working with Multiple Tracking Objects

使用多个跟踪对象

To solve this, you must create a tracking object for each web property to which you want to send data:

要解决此问题,您必须为要向其发送数据的每个网络资产创建一个跟踪对象:

ga('create', 'UA-12345-1', 'auto');
ga('create', 'UA-12345-6', 'auto', {'name': 'newTracker'});  // New tracker.

Once run, two tracker objects will be created. The first tracker will be the default tracking object, and not have a name. The second tracker will have the name of newTracker.

运行后,将创建两个跟踪器对象。第一个跟踪器将是默认跟踪对象,并且没有名称。第二个跟踪器的名称为 newTracker。

To send a pageview using both trackers, you prepend the name of the tracker to the beginning of the command, followed by a dot. So for example:

要使用两个跟踪器发送综合浏览量,请在命令的开头加上跟踪器的名称,后跟一个点。例如:

ga('send', 'pageview');
ga('newTracker.send', 'pageview'); // Send page view for new tracker

回答by grantpr

https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers#working_with_multiple_trackers

https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers#working_with_multiple_trackers

This has since been simplified, using a fourth argument - updated by Google on Dec 15, 2015.

此后使用第四个参数进行了简化 - 由 Google 于 2015 年 12 月 15 日更新。

ga('create', 'UA-XXXXX-Y', 'auto');
ga('create', 'UA-XXXXX-Z', 'auto', 'clientTracker');
ga('send', 'pageview');
ga('clientTracker.send', 'pageview');

回答by alexbchr

I know this is an old answer, but since I didn't see anyone mention this solution for pushing to both accounts at once, I thought I would share it...

我知道这是一个旧答案,但由于我没有看到任何人提到这个同时推送到两个帐户的解决方案,我想我会分享它......

Using multiple trackers is the way to go, but if you want to always push in both accounts, override gafunction like this:

使用多个跟踪器是可行的方法,但如果您想始终推送两个帐户,请覆盖如下ga功能:

ga('create', 'UA-XXXXXXXX-1', {
    'name': 'myCustomTracker',
    'cookieDomain': 'auto'
});
ga('create', 'UA-XXXXXXXX-2', 'auto');

ga(function () { //Wait for Analytics to be fully loaded
    var oldGa = ga;
    ga = function () { //Override ga function to call both trackers
        if (arguments && arguments.length > 0) {
            oldGa.apply(null, arguments);
            arguments[0] = "myCustomTracker." + arguments[0]; //Edit first argument to call second tracker.
            oldGa.apply(null, arguments);
        }
    };

    ga('send', 'pageview'); //Perform page view on both trackers at once.
});

Like this you'll be able to call gafunctions like before, pushing data to both trackers at once!

这样你就可以ga像以前一样调用函数,同时将数据推送到两个跟踪器!

回答by HenrikB

According to the example in Analytics documentation page, aren't you supposed to do:

根据Analytics 文档页面中的示例,您不应该这样做:

ga('create', 'UA-XXXXXXXX-3', 'auto');
ga('create', 'UA-ZZZZZZZZ-1', 'auto', {'name':'b'});
// note this last argument            ^^^^^^^^^^^^