Creating Trackers

Tracker objects (also known as "trackers") are objects that can collect and store data and then send that data to Google Analytics.

When creating a new tracker, you must specify a tracking ID (which is the same as the property ID that corresponds to one of your Google Analytics properties) as well as a cookie domain, which specifies how cookies are stored. (The recommended value 'auto' specifies automatic cookie domain configuration.)

If a cookie does not exist for the specified domain, a client ID is generated and stored in the cookie, and the user is identified as new. If a cookie exists containing a client ID value, that client ID is set on the tracker, and the user is identified as returning.

Upon creation, tracker objects also gather information about the current browsing context such as the page title and URL, and information about the device such as screen resolution, viewport size, and document encoding. When it's time to send data to Google Analytics, all of the information currently stored on the tracker gets sent.

The create method

The analytics.js library provides a variety ways to create trackers, but the most common way is to use the create command and pass the tracking ID and cookie domain fields as the second and third parameters:

ga('create', 'UA-XXXXX-Y', 'auto');

Naming trackers

You may also, optionally, name the tracker by passing the name field as the fourth argument in the create command. Naming a tracker is necessary in cases where you need to create more than one tracker for the same page. For more details on why you might need to do this, see the section below on working with multiple trackers.

ga('create', 'UA-XXXXX-Y', 'auto', 'myTracker');

Creating a tracker without setting the name field is known as creating a "default" tracker. A default tracker is internally given the name "t0".

Specifying fields at creation time

An optional fields object may also be passed that allows you to set any of the analytics.js fields at creation time, so they will be stored on the tracker and apply to all hits that are sent.

ga('create', 'UA-XXXXX-Y', 'auto', 'myTracker', {
  userId: '12345'

And as with all calls to the ga() function, the fields object may also be used to specify all of the fields together:

ga('create', {
  trackingId: 'UA-XXXXX-Y',
  cookieDomain: 'auto',
  name: 'myTracker',
  userId: '12345'

See the create method reference for more comprehensive details.

Working with multiple trackers

In some cases you might want to send data to multiple properties from a single page. This is useful for sites that have multiple owners overseeing sections of a site; each owner could view their own property.

To track data for two separate properties, you need to create two separate trackers, and at least one of them must be a named tracker. The following two commands create a default tracker and a tracker named "clientTracker":

ga('create', 'UA-XXXXX-Y', 'auto');
ga('create', 'UA-XXXXX-Z', 'auto', 'clientTracker');

Running commands for a specific tracker

To run analytics.js commands for a specific tracker, you prefix the command name with the tracker name, followed by a dot. When you don't specify a tracker name, the command is run on the default tracker.

To send pageviews for the above two trackers, you'd run the following two commands:

ga('send', 'pageview');
ga('clientTracker.send', 'pageview');

Future guides will go into more detail on the syntax for running specific commands. You can also refer to the command queue reference to see the full command syntax for all analytics.js commands.

Next steps

Once you've created a tracker, you may need to access the data stored on that tracker object. The next guide explains how to get and set tracker data.