Advertisement
FilipFD

CJ GTM tag_template

Mar 31st, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Required Permissions
  2. const logToConsole = require('logToConsole');
  3. const log = data.debug ? logToConsole : (() => {});
  4. const injectScript = require('injectScript');
  5. const setInWindow = require('setInWindow');
  6. const copyFromWindow = require('copyFromWindow');
  7. const queryPermission = require('queryPermission');
  8. const encodeUriComponent = require('encodeUriComponent');
  9. const getCookieValues = require('getCookieValues');
  10.  
  11. //Enables cookies to be read
  12. const cookieName = "cje";
  13. let cookieValues;
  14. if (queryPermission('get_cookies', cookieName)) {
  15.   cookieValues = getCookieValues(cookieName);
  16.   var cje = cookieValues.toString();
  17. }
  18.  
  19. //Logging For Tests
  20. const onSuccess = () => {
  21.   log('CJ: Script loaded successfully.');
  22.   data.gtmOnSuccess();
  23.  
  24. };
  25. const onFailure = () => {
  26.   log('CJ: Script load failed.');
  27.   data.gtmOnFailure();
  28. };
  29.  
  30. //Enables user specified Data Layer array and array objects to be Read.
  31. const callInWindow = require('callInWindow');
  32. const getType = require('getType');
  33.  
  34. // capture item level values from template field
  35. var inputArray = data.productArray;
  36. var productKey = data.productSku;
  37. var priceKey = data.productPrice;
  38. var quantityKey = data.productQuantity;
  39. var discountKey = data.productDiscount;
  40.  
  41. // capture standard values from template fields
  42. var companyID = data.companyID;
  43. var actionID = data.actionID;
  44. var orderID = data.orderID;
  45. var wholeOrderDiscount = data.wholeOrderDiscount;
  46. var currency = data.currency;
  47. var coupon = data.coupon;
  48. var orderSubTotal = data.orderSubTotal;
  49. var page = data.pageType;
  50.  
  51. //create item array if Advanced data is input by user
  52. if(inputArray != null) {
  53.   var items = [];
  54.     for (let i = 0; i < inputArray.length; i++) {
  55.       const element = inputArray[i];
  56.       let product = [];
  57.       product.itemId = element[productKey];
  58.       product.unitPrice = element[priceKey];
  59.       product.quantity = element[quantityKey];
  60.       product.discount = element[discountKey];
  61.       items.push(product);
  62. }}
  63.  
  64. // Create object from customParameters table if data is input by user
  65. if(data.customParameters != null){
  66.   const makeTableMap = require('makeTableMap');
  67.   const customParams = data.customParameters; // This is the array of user input from Custom Parameters Field
  68.   var customParameterMap = makeTableMap(customParams, 'customFieldName', 'customFieldValue');
  69. }
  70.  
  71. if (data.dataTypeSelect == 'orderData'){
  72. var cjData = {
  73.         order:{
  74.             'enterpriseId' : companyID,
  75.             'orderId' : orderID,
  76.             'cjeventOrder' : cje,
  77.             'actionTrackerId' : actionID,
  78.             'currency' : currency,  
  79.             'amount' : orderSubTotal,    
  80.             'discount' : wholeOrderDiscount,
  81.             'coupon' : coupon,
  82.             'pointOfSale' : 'web',
  83.             'trackingSource' : 'gtm',
  84.             'pageType' : 'conversionConfirmation',
  85.             '' : customParameterMap,
  86.             'items' : items
  87.             }};
  88. }
  89.  
  90. if (data.dataTypeSelect == 'pageData'){
  91. var cjData = {
  92.         sitePage:{
  93.             'enterpriseId' : companyID,
  94.             'cartSubtotal' : orderSubTotal,    
  95.             'trackingSource' : 'gtm',
  96.             'pageType' : page,
  97.             '' : customParameterMap,
  98.             'items' : items
  99.             }};
  100.  
  101. }
  102. setInWindow("cj", cjData, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement