From b05d76c2e440b901bad0a430ebbfb7dd1cf928d5 Mon Sep 17 00:00:00 2001 From: Alexander Sapountzis Date: Tue, 16 Jul 2024 10:46:00 -0400 Subject: [PATCH] style: Clean up lint --- .eslintrc | 2 +- package.json | 1 + src/commerce-handler.js | 120 +++++++++++++++++----------------- src/common.js | 4 +- src/event-handler.js | 22 +------ src/identity-handler.js | 27 +++----- src/initialization.js | 15 ++--- src/session-handler.js | 8 +-- src/user-attribute-handler.js | 16 +---- test/src/tests.js | 31 ++++----- 10 files changed, 96 insertions(+), 150 deletions(-) diff --git a/.eslintrc b/.eslintrc index 5f5cc11..df73644 100644 --- a/.eslintrc +++ b/.eslintrc @@ -16,7 +16,7 @@ "before": true, "beforeEach": true, "after": true, - "Leanplum": true + "heap": true }, "extends": ["plugin:prettier/recommended", "eslint:recommended"], "plugins": ["prettier"], diff --git a/package.json b/package.json index 1aa084f..3d4015a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commerce-handler.js b/src/commerce-handler.js index a449637..830e6eb 100644 --- a/src/commerce-handler.js +++ b/src/commerce-handler.js @@ -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: { @@ -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); } }; @@ -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 = []; @@ -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; } @@ -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)); @@ -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) { @@ -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); @@ -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++) { @@ -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; diff --git a/src/common.js b/src/common.js index 11f4d4e..a62e0d7 100644 --- a/src/common.js +++ b/src/common.js @@ -2,6 +2,6 @@ function Common() {} Common.prototype.exampleMethod = function () { return 'I am an example'; -} +}; -module.exports = Common; \ No newline at end of file +module.exports = Common; diff --git a/src/event-handler.js b/src/event-handler.js index aea6b0a..b536b0e 100644 --- a/src/event-handler.js +++ b/src/event-handler.js @@ -16,12 +16,8 @@ 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; @@ -29,19 +25,5 @@ EventHandler.prototype.logEvent = function(event) { 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; diff --git a/src/identity-handler.js b/src/identity-handler.js index 62f3b99..57af87f 100644 --- a/src/identity-handler.js +++ b/src/identity-handler.js @@ -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 diff --git a/src/initialization.js b/src/initialization.js index aaac2f5..f1b1db6 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -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 tags Generally, our integrations create script tags and append them to the . 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) { diff --git a/src/session-handler.js b/src/session-handler.js index 96d5bf4..0d65be7 100644 --- a/src/session-handler.js +++ b/src/session-handler.js @@ -1,10 +1,6 @@ var sessionHandler = { - onSessionStart: function(event) { - - }, - onSessionEnd: function(event) { - - } + onSessionStart: function () {}, + onSessionEnd: function () {}, }; module.exports = sessionHandler; diff --git a/src/user-attribute-handler.js b/src/user-attribute-handler.js index d00a122..cb187d5 100644 --- a/src/user-attribute-handler.js +++ b/src/user-attribute-handler.js @@ -10,18 +10,11 @@ For any additional methods, see http://docs.mparticle.com/developers/sdk/javascr function UserAttributeHandler(common) { this.common = common || {}; } -UserAttributeHandler.prototype.onRemoveUserAttribute = function( - key, - mParticleUser -) { +UserAttributeHandler.prototype.onRemoveUserAttribute = function (key) { delete this.common.userAttributes[key]; window.heap.addUserProperties(this.common.userAttributes); }; -UserAttributeHandler.prototype.onSetUserAttribute = function( - key, - value, - mParticleUser -) { +UserAttributeHandler.prototype.onSetUserAttribute = function (key, value) { if (!this.common.userAttributes) { this.common.userAttributes = {}; } @@ -29,10 +22,5 @@ UserAttributeHandler.prototype.onSetUserAttribute = function( this.common.userAttributes[key] = value; window.heap.addUserProperties(this.common.userAttributes); }; -UserAttributeHandler.prototype.onConsentStateUpdated = function( - oldState, - newState, - mParticleUser -) {}; module.exports = UserAttributeHandler; diff --git a/test/src/tests.js b/test/src/tests.js index 66f970e..fbd7cd3 100644 --- a/test/src/tests.js +++ b/test/src/tests.js @@ -118,11 +118,11 @@ describe('Heap Forwarder', function () { this.getIdentity = function () { return self.identity; - } + }; this.resetIdentity = function () { self.identity = null; - } + }; this.addUserProperties = function (properties) { self.addUserPropertiesCalled = true; @@ -267,7 +267,7 @@ describe('Heap Forwarder', function () { mParticle.forwarder.onLogoutComplete(); - expect(window.heap.getIdentity()).to.be.null + expect(window.heap.getIdentity()).to.be.null; done(); }); }); @@ -339,43 +339,34 @@ describe('Heap Forwarder', function () { }, }; - var purchaseEvent = { + var purchaseEvent = { EventName: 'Test Purchase Event', EventDataType: MessageType.Commerce, EventCategory: EventType.ProductPurchase, ProductAction: { ProductActionType: ProductActionType.Purchase, - ProductList: [ - product, - product, - ], + ProductList: [product, product], TransactionId: 123, Affiliation: 'my-affiliation', TotalAmount: 450, TaxAmount: 40, ShippingAmount: 10, - CouponCode: null - } + CouponCode: null, + }, }; - var impressionEvent = { + var impressionEvent = { EventName: 'eCommerce - Impression', EventDataType: 16, EventCategory: 22, ProductImpressions: [ { ProductImpressionList: 'Suggested Products List1', - ProductList: [ - product, - product, - ], + ProductList: [product, product], }, { ProductImpressionList: 'Suggested Products List2', - ProductList: [ - product, - product, - ], + ProductList: [product, product], }, ], }; @@ -391,7 +382,7 @@ describe('Heap Forwarder', function () { { Id: 'my_promo_1', Creative: 'sale_banner_1', - Position: 'top' + Position: 'top', }, { Id: 'my_promo_2',