Google Analytics with jQuery - show pathname in label

不问归期 提交于 2019-12-24 02:02:37

问题


I've got as far as creating the tracking and it works great like so:

  $('a.external').click(function(){
    _gaq.push(['_trackEvent', 'Exit Links', 'Click', $(this).attr('href')]);
  });

<a href="http://example.com" class="external">example</a>

This comes through to analytics correctly with the href as the label. What I'm wanting to do is be able to tag the current page they are on as well. So we can work out which page they were on when they clicked the link to go to an external page.

Something like this?

  $('a.external').click(function(){
    var pathName = window.location.pathname + document.location.hash;
    alert(pathName);
    _gaq.push(['_trackEvent', 'Exit Links', 'Click', $(this).attr('href')] pathName);
  });

That alerts the correct location but could anyone help me clean it up so that the pathName is added to the label within the gaq.push?

Thanks!

Sean


回答1:


Something will have to give if you want to pass the pathName as the ga event only allows three parameters that allow text.

 $('a.external').click(function () {
 var pathName = window.location.pathname + document.location.hash
 var externalLink = $(this).attr('href');
 alert(['_trackEvent', 'Exit Links', pathName, externalLink]);

});



来源:https://stackoverflow.com/questions/18654998/google-analytics-with-jquery-show-pathname-in-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!