Simo Ahava has a customtask generator, a great thing, it combines his becoming an article about customtask in Google tag manager into one user-friendly generator.
I added a few improvements…
Remove the .forEach function
In what?
Remove PII from Hits , it is a function in CustomTask, which if it encounters that in the URL or GA parameters there is an email, or other personal data defined via a regular expression, it will replace them with a placeholder. This change will not violate Google Analytics, to which personal information may not be sent.
What would I change?
.ForEach function
piiRegex.forEach(function(pii) { _pr_val = _pr_val.replace(pii.regex, '[REDACTED ' + pii.name + ']'); });
I would replace it with the classic ” for ” function .
for (var i = 0, len = piiRegex.length; i < len; i++) { _pr_val = _pr_val.replace(piiRegex[i].regex, '[REDACTED ' + piiRegex[i].name + ']'); }
Why?
EI 11 does not support the .forEach function . Classic for is even faster 😉,
Catching errors in CustomTask
Mistakes make me wonder what’s going on, what’s wrong.
Each time you use try {…} catch (err) {…} it has a chance to know the error message. So why not do it?
In the last lines, customTask is a clue if something happens.
} catch(err) { originalSendHitTask(originalSendHitTaskModel); }
And just expand it by sending more information via the standard dataLayer.push event to Google analytics.
} catch(err) { originalSendHitTask(originalSendHitTaskModel); window.dataLayer.push({ 'event': 'error.ga.customtask.catch', 'eventCategory': 'error', 'eventAction': 'error.gtm.ga.customtask.catch', 'eventLabel': err.message, 'eventValue': 0, 'eventNonInteraction': true}); };
One of the bugs you will discover is the previous one, with Internet Explorer 11.