Skip to content
necolas edited this page Mar 12, 2012 · 5 revisions

Google Analytics augments

More tracking settings

The optimized Google Analytics snippet included with H5BP includes something like this:

var _gaq = [['_setAccount', 'UA-XXXXX-X'], ['_trackPageview']];

In case you need more settings, just extend the array literal instead of .push()ing to the array afterwards:

var _gaq = [['_setAccount', 'UA-XXXXX-X'], ['_trackPageview'], ['_setAllowAnchor', true]];

Anonymize IP Addresses

In some countries, no personal data may be transferred outside jurisdictions that have have not similarly strict laws (ie. from Germany to outside the EU). Thus a webmaster using the google analytics script may have to ensure that no personal (trackable) data is transferred to the US. You can do that with the _gat.anonymizeIp option. In use it looks like this:

var _gaq = [['_setAccount', 'UA-XXXXX-X'], ['_gat._anonymizeIp'], ['_trackPageview']];

Track jQuery AJAX Requests in Google Analytics

An article by @JangoSteve explains how to track jQuery AJAX Requests in Google Analytics.

All you need is to add to the js/plugins.js:

/*
 * Log all jQuery AJAX requests to Google Analytics
 * See: http://www.alfajango.com/blog/track-jquery-ajax-requests-in-google-analytics/
 */
if (typeof _gaq !== "undefined" && _gaq !== null) {
  $(document).ajaxSend(function(event, xhr, settings){
    _gaq.push(['_trackPageview', settings.url]);
  });
}

Track javascript errors in Google Analytics

All you need is to add where _gaq defined:

(function(window){
  var undefined, 
    link = function (href) {
      var a = window.document.createElement('a');
      a.href = href;
      return a;
    };
  window.onerror = function (message, file, row) {
    var host = link(file).hostname;
    _gaq.push([
      '_trackEvent',
      (host == window.location.hostname || host == undefined || host == '' ? '' : 'external ') + 'error',
      message, file + ' LINE: ' + row, undefined, undefined, true
    ]);
  };
}(window));

or minified version (255 bytes):

(function(a){var b,c=function(b){var c=a.document.createElement("a");return c.href=b,c};a.onerror=function(d,e,f){var g=c(e).hostname;_gaq.push(["_trackEvent",(g==a.location.hostname||g==b||g==""?"":"external ")+"error",d,e+" LINE: "+f,b,b,!0])}})(window)

Track page scroll

All you need is to add where _gaq defined:

$(function(){
  var isDuplicateScrollEvent,
    scrollTimeStart = new Date,
    $window = $(window),
    $document = $(document),
    scrollPercent;
  $window.scroll(function(){
    scrollPercent = Math.round(100*($window.height() + $window.scrollTop())/$document.height());
      if (scrollPercent > 90 && !isDuplicateScrollEvent) { //page scrolled to 90%
        isDuplicateScrollEvent = 1;
        _gaq.push(['_trackEvent', 'scrool', 
          'Window: ' + $window.height() + 'px; Document: ' + $document.height() + 'px; Time: ' + Math.round((new Date - scrollTimeStart )/1000,1) + 's',
          undefined, undefined, true
        ]);
      }
    }); 
});
Clone this wiki locally