The Basics

These are the basic code snippets you'd find within every Google Analytics implementation.

Code block

The code block that calls Google's servers for the JavaScript file has changed, as has the order of how it is executed.

via ga.js
<script type="text/javascript">
                            
  var _gaq = _gaq || [];
  
  // Your tracking calls here. e.g.:
  _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  
</script>
via analytics.js
<script>
                            
    (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');

    // Your tracking calls here. e.g.:
    ga('create', 'UA-XXXXX-Y');
    ga('send', 'pageview');
  
</script>

_setAccount

This sets the property ID so the code knows where to send the data.

via ga.js
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
via analytics.js
ga('create', 'UA-XXXXX-Y');

_trackPageview

Send in a basic pageview

via ga.js
_gaq.push(['_trackPageview']);
via analytics.js
ga('send', 'pageview');

You can also send in custom page names, overriding the default (the current URL).

via ga.js
_gaq.push(['_trackPageview','/my-custom-page']);
via analytics.js
ga('send', 'pageview', '/my-custom-page');

// OR:

ga('send', {
  'hitType': 'pageview',
     'page': '/my-custom-page'
});

Multiple Accounts

In order to host multiple Universal Analytics accounts on the same page, you need to define a namespace to work within for the trackers. One can remain the default, with no namespace, but the rest must have a namespace. Previously, this namespace would be set on every request, prefixing the method. Now in Universal Analytics, you must set it within the 'create' call (using the name property) and then prefix all of the following methods.

via ga.js
_gaq.push(['foo._setAccount', 'UA-XXXXX-Y']);
_gaq.push(['foo._trackPageview']);
via analytics.js
ga('create', 'UA-XXXXX-Y', {'name': 'foo'});
ga('foo.send', 'pageview');