Skip to content

Update Google Analytics snippet to use Universal Analytics (analytics.js) instead of ga.js #1347

Closed
adeelejaz opened this Issue Apr 8, 2013 · 23 comments

10 participants

@adeelejaz

This is probably still open to debate while Google brings analytics.js out of beta but I've tried it on a few websites since it's release and it is pretty amazing in extra info and tools it brings.

It isn't really pretty (may be we can hack it to reduce it's footprint).

More Info: https://developers.google.com/analytics/devguides/collection/analyticsjs/

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXX-Y');
ga('send', 'pageview');
@mathiasbynens
H5BP member

Optimized version here: http://mathiasbynens.be/notes/async-analytics-snippet#universal-analytics (Assumes Firefox ≥ 9)

@necolas
H5BP member
necolas commented Apr 8, 2013

Yeah I saw this over on mathias' blog. Might wait until it's out of beta.

@mathiasbynens We'll probably make sure this works with older versions of Firefox too, so will need to revert the document.scripts change :)

@adeelejaz

Out of curiosity @necolas, is async issue only with Firefox 3.6 or older versions of Firefox as well?

PS: A few of my clients websites have a significant number of Firefox 2.x and Firefox 3.x users.

@wamatt
wamatt commented May 14, 2013

It's well known @paulirish works for Google. IMHO they're a great company, but is it really fair that GA analytics are promoted as default and not Mixpanel, KISSMetrics or other smaller companies etc?

Perhaps there might be an agnostic solution?

@adeelejaz

@wamatt The whole idea of boilerplate is to have "most common" piece in place to save time. The second most important element of boilerplate is if you ain't happy with something, rip it out. Feel free to fork the project and change it to your hearts content.

PS: If you haven't noticed, I do not work for Google but I have been using Analytics well before this project came into existence much like thousands out there.

@wamatt
wamatt commented May 14, 2013

@adeelejaz sure. I was actually editing my comment to include that acknowledgement. BTW the old "Fork or GTFO" type of dismissal feels somewhat aggressive.

Anyhow, let me explain. There are probably some new users that view the boilerplate as "this is how to get started with HTML5". It's almost has an official context to it, whether that is intended or not.

Where possible it should be corporate agnostic and apolitical, would you not agree?

@roblarsen
H5BP member
@adeelejaz

@wamatt really? "some new users"? You want to make a point about corporate agnostic and apolitical while taking a stab at @paulirish and his employer?

I have a suggestion for you, next time when you try to raise a point in an open community, please make sure you refrain for personal attacks. We have a lot of contributors on this project. Not all of them get their pay cheques from Google.

As for the "Fork or GTFO", I'm sorry but you just haven't understood the point of this. You fork this project, add a comment above the Analytics snippet about wiki entry explaining alternate options and if you prefer, provide alternate snippets. Once you are happy, send a pull request. Makes sense?

@wamatt
wamatt commented May 14, 2013

@roblarsen is the strawman necessary? I don't hold those views about Google or Paul.

@adeelejaz OIC, sorry thought you were suggesting something else. :) Thanks that's a sensible suggestion.

@roblarsen
H5BP member

c'mon, what else were you insinuating when you said "it's well known Paul works at Google." Anyone with half a brain is going to read that as you insinuating that GA is only in the project because Paul's corporate masters told him to. That's such a "nudge, nudge, wink wink" statement it's laughable. And your feigned ignorance of how people are going to read that is also laughable.

If you don't want people to misinterpret you, don't write in loaded but vaguely conspiratorial terms. Step up and say what you actually mean and then people aren't left to figure it out for themselves (which, as you've seen here, can backfire)

@wamatt
wamatt commented May 14, 2013

Furthermore according to Wikipedia, Google Analytics is the most popular analytics program in the world.[1]

If Microsoft Windows happens to be the dominant desktop OS, should software only consider that the use case? Or if Internet Explorer happens to be the most popular browser, should we consider IE specific extensions as sensible? etc.

"Being the most popular" does not seem like a convincing argument.

Sure analytics are useful to beginners, but so is stuff like choosing a webhost or domain registrar. Should we throw that in too?

[1] http://en.wikipedia.org/wiki/Google_Analytics#Popularity

@wamatt
wamatt commented May 14, 2013

@roblarsen there was no loading or conspiracy in my language, if you disagree, please feel free to point it out. My only assumption is that it's natural, common and in no way sinister, to have one's company (especially in the tech industry) influence directly or indirectly technology decisions.

I'm friends with a number of Googlers (among other software corporates). They are a great bunch, and often use and promote software that their company makes. For most purposes it's perfectly fine, as it's a cultural thing.

@roblarsen
H5BP member

I'm clearly not the only one who read it that way. In my reading you couldn't have worded it more provocatively.

FWIW, the GA snippet predates Paul working at Google.

According to Builtwith, GA is on 66% of the top million sites. There's nothing else even close that's also free (most of the other top ten are paid services.) So, as I said, the only viable option if you really believe that having GA here somehow poisons the project is to have no snippet for analytics at all. Adding some random option no one has ever heard of is pointless ("yeah boss, H5BP says use floozylitics, to we're switching from GA") so it's GA (by far the most common and therefore most useful as example code) or nothing. To my mind the nothing option does no one any good. The GA snippet does millions of folks some good as it helps them optimize code they're already using. that's super easy math. Helping millions of people is better than helping no one especially if it's to take a vague (anti-corporate? how does that even work if we then promote some other company?) stand.

@sarukuku

I don't see how changing from GA to something else or loosing it altogether would benefit most of the boilerplate users. It's simple math as @roblarsen said.

@wamatt
wamatt commented May 14, 2013

It's well known @paulirish works for Google. IMHO they're a great company, but is it really fair that GA analytics > are promoted as default and not Mixpanel, KISSMetrics or other smaller companies etc?

Perhaps there might be an agnostic solution?

hmm. Good point. For now GA is by far the largest free analytics suite out there, but I agree in principle that an agnostic solution would be preferable, although it's questionable how that would practically work. Why not submit a pull-request for discussion?

@wamatt
wamatt commented May 14, 2013

@roblarsen says:

FWIW, the GA snippet predates Paul working at Google.

I'll take your word on it, so fair enough, that was an assumption on my part.

I do want to point out that it's probably more correct to call GA a freemium service. Heavy usage sites pay a flat rate of $150k per year. Furthermore, an argument can be made that visitor data is used to enhance other commercial product offerings. HTML5 Boilerplate no doubt continues to add to that that popularity.

Also it's quite possible to be prochoice and agnostic, without being "anti-corporate".

Having said all that, there doesn't seem to be an ideal solution. In that case I agree it's probably more useful to have analytics than not, for new users.

@SoonDead

Google Analytics officially tells you now to use analytics.js if you are setting up a new domain, so it would make sense to include it in the boilerplate instead of ga.js.

(And @paulirish is clearly evil and h5bp is part of a global conspiracy that plays.the whole internet into google's hand by promoting it's analytics script. Pretty diabolical if you ask me.)

@Archie22is

Guy seriously now, you need to stop it. If you do no like the way the HTML5-boilerplate works, then create your own platform. We can't keep attacking each other like that - it's a free world. If you don't like something that is give to you (for free) then get your own.

#justsaying

@williammalo

Pro tip: the www isn't required, so you can use "//google-analytics.com/analytics.js" to save 4 bytes!

@necolas
H5BP member
necolas commented Jun 1, 2013

Does someone feel like putting together a Pull Request for this?

@mathiasbynens
H5BP member

Done in #1369.

@mathiasbynens mathiasbynens added a commit that referenced this issue Jun 1, 2013
@mathiasbynens mathiasbynens 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.
cc408b8
@mathiasbynens mathiasbynens added a commit that referenced this issue Jun 1, 2013
@mathiasbynens mathiasbynens 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.
4b7285d
@mathiasbynens mathiasbynens added a commit that referenced this issue Jun 1, 2013
@mathiasbynens mathiasbynens 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.
323220c
@mathiasbynens mathiasbynens added a commit that closed this issue Jun 3, 2013
@mathiasbynens mathiasbynens 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.
48d49e9
@WraithKenny WraithKenny added a commit to WraithKenny/html5-boilerplate that referenced this issue Jul 16, 2013
@mathiasbynens mathiasbynens 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.
fd4439c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.