21

我需要使用 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>

有人知道我的代码有什么问题吗?根据谷歌的例子,我觉得很好。

4

5 回答 5

27

使用多个跟踪对象

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

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

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

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

ga('send', 'pageview');
ga('newTracker.send', 'pageview'); // Send page view for new tracker
于 2015-05-01T22:21:56.113 回答
21

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:

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

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, For your second tracking id, ensure the tracking id is exactly same as the one in your GA web property

3, Ensure you have not applied any filters to the corresponding view inside 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

于 2013-09-27T12:24:13.773 回答
9

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

这已被简化,使用第四个参数 - 由 Google 于 2015 年 12 月 15 日更新。

ga('create', 'UA-XXXXX-Y', 'auto');
ga('create', 'UA-XXXXX-Z', 'auto', 'clientTracker');
ga('send', 'pageview');
ga('clientTracker.send', 'pageview');
于 2016-04-26T12:00:42.337 回答
1

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

ga('create', 'UA-XXXXXXXX-3', 'auto');
ga('create', 'UA-ZZZZZZZZ-1', 'auto', {'name':'b'});
// note this last argument            ^^^^^^^^^^^^
于 2014-06-28T01:07:06.550 回答
1

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

使用多个跟踪器是可行的方法,但如果您想始终推送两个帐户,请覆盖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.
});

像这样,您将能够ga像以前一样调用函数,一次将数据推送到两个跟踪器!

于 2016-08-23T18:50:29.867 回答