简体   繁体   English

在google analytics.js中发送事件

[英]ga send event in google analytics.js

I'm using Google analytics.js (so not the 'old' ga.js) and in some links I have for example: 我正在使用Google analytics.js(所以不是'旧'ga.js),而是在某些链接中我有例如:

<a href="www.example.com" onclick="ga('send', 'event', 'outbound link', 'position: header');">Link</a>

The onclick code is dynamically generated by php depending on what page you are. onclick代码由php动态生成,具体取决于您的页面。 Now I can't test it yet since my account hasn't been converted into the new analytics.js, but I was wondering what I should put in the code in the <head> section of my page. 现在我无法测试它,因为我的帐户尚未转换为新的analytics.js,但我想知道我应该在我的页面的<head>部分的代码中添加什么。

The docs states I should put 我应该提出的文件说明

(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');  // Creates a tracker.
ga('send', 'pageview');             // Sends a pageview.

But do I need to add ga('send', 'event') below that? 但是我需要在下面添加ga('send', 'event')吗? Isn't really clear from the docs... 从文档中不是很清楚......

You don't need to place the ga('send', 'event') inside the <head> code. 您无需在<head>代码中放置ga('send', 'event') You can use ga send event code in onclick event for any part of the webpage. 您可以在onclick事件中为网页的任何部分使用ga发送事件代码。 Try this method to execute in anyplace you want to track such as a button,link,tabs,etc. 尝试使用此方法在要跟踪的任何位置执行,例如按钮,链接,选项卡等。

According to your statement (your question is not clear) , you are using universal analytics. 根据您的陈述(您的问题尚不清楚),您正在使用通用分析。 Hence the new tracking code is different from the previous one and the code format you have pasted hear is also correct. 因此,新的跟踪代码与前一代码不同,您粘贴的代码格式也是正确的。

According to the GA help files we need to past in between <head> </head> tags. 根据GA帮助文件,我们需要在<head> </head>标签之间过去。 Once you done the needful you can execute the event tracking from any part form the webpage. 完成需要后,您可以从网页的任何部分执行事件跟踪。

eskimo , the code that goes in the head is important as it creates the tracker object and so you can use all tracking functions once the page is loaded (therefore the create command, followed immediately by pageview request). eskimo ,头部中的代码很重要,因为它创建了跟踪器对象,因此您可以在加载页面后使用所有跟踪功能(因此create命令,紧接着是pageview请求)。

So no need to put anything that is related to sending events -- they should be used only with elements you want to track. 因此,无需添加任何与发送事件相关的内容 - 它们只应与您要跟踪的元素一起使用。 I suggest using jQuery and adding event listener to make sure the event tracking is consistent. 我建议使用jQuery并添加事件监听器以确保事件跟踪是一致的。

Hope this helps :-) 希望这可以帮助 :-)

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

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