Skip to content

Commit

Permalink
style: Clean up lint
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Jul 16, 2024
1 parent ad855a2 commit b05d76c
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 150 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"before": true,
"beforeEach": true,
"after": true,
"Leanplum": true
"heap": true
},
"extends": ["plugin:prettier/recommended", "eslint:recommended"],
"plugins": ["prettier"],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build": "ENVIRONMENT=production rollup --config rollup.config.js",
"build:test": "rollup --config rollup.test.config.js",
"lint": "eslint src/ test/src/",
"lint:fix": "eslint src/ test/src/ --fix",
"watch": "ENVIRONMENT=production rollup --config rollup.config.js -w",
"test": "npm run build && npm run build:test && DEBUG=false karma start test/karma.config.js",
"test:debug": "npm run build && npm run build:test && DEBUG=true karma start test/karma.config.js",
Expand Down
120 changes: 60 additions & 60 deletions src/commerce-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,42 @@ var ProductActionTypes = {
Refund: 17,
RemoveFromCart: 11,
ViewDetail: 15,
}

var ProductActionNames = {
0: "Unknown",
1: "Add To Cart",
2: "Remove From Cart",
3: "Checkout",
4: "Checkout Option",
5: "Click",
6: "View Detail",
7: "Purchase",
8: "Refund",
9: "Add To Wishlist",
10: "Remove From Wishlist",
}
};

var PromotionType = {
PromotionClick: 19,
PromotionView: 18,
}
};

var PromotionTypeNames = {
19: "Click",
18: "View",
}
19: 'Click',
18: 'View',
};
var HeapConstants = {
EventNameItem: "Item",
EventNameProductAction: "Product Action Event",
EventNameProductActionPart: "Product Action: ",
EventNamePromotionPart: "Promotion: ",
EventNameImpression: "Impression Event",
EventNameItem: 'Item',
EventNameProductAction: 'Product Action Event',
EventNameProductActionPart: 'Product Action: ',
EventNamePromotionPart: 'Promotion: ',
EventNameImpression: 'Impression Event',
MaxPropertyLength: 1023,
KeyProductName: "product_name",
KeyProductPrice: "product_price",
KeyProductQuantity: "product_quantity",
KeyProductTotalProductAmount: "total_product_amount",
KeyProductSku: "product_id",
KeyProductBrand: "product_brand",
KeyProductCategory: "product_category",
KeyProductSkus: "skus",
KeyPromotionCreative: "creative",
KeyPromotionId: "id",
KeyPromotionPosition: "position",
}
KeyProductName: 'product_name',
KeyProductPrice: 'product_price',
KeyProductQuantity: 'product_quantity',
KeyProductTotalProductAmount: 'total_product_amount',
KeyProductSku: 'product_id',
KeyProductBrand: 'product_brand',
KeyProductCategory: 'product_category',
KeyProductSkus: 'skus',
KeyPromotionCreative: 'creative',
KeyPromotionId: 'id',
KeyPromotionPosition: 'position',
};

function CommerceHandler(common) {
this.common = common || {};
}

CommerceHandler.prototype.logCommerceEvent = function(event) {
CommerceHandler.prototype.logCommerceEvent = function (event) {
/*
Sample ecommerce event schema:
{
Expand Down Expand Up @@ -128,11 +114,10 @@ CommerceHandler.prototype.logCommerceEvent = function(event) {
break;
default:
events = buildProductActionEvents(event);
};
}

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

Expand All @@ -144,23 +129,28 @@ function buildImpressionEvents(event) {
var impression = event.ProductImpressions[i];
var productSkus = [];
if (impression.ProductList.length > 0) {
for(var j = 0; j < impression.ProductList.length; j++) {
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) {
if (eventObject.sku) {
productSkus.push(eventObject.sku);
}
}
}

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


return events;
};
}

function buildPromotionEvents(event) {
var events = [];
Expand All @@ -171,13 +161,17 @@ function buildPromotionEvents(event) {
var eventObject = buildPromotionItemEvent(promotion);
events.push(eventObject.event);

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

Expand All @@ -203,7 +197,8 @@ function buildProductActionEvents(event) {
actionEventName = HeapConstants.EventNameProductAction;
} else {
var productActionKey = event.ProductAction.ProductActionType;
actionEventName = HeapConstants.EventNameProductActionPart + productActionKey;
actionEventName =
HeapConstants.EventNameProductActionPart + productActionKey;
}

events.push(buildActionEvent(event, actionEventName, productSkus));
Expand All @@ -212,9 +207,10 @@ function buildProductActionEvents(event) {
}

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

function buildItemEvent(product) {
Expand All @@ -236,9 +232,12 @@ function buildItemEvent(product) {
properties[HeapConstants.KeyProductQuantity] = validatedQuantity;
}

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

var validatedSku = validateHeapPropertyValue(product.Sku);
Expand All @@ -259,18 +258,19 @@ function buildItemEvent(product) {
event.Name = HeapConstants.EventNameItem;
event.Properties = properties;

return {event: event, sku: validatedSku};
return { event: event, sku: validatedSku };
}

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

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

var validatedPromotionKeys = Object.keys(validatedPromotionValues);
for (var i = 0; i < validatedPromotionKeys.length; i++) {
Expand All @@ -288,17 +288,17 @@ function buildPromotionItemEvent(promotion) {
event.Properties = properties;
var promotionId = validatedPromotionValues.KeyPromotionId;

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

function validateHeapPropertyValue(value){
if (typeof value === "boolean" || typeof value === "number") {
function validateHeapPropertyValue(value) {
if (typeof value === 'boolean' || typeof value === 'number') {
return value;
}

if (value === undefined || value === null) {
return value;
} else if (value.length > HeapConstants.MaxPropertyLength){
} else if (value.length > HeapConstants.MaxPropertyLength) {
return value.substring(0, HeapConstants.MaxPropertyLength);
} else {
return value;
Expand Down
4 changes: 2 additions & 2 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ function Common() {}

Common.prototype.exampleMethod = function () {
return 'I am an example';
}
};

module.exports = Common;
module.exports = Common;
22 changes: 2 additions & 20 deletions src/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,14 @@ A non-ecommerce event has the following schema:
function EventHandler(common) {
this.common = common || {};
}
EventHandler.prototype.logEvent = function(event) {
var ignoredEvents = [
'click',
'change',
'submit'
];
EventHandler.prototype.logEvent = function (event) {
var ignoredEvents = ['click', 'change', 'submit'];

if (ignoredEvents.includes(event.EventName.toLowerCase())) {
return;
}

window.heap.track(event.EventName, event.EventAttributes);
};
EventHandler.prototype.logError = function(event) {
// The schema for a logError event is the same, but noteworthy differences are as follows:
// {
// EventAttributes: {m: 'name of error passed into MP', s: "Error", t: 'stack trace in string form if applicable'},
// EventName: "Error"
// }
};
EventHandler.prototype.logPageView = function(event) {
/* The schema for a logPagView event is the same, but noteworthy differences are as follows:
{
EventAttributes: {hostname: "www.google.com", title: 'Test Page'}, // These are event attributes only if no additional event attributes are explicitly provided to mParticle.logPageView(...)
}
*/
};

module.exports = EventHandler;
27 changes: 8 additions & 19 deletions src/identity-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,31 @@ For more userIdentity types, see https://docs.mparticle.com/developers/sdk/web/i
function IdentityHandler(common) {
this.common = common || {};
}
IdentityHandler.prototype.onUserIdentified = function(mParticleUser) {
IdentityHandler.prototype.onUserIdentified = function (mParticleUser) {
if (!mParticleUser && !mParticleUser.getUserIdentities()) {
return;
}

var identitiesObject = mParticleUser.getUserIdentities();
var identity = identitiesObject.userIdentities[this.common.userIdentificationType];
var identity =
identitiesObject.userIdentities[this.common.userIdentificationType];

if (identity) {
window.heap.identify(identity);
}
};
IdentityHandler.prototype.onIdentifyComplete = function(
mParticleUser,
identityApiRequest
) {};
IdentityHandler.prototype.onLoginComplete = function(
mParticleUser,
identityApiRequest
) {};
IdentityHandler.prototype.onLogoutComplete = function(
mParticleUser,
identityApiRequest
) {
IdentityHandler.prototype.onIdentifyComplete = function () {};
IdentityHandler.prototype.onLoginComplete = function () {};
IdentityHandler.prototype.onLogoutComplete = function () {
window.heap.resetIdentity();
};
IdentityHandler.prototype.onModifyComplete = function(
mParticleUser,
identityApiRequest
) {};
IdentityHandler.prototype.onModifyComplete = function () {};

/* In previous versions of the mParticle web SDK, setting user identities on
kits is only reachable via the onSetUserIdentity method below. We recommend
filling out `onSetUserIdentity` for maximum compatibility
*/
IdentityHandler.prototype.onSetUserIdentity = function(
IdentityHandler.prototype.onSetUserIdentity = function (
forwarderSettings,
id,
type
Expand Down
15 changes: 7 additions & 8 deletions src/initialization.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var renderSnippet = function (appId) {
// prettier-ignore
window.heapReadyCb=window.heapReadyCb||[],window.heap=window.heap||[],heap.load=function(e,t){window.heap.envId=e,window.heap.clientConfig=t=t||{},window.heap.clientConfig.shouldFetchServerConfig=!1;var a=document.createElement("script");a.type="text/javascript",a.async=!0,a.src="https://cdn.us.heap-api.com/config/"+e+"/heap_config.js";var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(a,r);var n=["init","startTracking","stopTracking","track","resetIdentity","identify","getSessionId","getUserId","getIdentity","addUserProperties","addEventProperties","removeEventProperty","clearEventProperties","addAccountProperties","addAdapter","addTransformer","addTransformerFn","onReady","addPageviewProperties","removePageviewProperty","clearPageviewProperties","trackPageview"],i=function(e){return function(){var t=Array.prototype.slice.call(arguments,0);window.heapReadyCb.push({name:e,fn:function(){heap[e]&&heap[e].apply(heap,t)}})}};for(var p=0;p<n.length;p++)heap[n[p]]=i(n[p])};
heap.load(appId);
}
};
var initialization = {
name: 'Heap',
moduleId: 31,
Expand All @@ -21,20 +22,18 @@ var initialization = {
processEvent,
eventQueue,
isInitialized,
common,
appVersion,
appName,
customFlags,
clientId
common
) {
/* `forwarderSettings` contains your SDK specific settings such as apiKey that your customer needs in order to initialize your SDK properly */
if (!testMode) {
/* Load your Web SDK here using a variant of your snippet from your readme that your customers would generally put into their <head> tags
Generally, our integrations create script tags and append them to the <head>. Please follow the following format as a guide:
*/
common.userIdentificationType = forwarderSettings.userIdentificationType
common.userIdentificationType =
forwarderSettings.userIdentificationType;

var forwardWebRequestsServerSide = forwarderSettings.forwardWebRequestsServerSide === 'True';
var forwardWebRequestsServerSide =
forwarderSettings.forwardWebRequestsServerSide === 'True';
common.forwardWebRequestsServerSide = forwardWebRequestsServerSide;
if (!forwardWebRequestsServerSide) {
if (!window.heap) {
Expand Down
8 changes: 2 additions & 6 deletions src/session-handler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
var sessionHandler = {
onSessionStart: function(event) {

},
onSessionEnd: function(event) {

}
onSessionStart: function () {},
onSessionEnd: function () {},
};

module.exports = sessionHandler;
Loading

0 comments on commit b05d76c

Please sign in to comment.