Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Mikael Malmgren
Added: Aug 2, 2018 1:00 PM
Views: 10
Tags: no tags
  1. function() {
  2.   return function(model) {
  3.     // Add the PII patterns into this array as objects
  4.     var piiRegex = [{
  5.     name: 'EMAIL',
  6.         regex: /[^\/=]{1,}@[^\/?&]{1,}\.[^\/?&]{1,}/gi
  7.     }];
  8.    
  9.     var globalSendTaskName = '_' + model.get('trackingId') + '_sendHitTask';
  10.    
  11.     // Fetch reference to the original sendHitTask
  12.     var originalSendTask = window[globalSendTaskName] = window[globalSendTaskName] || model.get('sendHitTask');
  13.  
  14.     var i, hitPayload, parts, val;
  15.    
  16.     // Overwrite sendHitTask with PII purger
  17.     model.set('sendHitTask', function(sendModel) {
  18.      
  19.       hitPayload = sendModel.get('hitPayload').split('&');
  20.       for (i = 0; i < hitPayload.length; i++) {
  21.         parts = hitPayload[i].split('=');
  22.         // Double-decode, to account for web server encode + analytics.js encode
  23.         try {
  24.           val = decodeURIComponent(decodeURIComponent(parts[1]));
  25.         } catch(e) {
  26.           val = decodeURIComponent(parts[1]);
  27.         }
  28.         piiRegex.forEach(function(pii) {
  29.           val = val.replace(pii.regex, '[REDACTED ' + pii.name + ']');
  30.         });
  31.         parts[1] = encodeURIComponent(val);
  32.         hitPayload[i] = parts.join('=');
  33.       }
  34.       sendModel.set('hitPayload', hitPayload.join('&'), true);
  35.       originalSendTask(sendModel);
  36.     });
  37.   };
  38. }