Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Google Universal Analytics #1369

Closed
wants to merge 1 commit into from

Conversation

mathiasbynens
Copy link
Member

This is an optimized version of the official Google Universal Analytics snippet, based on http://mathiasbynens.be/notes/async-analytics-snippet#universal-analytics. 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.

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.
(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]=

This comment was marked as abuse.

This comment was marked as abuse.

@necolas
Copy link
Member

necolas commented Jun 1, 2013

#shipit from me

@drublic
Copy link
Member

drublic commented Jun 2, 2013

Integrated and tested with CSS Modal. Works great for me.

mathiasbynens added a commit that referenced this pull request Jun 3, 2013
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.
@mathiasbynens
Copy link
Member Author

Merged. Thanks for reviewing!

@mathiasbynens mathiasbynens deleted the update-google-analytics branch June 3, 2013 17:32
@asuh
Copy link

asuh commented Jun 7, 2013

Hey guys, I'm having issues with this new snippet. Google's beta page for this says

Existing ga.js users should create a new web property for analytics.js and dual tag their site. It is perfectly safe to include both ga.js and analytics.js snippets on the same page.

What does it mean to create a new web property? Should existing users be using both the old and now current GA code?

@philwareham
Copy link

@asuh It means create a completely new property for your site from within Google Analytics admin tab, it'll then give you the option of using the analytics.js as the script. You'll receive a new property ID to use too (probably the same as your previous ID but with -2 affix instead of -1)

So the option is there to also keep your old property running untouched on the older ga.js and old property ID - for historic reasons - if you want.

@asuh
Copy link

asuh commented Jun 11, 2013

@philwareham Thank you for explaining this to me. It wasn't obvious and I was left confused. Considering this new technique, is there any reason why someone should run both the current and beta code at the same time for tracking? Essentially, creating a new property ID and implementing this code abandons the GA with all the stats and info and starts the new one from scratch. Or am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update Google Analytics snippet to use Universal Analytics (analytics.js) instead of ga.js
5 participants