Question

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

Was it helpful?

Solution

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

});

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top