ovipreneur

Custom JS - Prevent Duplicate Transaction

Jan 21st, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function() {
  2.   // customTask Builder by Simo Ahava
  3.   //
  4.   // More information about customTask: https://www.simoahava.com/analytics/customtask-the-guide/
  5.   //
  6.   // Change the default values for the settings below.
  7.  
  8.   // transactionDeduper: Configuration object for preventing duplicate transactions from being recorded.
  9.   // https://bit.ly/2AvSZ2Y
  10.   var transactionDeduper = {
  11.     keyName: '_transaction_ids',
  12.     cookieExpiresDays: 365
  13.   };
  14.  
  15.   // DO NOT EDIT ANYTHING BELOW THIS LINE
  16.   var readFromStorage = function(key) {
  17.     if (!window.Storage) {
  18.       // From: https://stackoverflow.com/a/15724300/2367037
  19.       var value = '; ' + document.cookie;
  20.       var parts = value.split('; ' + key + '=');
  21.       if (parts.length === 2) return parts.pop().split(';').shift();
  22.     } else {
  23.       return window.localStorage.getItem(key);
  24.     }
  25.   };
  26.  
  27.   var writeToStorage = function(key, value, expireDays) {
  28.     if (!window.Storage) {
  29.       var expiresDate = new Date();
  30.       expiresDate.setDate(expiresDate.getDate() + expireDays);
  31.       document.cookie = key + '=' + value + ';expires=' + expiresDate.toUTCString();
  32.     } else {
  33.       window.localStorage.setItem(key, value);
  34.     }
  35.   };
  36.  
  37.   var globalSendHitTaskName   = '_ga_originalSendHitTask';
  38.  
  39.   return function(customTaskModel) {
  40.  
  41.     window[globalSendHitTaskName] = window[globalSendHitTaskName] || customTaskModel.get('sendHitTask');
  42.     var tempFieldObject, dimensionIndex, count, ga, tracker, decorateTimer, decorateIframe, iframe;
  43.  
  44.     customTaskModel.set('sendHitTask', function(sendHitTaskModel) {
  45.  
  46.       var originalSendHitTaskModel = sendHitTaskModel,
  47.           originalSendHitTask      = window[globalSendHitTaskName],
  48.           canSendHit               = true;
  49.  
  50.       var hitPayload, hitPayloadParts, param, val, regexI, trackingId, snowplowVendor, snowplowVersion, snowplowPath, request, originalTrackingId, hitType, nonInteraction, d, transactionId, storedIds;
  51.  
  52.       try {
  53.  
  54.         // transactionDeduper
  55.         if (typeof transactionDeduper === 'object' && transactionDeduper.hasOwnProperty('keyName') && transactionDeduper.hasOwnProperty('cookieExpiresDays') && typeof sendHitTaskModel.get('&ti') !== 'undefined') {
  56.           transactionId = sendHitTaskModel.get('&ti');
  57.           storedIds = JSON.parse(readFromStorage(transactionDeduper.keyName) || '[]');
  58.           if (storedIds.indexOf(transactionId) > -1 && ['transaction', 'item'].indexOf(sendHitTaskModel.get('hitType')) === -1) {
  59.             canSendHit = false;
  60.           } else if (storedIds.indexOf(transactionId) === -1) {
  61.             storedIds.push(transactionId);
  62.             writeToStorage(transactionDeduper.keyName, JSON.stringify(storedIds), transactionDeduper.cookieExpiresDays);
  63.           }
  64.         }
  65.         // /transactionDeduper
  66.  
  67.         if (canSendHit) {
  68.           originalSendHitTask(sendHitTaskModel);
  69.         }
  70.  
  71.       } catch(e) {
  72.         originalSendHitTask(originalSendHitTaskModel);
  73.       }
  74.  
  75.     });
  76.  
  77.   };
  78. }
Advertisement
Add Comment
Please, Sign In to add comment