I'm planning to use a javascript variable to replace certain keywords (pii:s) from the URL before sending it from GTM to GA4.
I have encountered a javascript variable that replaces certain "banned" keywords from the query string. I don't know javascript very well myself.
This variable seems to work when I test it (it replaces the values of the banned keywords). However, it does not replace the value of email, unless the value is a real email address. I can't read javascript, but is this what the variable "piiRegex" does?
Does the code look OK overall?
See the code below:
function(){
var q = {{URL Query}};
// Add the PII patterns into this array as objects
var piiRegex = [{
name: 'EMAIL',
regex: RegExp(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 'gi'),
}
];
var bannedKeywords = [
'email',
'username',
'password',
'login',
'order',
'transaction',
'telephone',
'phone',
'card',
'registration',
'unsubscribe',
'subscribe',
'firstname',
'name',
'personal_email_verify',
'personal_lastname',
'secret',
'kco',
'token',
];
var queries = q.split('&');
for (i = 0; i < queries.length; i++) {
parts = queries[i].split('=');
// Double-decode, to account for web server encode + analytics.js encode
try {
key = decodeURIComponent(decodeURIComponent(parts[0]));
val = decodeURIComponent(decodeURIComponent(parts[1]));
} catch(e) {
key = decodeURIComponent(parts[0]);
val = decodeURIComponent(parts[1]);
}
piiRegex.forEach(function(pii) {
val = val.replace(pii.regex, '[CENSORED_' + pii.name + ']');
});
if (bannedKeywords.indexOf(key) > 0) {
val = '[CENSORED_' + key + ']';
}
parts[1] = encodeURIComponent(val);
queries[i] = parts.join('=');
}
if (q != ''){
q = '?' + queries.join('&');
}
return q;
}