ga Object Methods Reference

This reference describes the methods available on the ga object.

Method Summary

The following methods are available on the ga object after the analytics.js library is loaded. Since these methods are not available right away, you should always invoke them using the ga command queue's ready callback.

Don't — use ga object methods outside a readyCallback as the methods may not be available yet.

var trackers = ga.getAll();

Do — use ga object methods inside a readyCallback as they're guaranteed to be available.

ga(function() {
  var trackers = ga.getAll();
});
Methods
create([trackingId], [cookieDomain], [name], [fieldsObject]);

returns: Tracker

Creates a new tracker instance with the specified fields.

getByName(name)

returns: Tracker

Gets the tracker instance with the specified name.

getAll()

returns: Array<Tracker>

Gets all tracker instances.

remove(name)

returns: undefined

Removes the tracker instance with the specified name.

Method Details

create

Creates a new tracker instance with the specified fields.

Usage

ga.create([trackingId], [cookieDomain], [name], [fieldsObject]);

Parameters

See the field reference for individual field documentation.

Returns

Tracker

Examples

// Creates a default tracker for the property UA-XXXXX-Y
// and uses automatic cookie domain configuration.
ga(function() {
  var tracker = ga.create('UA-XXXXX-Y', 'auto');
})
// Creates a tracker with the name "myTracker" for the property
// UA-XXXXX-Y, sets the cookieDomain to "example.com" and specifies
// a site speed sample rate of 10%.
ga(function() {
  var myTracker = ga.create('UA-XXXXX-Y', 'example.com', 'myTracker', {
    siteSpeedSampleRate: 10
  });
});

getByName

Gets the tracker instance with the specified name.

Usage

ga.getByName(name);

Parameters

Name Type Required Description
name string yes The name of the tracker to get.

Returns

Tracker

Examples

// Gets the default tracker.
ga(function() {
  ga.getByName('t0');
});
// Gets the tracker with the name "myTracker".
ga(function() {
  ga.getByName('myTracker');
});

getAll

Gets all tracker instances.

ga.getAll();

Returns

Array<Tracker>

Example

// Logs a list of all tracker names to the console.
ga(function() {
  var trackers = ga.getAll();
  trackers.forEach(function(tracker) {
    console.log(tracker.get('name'));
  });
});

remove

Removes the tracker instance with the specified name.

Usage

ga.remove(name);

Parameters

Name Type Required Description
name string yes The name of the tracker to remove.

Returns

undefined

Examples

// Removes the default tracker.
ga(function() {
  // Note that, unlike the ga command queue's remove method,
  // this method requires passing a tracker name, even when
  // removing the default tracker.
  ga.remove('t0');
});
// Removes the tracker with the name "myTracker".
ga(function() {
  ga.remove('myTracker');
});