You can use the customTask Builder tool to create a customTask
script. customTask
is a method of the Google Analytics library, which lets you run JavaScript code when the hit request to Google Analytics is being built.
Click here to jump directly to the tool.
This is very useful for a number of reasons, and I recommend you read customTask
- The Guide before doing anything with the tool.
The tool automatically builds the necessary JavaScript, avoiding any potential conflicts that the overlapping methods might introduce otherwise.
When you add individual features to the script by selecting items in the table, you’ll see how the final script is modified accordingly.
When you are satisfied with the result, select all the text within the white code box. You can also click the Copy to clipboard button to automatically copy the code to your clipboard.
General deployment instructions
Upon adding individual items to the script, you’ll notice that the first lines of the script contain default values that you’ll need to modify. For example, when adding the Client ID as a Custom Dimension item, you’ll see these lines added to the code:
// clientIdIndex: The Custom Dimension index to where you want to send the visitor's Client ID.
// https://bit.ly/2Ms0ZcC
var clientIdIndex = 1;
The value 1
is just a default value - you’ll need to change this to the Custom Dimension index you have created in Google Analytics for this specific purpose.
Thus after copy-pasting the code into place, make sure you edit all the default values with the actual values you want to configure the items with. Otherwise you will run into severe data quality issues.
Deployment with Google Tag Manager
If you are using Google Tag Manager, you need to copy-paste the code into a Custom JavaScript variable. If you use the “Copy to clipboard (for GTM)” button, you don’t have to modify the JavaScript as instructed below.
If, for some reason, you refuse to use the “Copy to clipboard (for GTM)” button, after copy-pasting the contents to the Custom JavaScript variable, you’ll need to modify the first line by removing the var _customTask =
part like this:
// OLD, CHANGE THIS:
var _customTask = function() {
// NEW, TO THIS:
function() {
The second change you need to do is remove the semi-colon at the very end of the code block. In other words, remove the very last character of the entire code block, which should be a semi-colon ;
.
Once you’re done modifying the default values, save the variable with the name {{customTask}} or something similar and descriptive.
Then, in your Google Analytics Settings variable, go to Fields to set, and add a new field:
Field name: customTask
Value: {{customTask}}
If you’re not using a Google Analytics Settings variable, you need to click Enable overriding settings in this tag in all the Google Analytics tags where you want to add this customTask
as a field, and make the same change directly to the tag itself.
Deployment with analytics.js
With the regular (legacy) Universal Analytics library, you first need to make sure the variable is written into memory, so in the site JavaScript (or page template), make sure the code in the code box below is executed before any Google Analytics commands are run.
Then, when you want to invoke this customTask
in your trackers, find where you are creating the tracker and make the following change:
ga('create', 'UA-12345-1');
ga('set', 'customTask', _customTask()); // <-- Add this right after creating the tracker
ga('send', 'pageview');
The customTask Builder tool
The
customTask
Builder tool only works in the regular site, not the AMP pages. Please follow this link to use the tool.
Summary
I hope you find this tool useful. I will try to update it as I come up with new customTask
ideas, which seems to be quite often.
Please let me know in the comments if you are having trouble using the tool, or if you have new customTask
ideas in mind you’d want to include in the tool.