Google Analytics with jQuery - show pathname in la

2019-07-19 01:26发布

问题:

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]);

});