Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics pageTracker is not defined?

Little bit confused... I am trying to track mailto links being clicked, but constantly 'pageTracker is not defined' is shown. I have the following code just before my end body tag ()

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-000000']); // This is my account number, I have added the zeros in this editor
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>

Then I am using this in my mailto links

<a href="mailto:[email protected]" onClick="javascript:pageTracker._trackPageview('/mailto/hello');">[email protected]</a>

I cannot see why its not working? Any help would be appreciated

like image 331
YodasMyDad Avatar asked Aug 17 '10 14:08

YodasMyDad


4 Answers

The new Async Google Analytics code (that you're using) works a bit differently than the non-Async. Any time that you want to call a method on pageTracker you simply push a "message" onto the "_gaq" queue.

<a href="mailto:[email protected]" onClick="_gaq.push(['_trackPageview', '/mailto/hello'])">[email protected]</a>

Although, tracking a mailto link may work better as an event:

<a href="mailto:[email protected]" onClick="_gaq.push(['_trackEvent', 'mailto', 'home'])">[email protected]</a>

For more info take a look at the Async Tracking Users Guide.

like image 171
joshperry Avatar answered Sep 29 '22 12:09

joshperry


We can also add:

//mantain syntax between old and new asynch methods
//http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html#Syntax
function _pageTracker (type) {
    this.type = type;
    this._trackEvent = function(a,b,c) {
       _gaq.push(['_trackEvent', a, b, c]);
    };
}

var pageTracker = new _pageTracker();

in new code to mantain old code in pages.

like image 40
Merlinox Avatar answered Sep 29 '22 10:09

Merlinox


Here is the code :

onClick="_gaq.push(['_trackEvent', 'pdf', 'download', '/pdf/myPdf'])">myPdf</a>
like image 40
Nanda Avatar answered Sep 29 '22 12:09

Nanda


I needed a way to tack downloading PDFs too and heres what I used:

<a href="http://www.domain.com/assets/downloads/filename.pdf" target="_blank" onClick="_gaq.push(['_trackEvent', 'Downloads', 'Download', 'Price Brochure PDF'])">Download Brochure</a>

For more info about _trackEvent, heres the API Doc page

like image 41
Osura Avatar answered Sep 29 '22 11:09

Osura