Once again the Google Analytics snippet is changing (evolving) into something bigger and better… Let’s take a look…

Default New Analytics.js Script

This is currently in beta.

I saw HTML5 Boilerplate have been clever and renamed the vars.. it now reads b,o,i,l,e,r … hehe..

Don’t break you current tracking objects, simply rename the ga to whatever you’ve been using such as __gaTracker.

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
__gaTracker('create', 'UA-XXXX-Y');
__gaTracker('send', 'pageview');

Testing on localhost

In some cases you might want to test analytics.js from a webserver running on localhost. To set analytics.js cookies, you need to disable the default cookie domain using:

ga('create', 'UA-XXXX-Y', {
  'cookieDomain': 'none'

Tracking your hash urls on your web apps. you can set the page url like this. Add that to your page templates.


Don’t forget to remove the send from your main snippet. So it now looks like this:

To capture a new event simply add this when you want the event tracked

ga('send', 'event', 'category', 'action', 'label', value);

Where label and value are optional.

For example,

ga('send', 'event', 'register', 'currentUser');

GA Basic Config: https://developers.google.com/analytics/devguides/collection/analyticsjs/

GA Advanced Config: https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced?hl=es

Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

Free Guide:

How to Choose the Right Charting Library for Your Application

How do you make sure that the charting library you choose has everything you need? Sign up to receive this detailed guide from FusionCharts, which explores all the factors you need to consider before making the decision.

  • ravi pj

    Excellent Article, thank you!

Ending Soon
Free SitePoint Premium

Get one free year of unlimited book and course downloads on SitePoint Premium!