This reference describes the methods available on the Tracker
object.
Method Summary
Methods | |
---|---|
get(fieldName) |
returns: Gets the value of a field stored on the tracker. |
set(fieldName|fieldsObject, [fieldValue]) |
returns: Sets a field/value pair or a group of field/value pairs on the tracker. |
send([hitType], [...fields], [fieldsObject]) |
returns: Sends a hit to Google Analytics. |
Method Details
get
Gets the value of a field stored on the tracker.
Usage
tracker.get(fieldName);
Parameters
Name | Type | Required | Description |
---|---|---|---|
fieldName |
string |
yes | The name of the field the get the value of. |
Returns
*
Examples
// Creates a default tracker. ga('create', 'UA-XXXXX-Y', auto); // Gets the client ID of the default tracker and logs it. ga(function(tracker) { var clientId = tracker.get('clientId'); console.log(clientId); });
set
Sets a field/value pair or a group of field/value pairs on the tracker.
Usage
// Sets a single field/value pair. tracker.set(fieldName, fieldValue);
// Sets a group of field/value pairs. tracker.set(fieldsObject);
Parameters
See the field reference for individual field documentation.
Returns
undefined
Examples
// Creates a default tracker. ga('create', 'UA-XXXXX-Y', auto); ga(function(tracker) { // Sets the page field to "/about.html". tracker.set('page', '/about.html'); });
// Creates a default tracker. ga('create', 'UA-XXXXX-Y', auto); ga(function(tracker) { // Sets both the page and title fields. tracker.set({ page: '/about.html', title: 'About' }); });
send
Sends a hit to Google Analytics.
Usage
tracker.send([hitType], [...fields], [fieldsObject]);
The fields that are sent are the values specified in the ...fields
parameters and fieldsObject
, merged with the fields currently stored on the tracker.
Parameters
The fields that can be specified by the ...fields
parameters vary depending on the hit type. The following table lists the fields that correspond to each hit type. Hit types not listed do not accept ...fields
parameters, only the fieldsObject
.
Hit type | ...fields |
---|---|
pageview |
page |
event |
eventCategory , eventAction , eventLabel , eventValue |
social |
socialNetwork , socialAction , socialTarget |
timing |
timingCategory , timingVar , timingValue , timingLabel |
See the field reference for individual field documentation.
Returns
undefined
Examples
// Creates a default tracker. ga('create', 'UA-XXXXX-Y', auto); ga(function(tracker) { // Sends a pageview hit. tracker.send('pageview'); });
// Creates a default tracker. ga('create', 'UA-XXXXX-Y', auto); ga(function(tracker) { // Sends an event hit for the tracker named "myTracker" with the // following category, action, and label, and sets the nonInteraction // field value to true. tracker.send('event', 'link', 'click', 'http://example.com', { nonInteraction: true }); });