Renaming the ga object

In some cases you'd like to add analytics.js to your page, but the ga variable is already being used for something else. To deal with this, analytics.js provides a mechanism for renaming the global ga object.

Renaming the global object in the tracking snippet

The JavaScript tracking snippet allows you to rename the global ga object by changing the final parameter passed to the minified function. You'll also need to update all invocations of the command queue from ga() to whatever name you choose.

For example, if you wanted to rename the ga object to analytics, you could change the tracking snippet as follows:

<!-- Google Analytics -->
<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','analytics');

analytics('create', 'UA-XXXXX-Y', 'auto');
analytics('send', 'pageview');
</script>
<!-- End Google Analytics -->

Manually renaming the global object

Renaming the global object works because analytics.js, when it loads, looks for a string stored on a global variable called GoogleAnalyticsObject. If it finds that variable, it uses the string name as the new name for the global command queue.

If you're loading analytics.js and not using the tracking snippet, you can still rename the global object by setting the name you want to use on the variable GoogleAnalyticsObject prior to loading the analytics.js library.

For example, if you're using jQuery's $.getScript method to load analytics.js, you could rename the global object with the following code:

<script>
// Instructs analytics.js to use the name `analytics`.
window.GoogleAnalyticsObject = 'analytics';

// Uses jQuery to load analytics.js instead of the tracking snippet.
$.getScript('//www.google-analytics.com/analytics.js', function() {

  // Creates a tracker and sends a pageview using the renamed command queue.
  analytics('create', 'UA-12345-1', 'auto');
  analytics('send', 'pageview');
});
</script>

The alternative async tracking snippet

Unlike the standard JavaScript tracking snippet, the alternative async tracking snippet does not offer default support for renaming the global ga object.

However, using the technique described above, you can rename the global ga object and still get all the preloading benefits of the alternative async tracking snippet.

The following modified version of the alternative async tracking snippet sets the GoogleAnalyticsObject variable to analytics and renames all instances of ga to analytics as well:

<!-- Google Analytics -->
<script>

// Instructs analytics.js to use the name `analytics`.
window.GoogleAnalyticsObject = 'analytics';

// Creates an initial analytics() function.
// The queued commands will be executed once analytics.js loads.
window.analytics = window.analytics || function() {
  (analytics.q = analytics.q || []).push(arguments)
};

// Sets the time (as an integer) this tag was executed.
// Used for timing hits.
analytics.l = +new Date;

// Creates a default tracker with automatic cookie domain configuration.
analytics('create', 'UA-12345-1', 'auto');

// Sends a pageview hit from the tracker just created.
analytics('send', 'pageview');
</script>

<!-- Sets the `async` attribute to load the script asynchronously. -->
<script async src='//www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->