About
What is a user-event tracker ? (Analytics Script) in Google analytics
Tracker objects (also known as “trackers”) are objects that can collect and store data and then send that data to Google Analytics.
Management
Create
// Queues a named tracker object for creation.
ga('create', 'UA-XXXXX-Y', 'auto', 'myTracker');
where:
- UA-XXXXX-Y is the tracking id (property id)
- auto defines the cookie domain which specifies how cookies are stored (auto is the recommended value)
- myTracker is the name of the tracker (default to tracker). Seeworking_with_multiple_trackers
- More … see Create field reference
Initialization
- If a cookie does not exist for the specified domain, a client ID is generated and stored in the cookie.
- the tracker will gather the browsing context (page, url, screen,…)
Get
Getting a reference to a tracker object requires waiting until after the create command has been executed with the ready callback
- When using only one tracker (default)
ga(function(tracker) {
console.log(tracker);
});
- when using more than one
ga(function() {
console.log(ga.getByName('myTracker'));
});
Get Name
The default trackers are given the name “t0”.
ga(function(tracker) {
console.log(tracker.get('name'));
});
Get properties
by name
ga(function(tracker) {
console.log(tracker.get('name'));
console.log(tracker.get('clientId'));
console.log(tracker.get('referrer'));
});
where:
- name is the name of the tracker
- clientId is the client id
- referrer is the HTTP referrer
by id defined in the measurement protocol
ga(function(tracker) {
console.log(tracker.get('&dt'));
});
// same as
ga(function(tracker) {
console.log(tracker.get('title'));
});
where:
- & is a prefix that define the type of the properties to a parameter of the protocol
- dt is the document title as you can see kn
Set properties
- On the default tracker
ga('set', {
page: '/about',
title: 'About Us'
});
// or
ga(function(tracker) {
tracker.set('page', '/about');
});
- on a named tracker
ga('myTracker.set', 'page', '/about');
List
ga('create', 'UA-XXXXX-Y', 'auto', 'tracker1');
ga('create', 'UA-XXXXX-Z', 'auto', 'tracker2');
ga(function() {
// Logs an array of all tracker objects.
console.log(ga.getAll());
});
Exist
When there is no default tracker, the first argument of the ready callback is undefined
ga(function(tracker) {
console.log(tracker); // Logs `undefined`.
});
Send
Each time the send command is called, analytics.js executes a sequence of tasks to validate, construct, and send a measurement protocol request from the user's browser to Google Analytics.
See: