Permalink
Browse files

Update to Google Universal Analytics

This is an optimized version of the official Google Universal Analytics snippet,
based on http://mathiasbynens.be/notes/async-analytics-snippet.
The only difference is that this version still uses
`document.getElementsByTagName('script')[0]` instead of `document.scripts[0]`
for Firefox < 9 support.

Unminified, optimized code:

    (function(window, document, script, variableName, scriptElement, firstScript) {
      window['GoogleAnalyticsObject'] = variableName;
      window[variableName] || (window[variableName] = function() {
        (window[variableName].q = window[variableName].q || []).push(arguments)
      });
      window[variableName].l = +new Date;
      scriptElement = document.createElement(script);
      firstScript = document.getElementsByTagName(script)[0];
      scriptElement.src = '//www.google-analytics.com/analytics.js';
      firstScript.parentNode.insertBefore(scriptElement, firstScript)
    }(window, document, 'script', 'ga'));

    ga('create', 'UA-XXXX-Y');
    ga('send', 'pageview');

Minified:

    (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;e=o.createElement(i);r=o.getElementsByTagName(i)[0];e.src='//www.google-analytics.com/analytics.js';r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));ga('create','UA-XXXX-Y');ga('send','pageview');

Closes #1347 and #1369.
  • Loading branch information...
mathiasbynens committed Jun 3, 2013
1 parent deec362 commit 48d49e96d6db282eb9686d31ebbc5cbbbdd4d966
Showing with 7 additions and 4 deletions.
  1. +1 −0 CHANGELOG.md
  2. +6 −4 index.html
View
@@ -1,5 +1,6 @@
### HEAD
+* Update to Google Universal Analytics ([#1347](https://github.com/h5bp/html5-boilerplate/issues/1347)).
* Update to jQuery 1.10.1.
* Update to Normalize.css 1.1.2.
View
@@ -31,10 +31,12 @@
<!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
<script>
- var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
- (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
- g.src='//www.google-analytics.com/ga.js';
- s.parentNode.insertBefore(g,s)}(document,'script'));
+ (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
+ function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
+ e=o.createElement(i);r=o.getElementsByTagName(i)[0];
+ e.src='//www.google-analytics.com/analytics.js';
+ r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
+ ga('create','UA-XXXXX-X');ga('send','pageview');
</script>
</body>
</html>

5 comments on commit 48d49e9

@djbender

This comment has been minimized.

Show comment
Hide comment
@djbender

djbender Aug 25, 2013

👏 function(b,o,i,l,e,r)

👏 function(b,o,i,l,e,r)

@paulirish

This comment has been minimized.

Show comment
Hide comment
@paulirish

paulirish Aug 26, 2013

Member

👏 👏 👏 👏 👏 👏

Member

paulirish replied Aug 26, 2013

👏 👏 👏 👏 👏 👏

@vraa

This comment has been minimized.

Show comment
Hide comment

vraa replied Aug 26, 2013

👍

@mathiasbynens

This comment has been minimized.

Show comment
Hide comment
@mathiasbynens

mathiasbynens Aug 26, 2013

Member

Haha, glad to see you guys appreciate it :)

Member

mathiasbynens replied Aug 26, 2013

Haha, glad to see you guys appreciate it :)

@jlbruno

This comment has been minimized.

Show comment
Hide comment
@jlbruno

jlbruno Oct 28, 2013

I like that you even changed the way the script src was being set to reduce the number of params being set. 👍

I like that you even changed the way the script src was being set to reduce the number of params being set. 👍

Please sign in to comment.