This guide describes tasks, an advanced feature used to customize how analytics.js validates, constructs, and sends measurement protocol requests.
Overview
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 servers. The following table describes each of these tasks, in the order they are executed:
| Task Name | Description |
|---|---|
previewTask |
Aborts the request if the page is only being rendered to generate a 'Top Sites' thumbnail for Safari. |
checkProtocolTask |
Aborts the request if the page protocol is not http
or https. |
validationTask |
Aborts the request if required fields are missing or invalid. |
checkStorageTask |
Aborts the request if the tracker is configured to use cookies but the user's browser has cookies disabled. |
historyImportTask |
Imports information from ga.js and urchin.js cookies to preserve visitor history when a site migrates to Universal Analytics. |
samplerTask |
Samples out visitors based on the
sampleRate
setting for this tracker. |
buildHitTask |
Builds a measurement protocol request string and stores it in the
hitPayload field. |
sendHitTask |
Transmits the measurement protocol request stored in the
hitPayload field to Google Analytics servers. |
timingTask |
Automatically generates a site speed timing beacon based on the
siteSpeedSampleRate setting for this tracker. |
get and
set
methods. Using these methods, you may replace tasks with your own
custom functions, or augment the existing functionality by chaining your
custom functions to execute before or after an existing task.
Implementation
This section describes how to add new functionality to existing tasks, replace the built-in task functions with your own custom code, or disable a task function entirely.
Adding to Task
To insert new functionality you can chain your custom task function to execute
before or after an existing task. In the example below, the sendHitTask
is replaced with a custom task function that first calls the original
sendHitTask function to send the normal request beacon to
google-analytics.com/collection, then executes custom code to send a copy of
the measurement protocol request to a local server.
ga('create', 'UA-XXXXX-Y', 'auto');
ga(function(tracker) {
// Grab a reference to the default sendHitTask function.
var originalSendHitTask = tracker.get('sendHitTask');
// Modifies sendHitTask to send a copy of the request to a local server after
// sending the normal request to www.google-analytics.com/collect.
tracker.set('sendHitTask', function(model) {
originalSendHitTask(model);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/localhits', true);
xhr.send(model.get('hitPayload'));
});
});
ga('send', 'pageview');
Aborting Task Processing
A task can abort the processing of subsequent tasks by throwing an exception.
If the task throwing the exception executes before the sendHitTask,
this has the effect of preventing the measurement protocol request from being
sent to Google Analytics servers. In the example below, we abort the request
whenever the user's browser contains a cookie named 'testing' with the value
'true'.
ga('create', 'UA-XXXXX-Y', 'auto');
ga(function(tracker) {
var originalBuildHitTask = tracker.get('buildHitTask');
tracker.set('buildHitTask', function(model) {
if (document.cookie.match(/testing=true/)) {
throw 'Aborted tracking for test user.';
}
originalBuildHitTask(model);
});
});
ga('send', 'pageview');
Disabling a Task
To disable any of the built-in task functions, simply replace it with null.
ga('create', 'UA-XXXXX-Y', 'auto');
ga('set', 'checkProtocolTask', null); // Disable file protocol checking.
ga('send', 'pageview');