📜 ⬆️ ⬇️

Google Tag Manager usage examples for customTask

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.js

Below 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 dimension

The most reliable way to transfer Client ID to date. Setup guides:


2. Duplication of the GA hit and sending it to other places

Manuals:


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 users

Manual 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 GA

Manual 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 expression

Manual 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 Lenght

Manual 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:


An example of combining several solutions into one customTask

What we combine:


 function() { var newTrackingId = 'UA-12345678-1'; return function(model) { //Define client ID model.set('dimension1', model.get('clientId')); // Add the PII patterns into this array as objects var piiRegex = [{ name: 'EMAIL', regex: /(?<=emailAddress=|email=).+?(?=@|%40)/gi }]; // Fetch reference to the original sendHitTask var globalSendTaskName = '_' + model.get('trackingId') + '_sendHitTask'; var originalSendTask = window[globalSendTaskName] = window[globalSendTaskName] || model.get('sendHitTask'); var i, hitPayload, parts, val, oldTrackingId; model.set('sendHitTask', function(sendModel) { // Overwrite sendHitTask with PII purger hitPayload = sendModel.get('hitPayload').split('&'); for (i = 0; i < hitPayload.length; i++) { parts = hitPayload[i].split('='); // Double-decode, to account for web server encode + analytics.js encode try { val = decodeURIComponent(decodeURIComponent(parts[1])); } catch (e) { val = decodeURIComponent(parts[1]); } piiRegex.forEach(function(pii) { val = val.replace(pii.regex, '[REDACTED ' + pii.name + ']'); }); parts[1] = encodeURIComponent(val); hitPayload[i] = parts.join('='); } sendModel.set('hitPayload', hitPayload.join('&'), true); originalSendTask(sendModel); // Rewrite the tracking ID hitPayload = sendModel.get('hitPayload'); oldTrackingId = new RegExp(sendModel.get('trackingId'), 'gi'); sendModel.set('hitPayload', hitPayload.replace(oldTrackingId, newTrackingId), true); originalSendTask(sendModel); }); }; } 

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.

Source: https://habr.com/ru/post/359196/


All Articles