Skip to content

Commit

Permalink
Update to Google Universal Analytics
Browse files Browse the repository at this point in the history
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 48d49e9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
10 changes: 6 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@paulirish
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 👏 👏 👏 👏 👏

@vraa
Copy link

@vraa vraa commented on 48d49e9 Aug 26, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@mathiasbynens
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, glad to see you guys appreciate it :)

@jlbruno
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please # to comment.