The material is based on the Simo Ahava article "customTask - The Guide" .About a year ago (in 2017), the Universal Analytics JS library was updated. The update brought with it such a wonderful thing as
customTask . This feature, within the framework of Google Tag Manager, allows you to perform any “tasks” before sending a hit to Google Analytics.
In the
original article, Simo Ahava describes in detail the mechanism of how customTask works and shares useful examples. You can also familiarize yourself with the
official manual on the Universal Analytics library, which describes all tasks with their order of execution.
')
The order of the tasks analytics.jsBelow is a brief overview of customTask usage examples. I am sure some of the solutions will be useful for you.
Examples of using customTask (quick review)
1. Record Client ID in custom dimensionThe most reliable way to transfer Client ID to date. Setup guides:
2. Duplication of the GA hit and sending it to other placesManuals:
- Sending to multiple GA property , by Simo Ahava.
A good idea for optimizing tags in GTM (no need to create additional tags to send data to multiple accounts). The only drawback is that if there are cross-domains tracking in the tag settings, then the duplicate hit will leave without these settings (only the original hit will save the tag settings). - Sending to Google Sheet by dkomarovskiy.
Upload all hit data (payload) to Google Sheet. Useful for debugging and data analysis (if the project is small). - Sending to Google Bigquery ( note! SendHitTask is used here, ie you cannot use customTask and sendHitTask at the same time; you can rewrite the code from sendHitTask to customTask - then everything is ok ) by Dmitri Ilin.
Steep manual if you need to completely get rid of the sampling and limitations of GA. - Sending to Snowplow , by Simo Ahava.
Snowplow is a service that allows you to create / organize your own pipeline for working with data, including web analytics. The service includes all processes of working with data: from collection / processing to storage / analysis / visualization, i.e. You can make your own GA with blackjack and pivot tables.
3. Removal of personal data from GA payload (GDPR *)Manual from Simo Ahava.
First of all, it is useful for those who work with EU countries and are faced with meeting the requirements of the
GDPR . This solution allows you to remove all personal data from payload'a (wherever they are: URL, Custom Dimension, Event Label, etc.).
For example page path:
/test?tel=+44012345678&email=brian@me.com&other=bclifton@DOMAIN.com&firstName=brian&password=hello
after modifying customTask will be like this:
/test?tel=[REDACTED TELEPHONE]&email=b[REDACTED EMAIL]om&other=bcli[REDACTED SELF-EMAIL]OMAIN.com&firstName=[REDACTED NAME]&password=[REDACTED PASSWORD]
4. Tracking offline usersManual from Simo Ahava.
Solves the problem of a broken connection i. for example, when the connection is terminated, sending the hit does not end with an error (because there is no connection), but is fixed in the queue in localStorage. When the connection resumes, the hit is sent. It is useful for projects with a high share of the audience from mobile traffic with unstable connections (often travel by subway for example).
5. Accounting user consent to send data to GA (GDPR *)Manual from Simo Ahava.
Useful in meeting the requirements of
GDPR . For example, on your site there is a pop-up in which the user agrees / does not agree to the processing of their personal data by 3 persons. If refused, this customTask will not send data to GA and DoubleClick.
6. Submitting the Google Optimize Experiment ID to GAManual from Simo Ahava.
Helps with the problem of displaying Google Optimize data in GA. During the experiment, the user is assigned the variables Experiment Name and Experiment ID. Linking this data to specific sessions / segments is a problem this customTask helps to cope with. Task checks the Page View tag if it contains data about the experiment - writes it to the custom dimension and sends a hit.
7. Auto Link Domains as a regular expressionManual from Simo Ahava.
Solves the problem of the inability to use regular expressions in GTM tags in the
Auto Link Domains field when setting up inter-domain tracking.
8. GA Automatic Cutting GA Payload LenghtManual from Simo Ahava.
A beautiful solution to the problem of exceeding payload lenght. One by one, it removes unnecessary parameters from payload until it is less than 8192. You can customize what to throw out specifically.
Merge several solutions into one customTask
If you want to combine several solutions in one customTask, then you need to remember the following rules for working with customTask:
- Only one customTask can be defined in a GTM tag.
- In JS code customTask'a, the model parameter is defined once.
The sendHitTask task is also sent once (within one tag):
model.set ('sendHitTask', function (sendModel) {...});
If you write several sendHitTask in one customTask - only one (the last) is sent.
An example of combining several solutions into one customTaskWhat we combine:
- Client dimension definition in custom dimension
- removal of personal data (PII) from payload
- sending hit to several GA property
function() { var newTrackingId = 'UA-12345678-1'; return function(model) {
First we determine the Client ID. Then, through regular expressions, we specify what personal data we want to search. In this example, only one parameter is specified - email. More can be added (see the
Simo Ahava manual ).
The next step is to check payload for personal data and overwrite it. There is one nuance - double decoding. A try ... catch statement has been added to catch instances if the percent sign is found at '%' (the condition% 40 has also been added to regex). If this instruction is removed, then hits in which it is encountered '%' will not be available because JS error occurs. There are many ways of decoding, in this case, it is one of the working options. You can try your own.
The last steps are sending the modified payload to the original GA property and duplicating / sending to another GA property.
If you have more examples of using customTask - share your experience. I would be happy to add an article.