简体   繁体   English

Google Analytics(分析)设定

[英]Google Analytics analytics.js setup

I'm new to Google Analytics and as such just want to make sure I am putting everything in the right places and haven't broken any obvious rules! 我是Google Analytics(分析)的新手,因此只想确保我将所有内容放在正确的位置,并且没有违反任何明显的规则!

I have the analytics code below within the <head> tags: 我在<head>标记内包含以下分析代码:

<!-- Google 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-XXXX-Y', 'auto');
ga('send', 'pageview');

</script>
<!-- End Google Analytics -->

If I was then to use the following code, could I place it in a separate .js file so I don't have to paste it into every page I need tracking on? 如果当时要使用以下代码,是否可以将其放在单独的.js文件中,这样就不必将其粘贴到需要跟踪的每个页面中了吗? And then just link to the .js file of course. 然后只需链接到.js文件即可。

var phoneNo = document.getElementById('headerPhone');
addListener(phoneNo, 'click', function() {
  ga('send', 'event', 'phonenumber', 'click', 'header');
});
/**
 * Utility to wrap the different behaviors between W3C-compliant browsers
 * and IE when adding event handlers.
 *
 * @param {Object} element Object on which to attach the event listener.
 * @param {string} type A string representing the event type to listen for
 *     (e.g. load, click, etc.).
 * @param {function()} callback The function that receives the notification.
 */
function addListener(element, type, callback) {
 if (element.addEventListener) element.addEventListener(type, callback);
 else if (element.attachEvent) element.attachEvent('on' + type, callback);
}

Also, could I omit the text in the multiline comment? 另外,我可以省略多行注释中的文本吗?

Then I would just need to put the id on the element, such as: 然后,我只需要将id放在元素上,例如:

<h3 id="headerPhone" onclick="goog_report_conversion('tel:01000 000000')">01000 000000</h3>

The onClick event on the above is for AdWords tracking so ignore that. 上面的onClick事件用于AdWords跟踪,因此请忽略该事件。

To clarify the questions that apparently aren't clear enough: 为了澄清似乎还不够清楚的问题:

1) Am I correct in placing the second bit of code in a separate .js file and linking it to all pages? 1)我将第二部分代码放在单独的.js文件中并将其链接到所有页面是否正确?

2) Can I get rid of the multiline comment from the second bit of code? 2)我可以摆脱第二行代码中的多行注释吗?

3) Is the third bit of code correct, in conjunction with the second bit of code, to execute the tracking?(ie when the <h3 id="headerPhone"> is clicked will it cause the second bit of code to execute?). 3)代码的第三位与代码的第二位一起执行是否正确?(即,单击<h3 id="headerPhone"> ,会导致代码的第二位执行吗?) 。

4) Have I made any errors in the code itself? 4)我是否在代码本身中犯了任何错误?

  1. It's up to you. 由你决定。 You can either set it in external JS file or in the page body/head. 您可以在外部JS文件中或页面正文/头部中进行设置。 As long as definitions will be downloaded before actual use you're safe. 只要在实际使用之前下载定义,您就可以安全。

  2. Naturally, it's a comment. 自然,这是一条评论。 No need to have it in production. 无需在生产中使用它。

  3. Yes. 是。 GA event seems to be correct. GA事件似乎是正确的。 As well as calling the function. 以及调用函数。

  4. No. 没有。

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

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