简体   繁体   English

在外部JS文件中访问Google Analytics Universal Analytics

[英]Access Google Analytics Universal Analytics in external JS files

In the old version of Google Analytics you could just add var _gaq = _gaq || []; 在旧版Google Analytics中,您只需添加var _gaq = _gaq || []; var _gaq = _gaq || []; on the top of your javascript files, which would let you push events and transactions before GA had fully loaded. 在您的javascript文件的顶部,这将允许您在GA完全加载之前推送事件和事务。

With Universal Analytics you no longer use .push() , so what is the proper way to create the ga object in external files where Google Analytics might not have loaded yet but you need to push events and transactions? 使用Universal Analytics,您不再使用.push() ,那么在外部文件中创建ga对象的正确方法是什么?Google Analytics可能尚未加载,但您需要推送事件和交易?

The Immediately-Invoked Function Expression in the Google Analytics snippet handles the creation of that object. Google Analytics摘要中的立即调用函数表达式处理该对象的创建。 In the snippet you see the following: 在代码段中,您会看到以下内容:

i[r] = i[r] || function() {
    (i[r].q = i[r].q || []).push(arguments)
}

We know from the parameters passed to the IIFE that i = window and r = "ga": 我们从传递给IIFE的参数知道i = window和r =“ga”:

(function(i, s, o, g, r, a, m) {
    //...
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');

In un-uglyfied JavaScript, the snippet reads as such: 在un-uglyfied JavaScript中,代码片段如下所示:

window['ga'] = window['ga'] || function() {
    ( window['ga'].q =  window['ga'].q || []).push(arguments)
}

So by calling the global function ga , you are essentially creating an array that serves as the queue (if it doesn't already exist) and pushing values to the queue. 因此,通过调用全局函数ga ,您实际上创建了一个充当队列的数组(如果它尚不存在)并将值推送到队列中。

In Universal Analytics, calling this function: 在Universal Analytics中,调用此函数:

ga('create', 'UA-XXXX-Y', 'auto');

Is the same as this in the previous version of GA: 与之前版本的GA中的相同:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXX-Y']);

More info can be found in the Google dev docs . 更多信息可以在Google开发文档中找到

You don't need to redefine the ga function since it already is defined in the tracking snippet. 您无需重新定义ga函数,因为它已在跟踪代码段中定义。 All you have to do is use the ga object in your external file and you are good to go. 你所要做的就是在外部文件中使用ga对象,你就可以了。 ga already is a global object, so no need to scope it. ga已经是一个全局对象,所以不需要范围。

You also don't want to be pushing anything into ga object before you at least run the line that creates the tracker: 在至少运行创建跟踪器的行之前,您也不希望将任何内容推入ga对象:

ga('create', 'UA-XXXX-Y', 'auto');

If you d chances are the hit won't get through and won't reach your account. 如果您有机会点击将无法通过,将无法访问您的帐户。

Make sure that whatever external file you use you include it after the tracking snippet in your site, or maybe include the tracking snippet in the external file at the top. 确保您使用的任何外部文件都包含在您网站中的跟踪代码段之后,或者可能在顶部的外部文件中包含跟踪代码段。

Here is what I used in my external js file and it seems to be working and tracking nicely. 这是我在外部js文件中使用的,它似乎正在工作和跟踪很好。 Note to track the events stuff you can use the real time events tab in your GA account to confirm that its working. 请注意,要跟踪事件内容,您可以使用GA帐户中的实时事件标签确认其有效。

I am also tracking outbound events here. 我也在这里跟踪出站事件。 Note: I spent 2 days getting this out bound events tracking to work. 注意:我花了2天的时间来完成此绑定事件跟踪工作。 Make sure in your onclick event that you are using '' these single quotes. 在你的onclick事件中确保你使用''这些单引号。 I copied the code off this page https://support.google.com/analytics/answer/1136920?hl=en and the quotes on there are actually a different character that break the script, so type the quotes in from your keyboard. 我在此页面上复制了代码https://support.google.com/analytics/answer/1136920?hl=zh-CN并且实际上有一个不同的字符会破坏脚本,因此请在键盘中输入引号。

I wanted my GA code in an external file because I had all old classic code and now that Google has moved to Universal, I had to go to each page to make the change to the google spinet. 我想把我的GA代码放在一个外部文件中,因为我有所有旧的经典代码,现在Google已经转移到了Universal,我不得不去每个页面来改变google spinet。 Just smarter to centralize it I think, incase anymore changes come in the future. 我认为,更聪明地集中它,将来会有更改。

In my head tag 在我的头标签

<script type="text/javascript"  src="/web_resources/themes/OldsCollege/js/scripts.js" async></script>

in my js file; 在我的js文件中;

/*Analytics*/


    (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-616432-1', 'auto');
  ga('send', 'pageview');


var trackOutboundLink = function(url, action) {
   ga('send', 'event', 'outbound-26th', action, url, {'hitCallback':
     function () {
     document.location = url;
     }
   });
}

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

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