Skip to content

Commit

Permalink
Update to remove modern JS
Browse files Browse the repository at this point in the history
  • Loading branch information
SbDove committed Jun 27, 2024
1 parent 0ffc365 commit 15bcc63
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 68 deletions.
114 changes: 60 additions & 54 deletions src/commerce-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CommerceHandler.prototype.logCommerceEvent = function(event) {
21: ProductRemoveFromWishlist,
22: ProductImpression
*/
let events = [];
var events = [];

switch (event.EventCategory) {
case ProductActionTypes.Impression:
Expand All @@ -130,154 +130,160 @@ CommerceHandler.prototype.logCommerceEvent = function(event) {
events = buildProductActionEvents(event);
};

events.forEach((event) => {
for (var i = 0; i < events.length; i++) {
var event = events[i];
window.heap.track(event.Name, event.Properties);
});
}
};

function buildImpressionEvents(event) {
let events = [];
var events = [];

if (event.ProductImpressions.length > 0) {
event.ProductImpressions.forEach((impression) => {
let productSkus = [];
for (var i = 0; i < event.ProductImpressions.length; i++) {
var impression = event.ProductImpressions[i];
var productSkus = [];
if (impression.ProductList.length > 0) {
impression.ProductList.forEach((product) => {
let [productEvent, productSku] = buildProductEvent(product);
events.push(productEvent);
if(productSku) {
productSkus.push(productSku);
for(var j = 0; j < impression.ProductList.length; j++) {
var product = impression.ProductList[j];
var eventObject = buildItemEvent(product);
events.push(eventObject.event);
if(eventObject.sku) {
productSkus.push(eventObject.sku);
}
})
}
}

events.push(buildActionEvent(event, HeapConstants.EventNameImpression, productSkus))
})
}
}


return events;
};

function buildPromotionEvents(event) {
let events = [];
let promotionIds = [];
var events = [];
var promotionIds = [];
if (event.PromotionAction.PromotionList.length > 0) {
event.PromotionAction.PromotionList.forEach((promotion) => {
let [promotionEvent, promotionId] = buildPromotionEvent(promotion);
events.push(promotionEvent);
for (var i = 0; i < event.PromotionAction.PromotionList.length; i++) {
var promotion = event.PromotionAction.PromotionList[i];
var eventObject = buildPromotionItemEvent(promotion);
events.push(eventObject.event);

if(promotionId) {
promotionIds.push(promotionId);
if(eventObject.id) {
promotionIds.push(eventObject.id);
}
})
}
}
let promotionActionEventName = HeapConstants.EventNamePromotionPart + PromotionTypeNames[event.EventCategory];
var promotionActionEventName = HeapConstants.EventNamePromotionPart + PromotionTypeNames[event.EventCategory];
events.push(buildActionEvent(event, promotionActionEventName, promotionIds));
return events;
}

function buildProductActionEvents(event) {
let events = [];
let productSkus = [];
var events = [];
var productSkus = [];

if (event.ProductAction.ProductList.length > 0) {
event.ProductAction.ProductList.forEach((product) => {
let [productEvent, productSku] = buildProductEvent(product);
for (var i = 0; i < event.ProductAction.ProductList.length; i++) {
var product = event.ProductAction.ProductList[i];
var eventObj = buildItemEvent(product);

events.push(productEvent);
events.push(eventObj.event);

if (productSku) {
productSkus.push(productSku);
if (eventObj.sku) {
productSkus.push(eventObj.sku);
}
});
}
}
let actionEventName = event.ProductAction == null ? HeapConstants.EventNameProductAction : HeapConstants.EventNameProductActionPart + ProductActionNames[event.ProductAction.ProductActionType]
var actionEventName = event.ProductAction == null ? HeapConstants.EventNameProductAction : HeapConstants.EventNameProductActionPart + ProductActionNames[event.ProductAction.ProductActionType]
events.push(buildActionEvent(event, actionEventName, productSkus));

return events;
}

function buildActionEvent(event, eventName, productSkus) {
let properties = event.EventAttributes == null ? {} : event.EventAttributes;
var properties = event.EventAttributes == null ? {} : event.EventAttributes;
properties[HeapConstants.KeyProductSkus] = productSkus;
return {Name: eventName, Properties: properties};
}

function buildProductEvent(product) {
let event = {};
let properties = product.Attributes;
function buildItemEvent(product) {
var event = {};
var properties = product.Attributes;
if (!properties) {
properties = {};
}

let validatedName = validateHeapPropertyValue(product.Name);
var validatedName = validateHeapPropertyValue(product.Name);
if (validatedName) {
properties[HeapConstants.KeyProductName] = validatedName;
}

let validatedPrice = validateHeapPropertyValue(product.Price);
var validatedPrice = validateHeapPropertyValue(product.Price);
if (validatedPrice) {
properties[HeapConstants.KeyProductPrice] = validatedPrice;
}

let validatedQuantity = validateHeapPropertyValue(product.Quantity);
var validatedQuantity = validateHeapPropertyValue(product.Quantity);
if (validatedQuantity) {
properties[HeapConstants.KeyProductQuantity] = validatedQuantity;
}

let validatedTotalProductAmount = validateHeapPropertyValue(product.TotalProductAmount);
var validatedTotalProductAmount = validateHeapPropertyValue(product.TotalProductAmount);
if (validatedTotalProductAmount) {
properties[HeapConstants.KeyProductTotalProductAmount] = validatedTotalProductAmount;
}

let validatedSku = validateHeapPropertyValue(product.Sku);
var validatedSku = validateHeapPropertyValue(product.Sku);
if (validatedSku) {
properties[HeapConstants.KeyProductSku] = validatedSku;
}

let validatedBrand = validateHeapPropertyValue(product.Brand);
var validatedBrand = validateHeapPropertyValue(product.Brand);
if (validatedBrand) {
properties[HeapConstants.KeyProductBrand] = validatedBrand;
}

let validatedCategory = validateHeapPropertyValue(product.Category);
var validatedCategory = validateHeapPropertyValue(product.Category);
if (validatedCategory) {
properties[HeapConstants.KeyProductCategory] = validatedCategory;
}

event.Name = HeapConstants.EventNameItem;
event.Properties = properties;

let productSku = validatedSku;
return [event, productSku];
var productSku = validatedSku;
return {event: event, sku: productSku};
}

function buildPromotionEvent(promotion) {
let event = {};
let properties = promotion.Attributes ? promotion.Attributes : {};
function buildPromotionItemEvent(promotion) {
var event = {};
var properties = promotion.Attributes ? promotion.Attributes : {};

let validatedPromotionValues = {
var validatedPromotionValues = {
KeyPromotionCreative: validateHeapPropertyValue(promotion.Creative),
KeyPromotionId: validateHeapPropertyValue(promotion.Id),
KeyPromotionPosition: validateHeapPropertyValue(promotion.Position),
}

Object.keys(validatedPromotionValues).forEach((key) => {
let value = validatedPromotionValues[key];
for (var i = 0; i < Object.keys(validatedPromotionValues).length; i++) {
var key = Object.keys(validatedPromotionValues)[i];
var value = validatedPromotionValues[key];

if (value === undefined || key === undefined) {
return;
}

let constKey = HeapConstants[key];
var constKey = HeapConstants[key];
properties[constKey] = value;
})
}
event.Name = HeapConstants.EventNameItem;
event.Properties = properties;
let promotionId = validatedPromotionValues.KeyPromotionId;
var promotionId = validatedPromotionValues.KeyPromotionId;

return [event, promotionId];
return {event: event, id: promotionId};
}

function validateHeapPropertyValue(value){
Expand Down
29 changes: 15 additions & 14 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ describe('Heap Forwarder', function () {
});

describe('CommerceEventProcessing', function () {
const product1 = {
var product1 = {
Name: 'iphone',
Sku: 'iphoneSKU',
Price: 999,
Expand All @@ -339,7 +339,7 @@ describe('Heap Forwarder', function () {
},
};

const product2 = {
var product2 = {
Name: 'galaxy',
Sku: 'galaxySKU',
Price: 799,
Expand All @@ -356,7 +356,7 @@ describe('Heap Forwarder', function () {
},
};

const purchaseEvent = {
var purchaseEvent = {
EventName: 'Test Purchase Event',
EventDataType: MessageType.Commerce,
EventCategory: EventType.ProductPurchase,
Expand All @@ -375,7 +375,7 @@ describe('Heap Forwarder', function () {
}
};

const impressionEvent = {
var impressionEvent = {
EventName: 'eCommerce - Impression',
EventDataType: 16,
EventCategory: 22,
Expand All @@ -397,7 +397,7 @@ describe('Heap Forwarder', function () {
],
};

const promotionEvent = {
var promotionEvent = {
EventName: 'eCommerce - PromotionClick',
EventDataType: 16,
CurrencyCode: null,
Expand All @@ -418,6 +418,7 @@ describe('Heap Forwarder', function () {
],
},
};

var validateProductProperties = function (properties) {
if (properties.product_name === 'iphone') {
properties.prod1AttrKey1.should.equal('value1');
Expand Down Expand Up @@ -452,9 +453,9 @@ describe('Heap Forwarder', function () {
window.heap.trackCalled.should.equal(true);
window.heap.events.length.should.equal(3);

for (let i = 0; i < window.heap.events.length; i++) {
let eventName = window.heap.events[i];
let properties = window.heap.eventProperties[i];
for (var i = 0; i < window.heap.events.length; i++) {
var eventName = window.heap.events[i];
var properties = window.heap.eventProperties[i];

if (eventName === 'Item') {
validateProductProperties(properties);
Expand All @@ -481,9 +482,9 @@ describe('Heap Forwarder', function () {
window.heap.trackCalled.should.equal(true);
window.heap.events.length.should.equal(6);

for (let i = 0; i < window.heap.events.length; i++) {
let eventName = window.heap.events[i];
let properties = window.heap.eventProperties[i];
for (var i = 0; i < window.heap.events.length; i++) {
var eventName = window.heap.events[i];
var properties = window.heap.eventProperties[i];

if (eventName === 'Item') {
validateProductProperties(properties);
Expand All @@ -506,9 +507,9 @@ describe('Heap Forwarder', function () {

mParticle.forwarder.process(promotionEvent);

for (let i = 0; i < window.heap.events.length; i++) {
let eventName = window.heap.events[i];
let properties = window.heap.eventProperties[i];
for (var i = 0; i < window.heap.events.length; i++) {
var eventName = window.heap.events[i];
var properties = window.heap.eventProperties[i];

if (eventName === 'Item') {
properties.creative.should.be.ok;
Expand Down

0 comments on commit 15bcc63

Please sign in to comment.