简体   繁体   English

使用javascript进行Google Analytics(分析)事件跟踪

[英]Google Analytics Event Tracking using javascript

Is there an alternative to using the google analytics onclick event? 除了使用Google Analytics(分析)onclick事件之外,还有其他选择吗?

 onclick="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value]);"> 

I have a website that has a large amount of links that I want to track. 我有一个网站,其中包含大量要跟踪的链接。 However, I don't want to have to go and add this to every link I have. 但是,我不想去将它添加到我拥有的每个链接中。 I have done some research but cant seem to find something that work better. 我已经做了一些研究,但似乎找不到更好的方法。 Any suggestions? 有什么建议么?

You could get all anchors and attach an onclick event that will call the tracker function. 您可以获取所有锚点并附加一个onclick事件,该事件将调用跟踪器功能。 Something like this: 像这样:

  var links = document.getElementsByTagName('a');
  for (var i = 0; i < links.length; ++i) {
    links[i].addEventListener('click', fireAnalyticsEvent, false);
  }
  function fireAnalyticsEvent(ev) {
   _gaq.push(['_trackEvent', 'category', 'action', 'opt_label', ev.srcElement.getAttribute('href')]);
  }

However, bear in mind that as far as outgoing links that are opened at the same tab are concerned, GA is not reliable: the request to analytics might be cut off when the page navigates to the new URL. 但是,请记住,就在同一标签上打开的传出链接而言,GA不可靠:当页面导航到新URL时,对分析的请求可能会被切断。 If this is what you are trying to track then you would have to use a server-side redirect system to track the clicks (something like a python/php/your fave server language that will get the URL as a parameter, perform the tracking logic, and then return 302 redirect headers to have the client navigate to the URL that it got as input). 如果这是您要跟踪的内容,则必须使用服务器端重定向系统来跟踪点击次数(类似于python / php / fave服务器语言,它将获取URL作为参数,请执行跟踪逻辑,然后返回302重定向标头,以使客户端导航到作为输入获得的URL)。

You could alternativley solve your problem by making a php "kicker/redirect"-page that will take a url parameter and if you add standard GA to that page, it will track the parameter right there.. You can ofcourse use javascript and get the query parameter and track as "event" if you want. 您可以通过制作一个php“ kicker / redirect”页面来采用网址参数来解决您的问题,如果您向该页面添加标准GA,它将在此处跟踪该参数。您当然可以使用javascript并获取查询参数并根据需要将其跟踪为“事件”。

Edit: So you'd need to rewrite your links, OR use jquery to manipulate the url at runtime: http://techpad.co.uk/content.php?sid=103 编辑:因此,您需要重写链接,或者使用jquery在运行时操纵URL: http : //techpad.co.uk/content.php?sid=103

-- -

I think it would be cleaner than.. (abstract) somehow catching all click events, then filter if the links is in a certain element, or links to certain destination.. doable, but too messy in my opinion. 我认为这会比..(抽象)更干净。以某种方式捕获所有单击事件,然后过滤链接是否在某个元素中,或链接到某个目标..可行,但在我看来太混乱了。

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

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