diff --git a/src/apiClient.ts b/src/apiClient.ts index 3a6695ad..cae4e45a 100644 --- a/src/apiClient.ts +++ b/src/apiClient.ts @@ -135,7 +135,7 @@ export default function APIClient( this.queueEventForBatchUpload(event); } - if (event.EventName !== Types.MessageType.AppStateTransition) { + if (event.EventName !== Types.MessageType.AppStateTransition.toString()) { if (kitBlocker && kitBlocker.kitBlockingEnabled) { event = kitBlocker.createBlockedEvent(event); } diff --git a/src/identity-user-interfaces.ts b/src/identity-user-interfaces.ts index 497e732a..44e34594 100644 --- a/src/identity-user-interfaces.ts +++ b/src/identity-user-interfaces.ts @@ -1,6 +1,6 @@ import { AllUserAttributes, MPID, Product, User } from '@mparticle/web-sdk'; import { SDKIdentityTypeEnum } from './identity.interfaces'; -import { MessageType } from './types.interfaces'; +import { MessageType } from './types'; import { BaseEvent } from './sdkRuntimeModels'; import Constants from './constants'; const { HTTPCodes } = Constants; @@ -58,7 +58,7 @@ export interface ISDKUserIdentityChangeData { } export interface IUserIdentityChangeEvent extends BaseEvent { - messageType: MessageType.UserIdentityChange; + messageType: typeof MessageType.UserIdentityChange; userIdentityChanges: ISDKUserIdentityChanges; } @@ -75,7 +75,7 @@ export interface ISDKUserAttributeChangeData { } export interface IUserAttributeChangeEvent extends BaseEvent { - messageType: MessageType.UserAttributeChange; + messageType: typeof MessageType.UserAttributeChange; userAttributeChanges: ISDKUserAttributeChangeData; } diff --git a/src/identity.js b/src/identity.js index 3abebed7..5dbb973b 100644 --- a/src/identity.js +++ b/src/identity.js @@ -17,7 +17,7 @@ import { isObject, } from './utils'; import { hasMPIDAndUserLoginChanged, hasMPIDChanged } from './user-utils'; -import { getNewIdentitiesByName } from './type-utils'; +import { IdentityType } from './types'; import { processReadyQueue } from './pre-init-utils'; export default function Identity(mpInstance) { @@ -1623,7 +1623,7 @@ export default function Identity(mpInstance) { self.setForwarderCallbacks(newUser, method); } - const newIdentitiesByName = getNewIdentitiesByName( + const newIdentitiesByName = IdentityType.getNewIdentitiesByName( newIdentitiesByType ); diff --git a/src/kitFilterHelper.ts b/src/kitFilterHelper.ts index 06e8ba7e..0bbb44ba 100644 --- a/src/kitFilterHelper.ts +++ b/src/kitFilterHelper.ts @@ -1,17 +1,17 @@ -import { generateHash } from "./utils"; +import { generateHash, valueof } from "./utils"; // TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5381 -import { EventTypeEnum, IdentityType } from "./types.interfaces"; +import { EventType, IdentityType } from "./types"; export default class KitFilterHelper { - static hashEventType(eventType: EventTypeEnum): number { + static hashEventType(eventType: valueof): number { return generateHash(eventType as unknown as string); }; - static hashEventName(eventName: string, eventType: EventTypeEnum): number { + static hashEventName(eventName: string, eventType: valueof): number { return generateHash(eventType + eventName); }; - static hashEventAttributeKey(eventType: EventTypeEnum, eventName: string, customAttributeName: string): number { + static hashEventAttributeKey(eventType: valueof, eventName: string, customAttributeName: string): number { return generateHash(eventType + eventName + customAttributeName); } @@ -21,7 +21,7 @@ export default class KitFilterHelper { // User Identities are not actually hashed, this method is named this way to // be consistent with the filter class. UserIdentityType is also a number - static hashUserIdentity(userIdentity: IdentityType): IdentityType { + static hashUserIdentity(userIdentity: typeof IdentityType): typeof IdentityType { return userIdentity; } diff --git a/src/sdkRuntimeModels.ts b/src/sdkRuntimeModels.ts index ae3e29b5..2b0eea95 100644 --- a/src/sdkRuntimeModels.ts +++ b/src/sdkRuntimeModels.ts @@ -29,7 +29,7 @@ import { IdentityCallback, ISDKUserAttributes, } from './identity-user-interfaces'; -import { IIdentityType } from './types.interfaces'; +import { IdentityType } from './types'; import IntegrationCapture from './integrationCapture'; import { INativeSdkHelpers } from './nativeSdkHelpers.interfaces'; import { ICookieSyncManager, IPixelConfiguration } from './cookieSyncManager.interfaces'; @@ -154,7 +154,7 @@ interface IEvents { export interface MParticleWebSDK { addForwarder(mockForwarder: MPForwarder): void; _IntegrationCapture: IntegrationCapture; - IdentityType: IIdentityType; + IdentityType: typeof IdentityType; _Identity: IIdentity; Identity: SDKIdentityApi; Logger: SDKLoggerApi; diff --git a/src/sideloadedKit.ts b/src/sideloadedKit.ts index 3923b886..5bc6d1e6 100644 --- a/src/sideloadedKit.ts +++ b/src/sideloadedKit.ts @@ -6,22 +6,23 @@ import { IKitFilterSettings, } from './configAPIClient'; import { UnregisteredKit } from './forwarders.interfaces'; -import { EventTypeEnum, IdentityType } from './types.interfaces'; +import { EventType, IdentityType } from './types'; +import { valueof } from './utils'; export interface IMPSideloadedKit { kitInstance: UnregisteredKit; filterDictionary: IKitFilterSettings; - addEventTypeFilter(eventType: EventTypeEnum): void; - addEventNameFilter(eventType: EventTypeEnum, eventName: string): void; + addEventTypeFilter(eventType: valueof): void; + addEventNameFilter(eventType: valueof, eventName: string): void; addEventAttributeFilter( - eventType: EventTypeEnum, + eventType: valueof, eventName: string, customAttributeKey: string ): void; addScreenNameFilter(screenName: string): void; addScreenAttributeFilter(screenName: string, screenAttribute: string): void; - addUserIdentityFilter(userIdentity: IdentityType): void; + addUserIdentityFilter(userIdentity: typeof IdentityType): void; addUserAttributeFilter(userAttributeKey: string): void; } @@ -57,13 +58,13 @@ export default class MPSideloadedKit implements IMPSideloadedKit{ this.kitInstance = unregisteredKitInstance; } - public addEventTypeFilter(eventType: EventTypeEnum): void { + public addEventTypeFilter(eventType: valueof): void { const hashedEventType = KitFilterHelper.hashEventType(eventType); this.filterDictionary.eventTypeFilters.push(hashedEventType); } public addEventNameFilter( - eventType: EventTypeEnum, + eventType: valueof, eventName: string ): void { const hashedEventName = KitFilterHelper.hashEventName( @@ -74,7 +75,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{ } public addEventAttributeFilter( - eventType: EventTypeEnum, + eventType: valueof, eventName: string, customAttributeKey: string ): void { @@ -89,7 +90,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{ public addScreenNameFilter(screenName: string): void { const hashedScreenName = KitFilterHelper.hashEventName( screenName, - EventTypeEnum.Unknown + EventType.Unknown, ); this.filterDictionary.screenNameFilters.push(hashedScreenName); } @@ -99,7 +100,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{ screenAttribute: string ): void { const hashedScreenAttribute = KitFilterHelper.hashEventAttributeKey( - EventTypeEnum.Unknown, + EventType.Unknown, screenName, screenAttribute ); @@ -108,7 +109,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{ ); } - public addUserIdentityFilter(userIdentity: IdentityType): void { + public addUserIdentityFilter(userIdentity: typeof IdentityType): void { const hashedIdentityType = KitFilterHelper.hashUserIdentity( userIdentity ); diff --git a/src/type-utils.ts b/src/type-utils.ts deleted file mode 100644 index a20a54f4..00000000 --- a/src/type-utils.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { CommerceEventType, EventTypeEnum, IdentityType } from './types.interfaces'; -import { parseNumber } from './utils'; - -export interface IIdentitiesByType { - [key: number]: string; -} - -export function getNewIdentitiesByName(newIdentitiesByType: IIdentitiesByType) { - const newIdentitiesByName = {}; - - for (var key in newIdentitiesByType) { - const identityNameKey = getIdentityName(parseNumber(key)); - newIdentitiesByName[identityNameKey] = newIdentitiesByType[key]; - } - - return newIdentitiesByName; -} - -export function getIdentityName(identityType: IdentityType): string | null { - switch (identityType) { - case IdentityType.Other: - return 'other'; - case IdentityType.CustomerId: - return 'customerid'; - case IdentityType.Facebook: - return 'facebook'; - case IdentityType.Twitter: - return 'twitter'; - case IdentityType.Google: - return 'google'; - case IdentityType.Microsoft: - return 'microsoft'; - case IdentityType.Yahoo: - return 'yahoo'; - case IdentityType.Email: - return 'email'; - case IdentityType.FacebookCustomAudienceId: - return 'facebookcustomaudienceid'; - case IdentityType.Other2: - return 'other2'; - case IdentityType.Other3: - return 'other3'; - case IdentityType.Other4: - return 'other4'; - case IdentityType.Other5: - return 'other5'; - case IdentityType.Other6: - return 'other6'; - case IdentityType.Other7: - return 'other7'; - case IdentityType.Other8: - return 'other8'; - case IdentityType.Other9: - return 'other9'; - case IdentityType.Other10: - return 'other10'; - case IdentityType.MobileNumber: - return 'mobile_number'; - case IdentityType.PhoneNumber2: - return 'phone_number_2'; - case IdentityType.PhoneNumber3: - return 'phone_number_3'; - default: - return null; - } -} - -export function getName(id: EventTypeEnum | CommerceEventType): string { - switch (id) { - case EventTypeEnum.Unknown: - return 'Unknown'; - case EventTypeEnum.Navigation: - return 'Navigation'; - case EventTypeEnum.Location: - return 'Location'; - case EventTypeEnum.Search: - return 'Search'; - case EventTypeEnum.Transaction: - return 'Transaction'; - case EventTypeEnum.UserContent: - return 'User Content'; - case EventTypeEnum.UserPreference: - return 'User Preference'; - case EventTypeEnum.Social: - return 'Social'; - case CommerceEventType.ProductAddToCart: - return 'Product Added to Cart'; - case CommerceEventType.ProductAddToWishlist: - return 'Product Added to Wishlist'; - case CommerceEventType.ProductCheckout: - return 'Product Checkout'; - case CommerceEventType.ProductCheckoutOption: - return 'Product Checkout Options'; - case CommerceEventType.ProductClick: - return 'Product Click'; - case CommerceEventType.ProductImpression: - return 'Product Impression'; - case CommerceEventType.ProductPurchase: - return 'Product Purchased'; - case CommerceEventType.ProductRefund: - return 'Product Refunded'; - case CommerceEventType.ProductRemoveFromCart: - return 'Product Removed From Cart'; - case CommerceEventType.ProductRemoveFromWishlist: - return 'Product Removed from Wishlist'; - case CommerceEventType.ProductViewDetail: - return 'Product View Details'; - case CommerceEventType.PromotionClick: - return 'Promotion Click'; - case CommerceEventType.PromotionView: - return 'Promotion View'; - default: - return 'Other'; - } -} - -export function isValid(identityType: IdentityType): boolean { - if (typeof identityType === 'number') { - for (var prop in IdentityType) { - if (IdentityType.hasOwnProperty(prop)) { - if (IdentityType[prop] === identityType as unknown as string) { - return true; - } - } - } - } - - return false; -} \ No newline at end of file diff --git a/src/types.interfaces.ts b/src/types.interfaces.ts deleted file mode 100644 index e4c9efc5..00000000 --- a/src/types.interfaces.ts +++ /dev/null @@ -1,87 +0,0 @@ -// TODO: Rename to Event Type? -// TODO: Add actual values for each enum -export enum EventTypeEnum { - Unknown, - Navigation, - Location, - Search, - Transaction, - UserContent, - UserPreference, - Social, - Other, - Media, -} - -export enum EventType { - Unknown = 0, - Navigation = 1, - Location = 2, - Search = 3, - Transaction = 4, - UserContent = 5, - UserPreference = 6, - Social = 7, - Other = 8, - Media = 9, -} - -// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5403 -export enum MessageType { - SessionStart = 1, - SessionEnd = 2, - PageView = 3, - PageEvent = 4, - CrashReport = 5, - OptOut = 6, - AppStateTransition = 10, - Profile = 14, - Commerce = 16, - UserAttributeChange = 17, - UserIdentityChange = 18, - Media = 20, -}; - -export enum IdentityType { - Other = 0, - CustomerId = 1, - Facebook = 2, - Twitter = 3, - Google = 4, - Microsoft = 5, - Yahoo = 6, - Email = 7, - FacebookCustomAudienceId = 9, - Other2 = 10, - Other3 = 11, - Other4 = 12, - Other5 = 13, - Other6 = 14, - Other7 = 15, - Other8 = 16, - Other9 = 17, - Other10 = 18, - MobileNumber = 19, - PhoneNumber2 = 20, - PhoneNumber3 = 21, -} - -export enum CommerceEventType { - ProductAddToCart = 10, - ProductRemoveFromCart = 11, - ProductCheckout = 12, - ProductCheckoutOption = 13, - ProductClick = 14, - ProductViewDetail = 15, - ProductPurchase = 16, - ProductRefund = 17, - PromotionView = 18, - PromotionClick = 19, - ProductAddToWishlist = 20, - ProductRemoveFromWishlist = 21, - ProductImpression = 22, -} - -export interface IIdentityType { - getIdentityType(identityType: string): IdentityType | null; -} diff --git a/src/types.js b/src/types.js deleted file mode 100644 index 47f5aba0..00000000 --- a/src/types.js +++ /dev/null @@ -1,278 +0,0 @@ -import Constants from './constants'; -import { getIdentityName, getName, isValid } from './type-utils'; - -var MessageType = { - SessionStart: 1, - SessionEnd: 2, - PageView: 3, - PageEvent: 4, - CrashReport: 5, - OptOut: 6, - AppStateTransition: 10, - Profile: 14, - Commerce: 16, - Media: 20, - UserAttributeChange: 17, - UserIdentityChange: 18, -}; - -// Dictionary that contains MessageTypes that will -// trigger an immediate upload. -var TriggerUploadType = { - [MessageType.Commerce]: 1, - [MessageType.UserIdentityChange]: 1, -}; - -var EventType = { - Unknown: 0, - Navigation: 1, - Location: 2, - Search: 3, - Transaction: 4, - UserContent: 5, - UserPreference: 6, - Social: 7, - Other: 8, - Media: 9, -}; - -EventType.getName = getName; - -// Continuation of enum above, but in seperate object since we don't expose these to end user -var CommerceEventType = { - ProductAddToCart: 10, - ProductRemoveFromCart: 11, - ProductCheckout: 12, - ProductCheckoutOption: 13, - ProductClick: 14, - ProductViewDetail: 15, - ProductPurchase: 16, - ProductRefund: 17, - PromotionView: 18, - PromotionClick: 19, - ProductAddToWishlist: 20, - ProductRemoveFromWishlist: 21, - ProductImpression: 22, -}; - -var IdentityType = { - Other: 0, - CustomerId: 1, - Facebook: 2, - Twitter: 3, - Google: 4, - Microsoft: 5, - Yahoo: 6, - Email: 7, - FacebookCustomAudienceId: 9, - Other2: 10, - Other3: 11, - Other4: 12, - Other5: 13, - Other6: 14, - Other7: 15, - Other8: 16, - Other9: 17, - Other10: 18, - MobileNumber: 19, - PhoneNumber2: 20, - PhoneNumber3: 21, -}; - -IdentityType.isValid = isValid; - -IdentityType.getName = function(identityType) { - switch (identityType) { - case window.mParticle.IdentityType.CustomerId: - return 'Customer ID'; - case window.mParticle.IdentityType.Facebook: - return 'Facebook ID'; - case window.mParticle.IdentityType.Twitter: - return 'Twitter ID'; - case window.mParticle.IdentityType.Google: - return 'Google ID'; - case window.mParticle.IdentityType.Microsoft: - return 'Microsoft ID'; - case window.mParticle.IdentityType.Yahoo: - return 'Yahoo ID'; - case window.mParticle.IdentityType.Email: - return 'Email'; - case window.mParticle.IdentityType.FacebookCustomAudienceId: - return 'Facebook App User ID'; - default: - return 'Other ID'; - } -}; - -IdentityType.getIdentityType = function(identityName) { - switch (identityName) { - case 'other': - return IdentityType.Other; - case 'customerid': - return IdentityType.CustomerId; - case 'facebook': - return IdentityType.Facebook; - case 'twitter': - return IdentityType.Twitter; - case 'google': - return IdentityType.Google; - case 'microsoft': - return IdentityType.Microsoft; - case 'yahoo': - return IdentityType.Yahoo; - case 'email': - return IdentityType.Email; - case 'facebookcustomaudienceid': - return IdentityType.FacebookCustomAudienceId; - case 'other2': - return IdentityType.Other2; - case 'other3': - return IdentityType.Other3; - case 'other4': - return IdentityType.Other4; - case 'other5': - return IdentityType.Other5; - case 'other6': - return IdentityType.Other6; - case 'other7': - return IdentityType.Other7; - case 'other8': - return IdentityType.Other8; - case 'other9': - return IdentityType.Other9; - case 'other10': - return IdentityType.Other10; - case 'mobile_number': - return IdentityType.MobileNumber; - case 'phone_number_2': - return IdentityType.PhoneNumber2; - case 'phone_number_3': - return IdentityType.PhoneNumber3; - default: - return false; - } -}; - -IdentityType.getIdentityName = function(identityType) { - return getIdentityName(identityType); -}; - -var ProductActionType = { - Unknown: 0, - AddToCart: 1, - RemoveFromCart: 2, - Checkout: 3, - CheckoutOption: 4, - Click: 5, - ViewDetail: 6, - Purchase: 7, - Refund: 8, - AddToWishlist: 9, - RemoveFromWishlist: 10, -}; - -ProductActionType.getName = function(id) { - switch (id) { - case ProductActionType.AddToCart: - return 'Add to Cart'; - case ProductActionType.RemoveFromCart: - return 'Remove from Cart'; - case ProductActionType.Checkout: - return 'Checkout'; - case ProductActionType.CheckoutOption: - return 'Checkout Option'; - case ProductActionType.Click: - return 'Click'; - case ProductActionType.ViewDetail: - return 'View Detail'; - case ProductActionType.Purchase: - return 'Purchase'; - case ProductActionType.Refund: - return 'Refund'; - case ProductActionType.AddToWishlist: - return 'Add to Wishlist'; - case ProductActionType.RemoveFromWishlist: - return 'Remove from Wishlist'; - default: - return 'Unknown'; - } -}; - -// these are the action names used by server and mobile SDKs when expanding a CommerceEvent -ProductActionType.getExpansionName = function(id) { - switch (id) { - case ProductActionType.AddToCart: - return 'add_to_cart'; - case ProductActionType.RemoveFromCart: - return 'remove_from_cart'; - case ProductActionType.Checkout: - return 'checkout'; - case ProductActionType.CheckoutOption: - return 'checkout_option'; - case ProductActionType.Click: - return 'click'; - case ProductActionType.ViewDetail: - return 'view_detail'; - case ProductActionType.Purchase: - return 'purchase'; - case ProductActionType.Refund: - return 'refund'; - case ProductActionType.AddToWishlist: - return 'add_to_wishlist'; - case ProductActionType.RemoveFromWishlist: - return 'remove_from_wishlist'; - default: - return 'unknown'; - } -}; - -var PromotionActionType = { - Unknown: 0, - PromotionView: 1, - PromotionClick: 2, -}; - -PromotionActionType.getName = function(id) { - switch (id) { - case PromotionActionType.PromotionView: - return 'view'; - case PromotionActionType.PromotionClick: - return 'click'; - default: - return 'unknown'; - } -}; - -// these are the names that the server and mobile SDKs use while expanding CommerceEvent -PromotionActionType.getExpansionName = function(id) { - switch (id) { - case PromotionActionType.PromotionView: - return 'view'; - case PromotionActionType.PromotionClick: - return 'click'; - default: - return 'unknown'; - } -}; - -var ProfileMessageType = { - Logout: 3, -}; -var ApplicationTransitionType = { - AppInit: 1, -}; - -export default { - MessageType: MessageType, - EventType: EventType, - CommerceEventType: CommerceEventType, - IdentityType: IdentityType, - ProfileMessageType: ProfileMessageType, - ApplicationTransitionType: ApplicationTransitionType, - ProductActionType: ProductActionType, - PromotionActionType: PromotionActionType, - TriggerUploadType: TriggerUploadType, - - // QUESTION: Can we deprecate this? - Environment: Constants.Environment, -}; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 00000000..97d0f712 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,399 @@ +import Constants from './constants'; +import { parseNumber } from './utils'; + +interface IdentitiesByType { + [key: number]: string; +} + +export const MessageType = { + SessionStart: 1 as const, + SessionEnd: 2 as const, + PageView: 3 as const, + PageEvent: 4 as const, + CrashReport: 5 as const, + OptOut: 6 as const, + AppStateTransition: 10 as const, + Profile: 14 as const, + Commerce: 16 as const, + Media: 20 as const, + UserAttributeChange: 17 as const, + UserIdentityChange: 18 as const, +}; + +// Dictionary that contains MessageTypes that will +// trigger an immediate upload. +export const TriggerUploadType = { + [MessageType.Commerce]: 1 as const, + [MessageType.UserIdentityChange]: 1 as const, +}; + +export const EventType = { + Unknown: 0 as const, + Navigation: 1 as const, + Location: 2 as const, + Search: 3 as const, + Transaction: 4 as const, + UserContent: 5 as const, + UserPreference: 6 as const, + Social: 7 as const, + Other: 8 as const, + Media: 9 as const, + + getName(id: number): string { + switch (id) { + case EventType.Unknown: + return 'Unknown'; + case EventType.Navigation: + return 'Navigation'; + case EventType.Location: + return 'Location'; + case EventType.Search: + return 'Search'; + case EventType.Transaction: + return 'Transaction'; + case EventType.UserContent: + return 'User Content'; + case EventType.UserPreference: + return 'User Preference'; + case EventType.Social: + return 'Social'; + case CommerceEventType.ProductAddToCart: + return 'Product Added to Cart'; + case CommerceEventType.ProductAddToWishlist: + return 'Product Added to Wishlist'; + case CommerceEventType.ProductCheckout: + return 'Product Checkout'; + case CommerceEventType.ProductCheckoutOption: + return 'Product Checkout Options'; + case CommerceEventType.ProductClick: + return 'Product Click'; + case CommerceEventType.ProductImpression: + return 'Product Impression'; + case CommerceEventType.ProductPurchase: + return 'Product Purchased'; + case CommerceEventType.ProductRefund: + return 'Product Refunded'; + case CommerceEventType.ProductRemoveFromCart: + return 'Product Removed From Cart'; + case CommerceEventType.ProductRemoveFromWishlist: + return 'Product Removed from Wishlist'; + case CommerceEventType.ProductViewDetail: + return 'Product View Details'; + case CommerceEventType.PromotionClick: + return 'Promotion Click'; + case CommerceEventType.PromotionView: + return 'Promotion View'; + default: + return 'Other'; + } + }, +} as const; + +// Continuation of EventType enum above, but in seperate object since we don't expose these to end user +export const CommerceEventType = { + ProductAddToCart: 10 as const, + ProductRemoveFromCart: 11 as const, + ProductCheckout: 12 as const, + ProductCheckoutOption: 13 as const, + ProductClick: 14 as const, + ProductViewDetail: 15 as const, + ProductPurchase: 16 as const, + ProductRefund: 17 as const, + PromotionView: 18 as const, + PromotionClick: 19 as const, + ProductAddToWishlist: 20 as const, + ProductRemoveFromWishlist: 21 as const, + ProductImpression: 22 as const, +}; + +export const IdentityType = { + Other: 0 as const, + CustomerId: 1 as const, + Facebook: 2 as const, + Twitter: 3 as const, + Google: 4 as const, + Microsoft: 5 as const, + Yahoo: 6 as const, + Email: 7 as const, + FacebookCustomAudienceId: 9 as const, + Other2: 10 as const, + Other3: 11 as const, + Other4: 12 as const, + Other5: 13 as const, + Other6: 14 as const, + Other7: 15 as const, + Other8: 16 as const, + Other9: 17 as const, + Other10: 18 as const, + MobileNumber: 19 as const, + PhoneNumber2: 20 as const, + PhoneNumber3: 21 as const, + + isValid(identityType: number): boolean { + if (typeof identityType === 'number') { + for (var prop in IdentityType) { + if (IdentityType.hasOwnProperty(prop)) { + if (IdentityType[prop] === identityType) { + return true; + } + } + } + } + + return false; + }, + + getName: (identityType: number): string => { + switch (identityType) { + case window.mParticle.IdentityType.CustomerId: + return 'Customer ID'; + case window.mParticle.IdentityType.Facebook: + return 'Facebook ID'; + case window.mParticle.IdentityType.Twitter: + return 'Twitter ID'; + case window.mParticle.IdentityType.Google: + return 'Google ID'; + case window.mParticle.IdentityType.Microsoft: + return 'Microsoft ID'; + case window.mParticle.IdentityType.Yahoo: + return 'Yahoo ID'; + case window.mParticle.IdentityType.Email: + return 'Email'; + case window.mParticle.IdentityType.FacebookCustomAudienceId: + return 'Facebook App User ID'; + default: + return 'Other ID'; + } + }, + + getIdentityType: (identityName: string): typeof IdentityType | boolean => { + switch (identityName) { + case 'other': + return IdentityType.Other; + case 'customerid': + return IdentityType.CustomerId; + case 'facebook': + return IdentityType.Facebook; + case 'twitter': + return IdentityType.Twitter; + case 'google': + return IdentityType.Google; + case 'microsoft': + return IdentityType.Microsoft; + case 'yahoo': + return IdentityType.Yahoo; + case 'email': + return IdentityType.Email; + case 'facebookcustomaudienceid': + return IdentityType.FacebookCustomAudienceId; + case 'other2': + return IdentityType.Other2; + case 'other3': + return IdentityType.Other3; + case 'other4': + return IdentityType.Other4; + case 'other5': + return IdentityType.Other5; + case 'other6': + return IdentityType.Other6; + case 'other7': + return IdentityType.Other7; + case 'other8': + return IdentityType.Other8; + case 'other9': + return IdentityType.Other9; + case 'other10': + return IdentityType.Other10; + case 'mobile_number': + return IdentityType.MobileNumber; + case 'phone_number_2': + return IdentityType.PhoneNumber2; + case 'phone_number_3': + return IdentityType.PhoneNumber3; + default: + return false; + } + }, + + getIdentityName: (identityType): string | null => { + switch (identityType) { + case IdentityType.Other: + return 'other'; + case IdentityType.CustomerId: + return 'customerid'; + case IdentityType.Facebook: + return 'facebook'; + case IdentityType.Twitter: + return 'twitter'; + case IdentityType.Google: + return 'google'; + case IdentityType.Microsoft: + return 'microsoft'; + case IdentityType.Yahoo: + return 'yahoo'; + case IdentityType.Email: + return 'email'; + case IdentityType.FacebookCustomAudienceId: + return 'facebookcustomaudienceid'; + case IdentityType.Other2: + return 'other2'; + case IdentityType.Other3: + return 'other3'; + case IdentityType.Other4: + return 'other4'; + case IdentityType.Other5: + return 'other5'; + case IdentityType.Other6: + return 'other6'; + case IdentityType.Other7: + return 'other7'; + case IdentityType.Other8: + return 'other8'; + case IdentityType.Other9: + return 'other9'; + case IdentityType.Other10: + return 'other10'; + case IdentityType.MobileNumber: + return 'mobile_number'; + case IdentityType.PhoneNumber2: + return 'phone_number_2'; + case IdentityType.PhoneNumber3: + return 'phone_number_3'; + default: + return null; + } + }, + + getNewIdentitiesByName: (newIdentitiesByType: IdentitiesByType) => { + const newIdentitiesByName: IdentitiesByType = {}; + + for (var key in newIdentitiesByType) { + const identityNameKey = IdentityType.getIdentityName( + parseNumber(key) + ); + newIdentitiesByName[identityNameKey] = newIdentitiesByType[key]; + } + + return newIdentitiesByName; + }, +}; + +export const ProductActionType = { + Unknown: 0 as const, + AddToCart: 1 as const, + RemoveFromCart: 2 as const, + Checkout: 3 as const, + CheckoutOption: 4 as const, + Click: 5 as const, + ViewDetail: 6 as const, + Purchase: 7 as const, + Refund: 8 as const, + AddToWishlist: 9 as const, + RemoveFromWishlist: 10 as const, + + getName: (id: number): string => { + switch (id) { + case ProductActionType.AddToCart: + return 'Add to Cart'; + case ProductActionType.RemoveFromCart: + return 'Remove from Cart'; + case ProductActionType.Checkout: + return 'Checkout'; + case ProductActionType.CheckoutOption: + return 'Checkout Option'; + case ProductActionType.Click: + return 'Click'; + case ProductActionType.ViewDetail: + return 'View Detail'; + case ProductActionType.Purchase: + return 'Purchase'; + case ProductActionType.Refund: + return 'Refund'; + case ProductActionType.AddToWishlist: + return 'Add to Wishlist'; + case ProductActionType.RemoveFromWishlist: + return 'Remove from Wishlist'; + default: + return 'Unknown'; + } + }, + + // these are the action names used by server and mobile SDKs when expanding a CommerceEvent + getExpansionName: (id: number) => { + switch (id) { + case ProductActionType.AddToCart: + return 'add_to_cart'; + case ProductActionType.RemoveFromCart: + return 'remove_from_cart'; + case ProductActionType.Checkout: + return 'checkout'; + case ProductActionType.CheckoutOption: + return 'checkout_option'; + case ProductActionType.Click: + return 'click'; + case ProductActionType.ViewDetail: + return 'view_detail'; + case ProductActionType.Purchase: + return 'purchase'; + case ProductActionType.Refund: + return 'refund'; + case ProductActionType.AddToWishlist: + return 'add_to_wishlist'; + case ProductActionType.RemoveFromWishlist: + return 'remove_from_wishlist'; + default: + return 'unknown'; + } + }, +}; + +export const PromotionActionType = { + Unknown: 0 as const, + PromotionView: 1 as const, + PromotionClick: 2 as const, + + getName: (id: number): string => { + switch (id) { + case PromotionActionType.PromotionView: + return 'view'; + case PromotionActionType.PromotionClick: + return 'click'; + default: + return 'unknown'; + } + }, + + // these are the names that the server and mobile SDKs use while expanding CommerceEvent + getExpansionName: (id: number): string => { + switch (id) { + case PromotionActionType.PromotionView: + return 'view'; + case PromotionActionType.PromotionClick: + return 'click'; + default: + return 'unknown'; + } + }, +}; + +export const ProfileMessageType = { + Logout: 3 as const, +}; + +export const ApplicationTransitionType = { + AppInit: 1 as const, +}; + +export default{ + MessageType, + EventType, + CommerceEventType, + IdentityType, + ProfileMessageType, + ApplicationTransitionType, + ProductActionType, + PromotionActionType, + TriggerUploadType, + + Environment: Constants.Environment, +} as const; diff --git a/test/jest/identity.spec.ts b/test/jest/identity.spec.ts index 15fa07ec..210cc586 100644 --- a/test/jest/identity.spec.ts +++ b/test/jest/identity.spec.ts @@ -14,7 +14,7 @@ import { SDKIdentityTypeEnum, IIdentityAPIIdentityChangeData, } from '../../src/identity.interfaces'; -import { MessageType } from '../../src/types.interfaces'; +import { MessageType } from '../../src/types'; describe('Identity', () => { describe('#types', () => { diff --git a/test/jest/kitFilterHelper.spec.ts b/test/jest/kitFilterHelper.spec.ts index e4a5724b..ddf505fc 100644 --- a/test/jest/kitFilterHelper.spec.ts +++ b/test/jest/kitFilterHelper.spec.ts @@ -1,5 +1,5 @@ import KitFilterHelper from "../../src/kitFilterHelper"; -import { EventTypeEnum, IdentityType } from "../../src/types.interfaces"; +import { EventType, IdentityType } from "../../src/types"; import Constants from '../../src/constants'; const { CCPAPurpose } = Constants; @@ -7,41 +7,41 @@ const { CCPAPurpose } = Constants; describe('FilterHashingUtilities', () => { describe('#hashEventType', () => { it('should hash event type Unknown', () => { - const eventTypeUnknownHash = KitFilterHelper.hashEventType(EventTypeEnum.Unknown); + const eventTypeUnknownHash = KitFilterHelper.hashEventType(EventType.Unknown); const expectedUnknownHash = 48; expect(eventTypeUnknownHash).toBe(expectedUnknownHash); }); it('should hash event type Navigation', () => { - const eventTypeNavigationHash = KitFilterHelper.hashEventType(EventTypeEnum.Navigation); + const eventTypeNavigationHash = KitFilterHelper.hashEventType(EventType.Navigation); const expectedNavigationHash = 49; expect(eventTypeNavigationHash).toBe(expectedNavigationHash); }); it('should hash event type Location', () => { - const eventTypeLocationHash = KitFilterHelper.hashEventType(EventTypeEnum.Location); + const eventTypeLocationHash = KitFilterHelper.hashEventType(EventType.Location); const expectedLocationHash = 50; expect(eventTypeLocationHash).toBe(expectedLocationHash); }); it('should hash event type Search', () => { - const eventTypeSearchHash = KitFilterHelper.hashEventType(EventTypeEnum.Search); + const eventTypeSearchHash = KitFilterHelper.hashEventType(EventType.Search); const expectedSearchHash = 51; expect(eventTypeSearchHash).toBe(expectedSearchHash); }); it('should hash event type Transaction', () => { - const eventTypeTransactionHash = KitFilterHelper.hashEventType(EventTypeEnum.Transaction); + const eventTypeTransactionHash = KitFilterHelper.hashEventType(EventType.Transaction); const expectedTransactionHash = 52; expect(eventTypeTransactionHash).toBe(expectedTransactionHash); }); it('should hash event type UserContent', () => { - const eventTypeUserContentHash = KitFilterHelper.hashEventType(EventTypeEnum.UserContent); + const eventTypeUserContentHash = KitFilterHelper.hashEventType(EventType.UserContent); const expectedUserContentHash = 53; expect(eventTypeUserContentHash).toBe(expectedUserContentHash); @@ -49,7 +49,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event type UserPreference', () => { - const eventTypeUserPreferenceHash = KitFilterHelper.hashEventType(EventTypeEnum.UserPreference); + const eventTypeUserPreferenceHash = KitFilterHelper.hashEventType(EventType.UserPreference); const expectedUserPreferenceHash = 54; expect(eventTypeUserPreferenceHash).toBe(expectedUserPreferenceHash); @@ -57,7 +57,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event type Social', () => { - const eventTypeSocialHash = KitFilterHelper.hashEventType(EventTypeEnum.Social); + const eventTypeSocialHash = KitFilterHelper.hashEventType(EventType.Social); const expectedSocialHash = 55; expect(eventTypeSocialHash).toBe(expectedSocialHash); @@ -65,7 +65,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event type Other', () => { - const eventTypeOtherHash = KitFilterHelper.hashEventType(EventTypeEnum.Other); + const eventTypeOtherHash = KitFilterHelper.hashEventType(EventType.Other); const expectedOtherHash = 56; expect(eventTypeOtherHash).toBe(expectedOtherHash); @@ -73,7 +73,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event type Media', () => { - const eventTypeMediaHash = KitFilterHelper.hashEventType(EventTypeEnum.Media); + const eventTypeMediaHash = KitFilterHelper.hashEventType(EventType.Media); const expectedMediaHash = 57; expect(eventTypeMediaHash).toBe(expectedMediaHash); @@ -85,14 +85,14 @@ describe('FilterHashingUtilities', () => { const eventName = 'foo-event-name'; it('should hash event name with event type Unknown', () => { - const eventTypeUnknownHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.Unknown); + const eventTypeUnknownHash = KitFilterHelper.hashEventName(eventName, EventType.Unknown); const expectedUnknownHash = -59445899; expect(eventTypeUnknownHash).toBe(expectedUnknownHash); }); it('should hash event name with event type Navigation', () => { - const eventTypeNavigationHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.Navigation); + const eventTypeNavigationHash = KitFilterHelper.hashEventName(eventName, EventType.Navigation); const expectedNavigationHash = 1448105910; @@ -100,7 +100,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event name with event type Location', () => { - const eventTypeLocationHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.Location); + const eventTypeLocationHash = KitFilterHelper.hashEventName(eventName, EventType.Location); const expectedLocationHash = -1339309577; @@ -108,7 +108,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event name with event type Search', () => { - const eventTypeSearchHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.Search); + const eventTypeSearchHash = KitFilterHelper.hashEventName(eventName, EventType.Search); const expectedSearchHash = 168242232; @@ -116,7 +116,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event name with event type Transaction', () => { - const eventTypeTransactionHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.Transaction); + const eventTypeTransactionHash = KitFilterHelper.hashEventName(eventName, EventType.Transaction); const expectedTransactionHash = 1675794041; @@ -124,7 +124,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event name with event type UserContent', () => { - const eventTypeUserContentHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.UserContent); + const eventTypeUserContentHash = KitFilterHelper.hashEventName(eventName, EventType.UserContent); const expectedUserContentHash = -1111621446; @@ -133,7 +133,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event name with event type UserPreference', () => { - const eventTypeUserPreferenceHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.UserPreference); + const eventTypeUserPreferenceHash = KitFilterHelper.hashEventName(eventName, EventType.UserPreference); const expectedUserPreferenceHash = 395930363; @@ -142,7 +142,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event name with event type Social', () => { - const eventTypeSocialHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.Social); + const eventTypeSocialHash = KitFilterHelper.hashEventName(eventName, EventType.Social); const expectedSocialHash = 1903482172; @@ -151,7 +151,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event name with event type Other', () => { - const eventTypeOtherHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.Other); + const eventTypeOtherHash = KitFilterHelper.hashEventName(eventName, EventType.Other); const expectedOtherHash = -883933315; @@ -160,7 +160,7 @@ describe('FilterHashingUtilities', () => { }); it('should hash event name with event type Media', () => { - const eventTypeMediaHash = KitFilterHelper.hashEventName(eventName, EventTypeEnum.Media); + const eventTypeMediaHash = KitFilterHelper.hashEventName(eventName, EventType.Media); const expectedMediaHash = 623618494; expect(eventTypeMediaHash).toBe(expectedMediaHash); @@ -170,7 +170,7 @@ describe('FilterHashingUtilities', () => { describe('#hashEventAttributeKey', () => { it('should hash event attribute key', () => { - const eventType:EventTypeEnum = EventTypeEnum.Navigation; + const eventType = EventType.Navigation; const eventName :string = 'foo-event-name'; const customAttributeName: string = 'event-attribute-key'; diff --git a/test/jest/sideloadedKit.spec.ts b/test/jest/sideloadedKit.spec.ts index 5086c70d..a846a3c6 100644 --- a/test/jest/sideloadedKit.spec.ts +++ b/test/jest/sideloadedKit.spec.ts @@ -1,9 +1,8 @@ import MPSideloadedKit, { IMPSideloadedKit } from "../../src/sideloadedKit"; import { IMPSideloadedKitConstructor } from "../../src/sideloadedKit"; -import { EventTypeEnum, IdentityType } from "../../src/types.interfaces"; +import { EventType, IdentityType } from "../../src/types"; import { UnregisteredKit } from '../../src/forwarders.interfaces'; import { IKitFilterSettings } from '../../src/configAPIClient'; -import { mParticle } from "../src/config/constants"; const mockKitInstance: UnregisteredKit = { register: function() {} @@ -21,7 +20,7 @@ describe('MPSideloadedKit', () => { describe('#addEventTypeFilter', () => { it('should add a hashed event type to eventTypeFilters', () => { const expectedResult = [48]; - mpSideloadedKit.addEventTypeFilter(EventTypeEnum.Unknown); + mpSideloadedKit.addEventTypeFilter(EventType.Unknown); expect(filterDictionary.eventTypeFilters).toEqual(expectedResult); }); }); @@ -30,14 +29,14 @@ describe('MPSideloadedKit', () => { it('should add a hashed event name to eventNameFilters', () => { const eventName = 'foo-event-name'; const expectedResult = [-59445899]; - mpSideloadedKit.addEventNameFilter(EventTypeEnum.Unknown, eventName); + mpSideloadedKit.addEventNameFilter(EventType.Unknown, eventName); expect(filterDictionary.eventNameFilters).toEqual(expectedResult); }); }); describe('#addEventAttributeFilter', () => { it('should add a hashed event attribute to attributeFilters', () => { - const eventType: EventTypeEnum = EventTypeEnum.Navigation; + const eventType = EventType.Navigation; const eventName: string = 'foo-event-name'; const customAttributeName: string = 'event-attribute-key'; diff --git a/test/jest/type-utils.spec.ts b/test/jest/type-utils.spec.ts deleted file mode 100644 index 9794883c..00000000 --- a/test/jest/type-utils.spec.ts +++ /dev/null @@ -1,138 +0,0 @@ -import { getIdentityName, getName, getNewIdentitiesByName } from '../../src/type-utils'; -import { CommerceEventType, EventTypeEnum, IdentityType } from '../../src/types.interfaces'; - -describe('getNewIdentitesByName', () => { - it('returns an identity name when passing an identity type', () => { - const { Email, CustomerId } = IdentityType; - - const newIdentitiesByType = { - [CustomerId]: 'foo', - [Email]: 'bar@gmail.com', - }; - - expect(getNewIdentitiesByName(newIdentitiesByType)).toEqual({ - customerid: 'foo', - email: 'bar@gmail.com', - }); - }); -}); - -describe('getIdentityName', () => { - it('returns an identity name when passing an identity type', () => { - const { - Other, - CustomerId, - Facebook, - Twitter, - Google, - Microsoft, - Yahoo, - Email, - FacebookCustomAudienceId, - Other2, - Other3, - Other4, - Other5, - Other6, - Other7, - Other8, - Other9, - Other10, - MobileNumber, - PhoneNumber2, - PhoneNumber3, - } = IdentityType; - - expect(getIdentityName(Other)).toBe('other'); - expect(getIdentityName(CustomerId)).toBe('customerid'); - expect(getIdentityName(Facebook)).toBe('facebook'); - expect(getIdentityName(Twitter)).toBe('twitter'); - expect(getIdentityName(Google)).toBe('google'); - expect(getIdentityName(Microsoft)).toBe('microsoft'); - expect(getIdentityName(Yahoo)).toBe('yahoo'); - expect(getIdentityName(Email)).toBe('email'); - expect(getIdentityName(FacebookCustomAudienceId)).toBe('facebookcustomaudienceid'); - expect(getIdentityName(Other2)).toBe('other2'); - expect(getIdentityName(Other3)).toBe('other3'); - expect(getIdentityName(Other4)).toBe('other4'); - expect(getIdentityName(Other5)).toBe('other5'); - expect(getIdentityName(Other6)).toBe('other6'); - expect(getIdentityName(Other7)).toBe('other7'); - expect(getIdentityName(Other8)).toBe('other8'); - expect(getIdentityName(Other9)).toBe('other9'); - expect(getIdentityName(Other10)).toBe('other10'); - expect(getIdentityName(MobileNumber)).toBe('mobile_number'); - expect(getIdentityName(PhoneNumber2)).toBe('phone_number_2'); - expect(getIdentityName(PhoneNumber3)).toBe('phone_number_3'); - }); - - it('returns null if the identity type is not found', () => { - expect(getIdentityName('foo' as unknown as IdentityType)).toBe(null); - }); -}); - -describe('#getName', () => { - it('returns the name of an Event Type', () => { - const { - Unknown, - Navigation, - Location, - Search, - Transaction, - UserContent, - UserPreference, - Social, - Other, - Media, - } = EventTypeEnum; - - expect(getName(Unknown)).toBe('Unknown'); - expect(getName(Navigation)).toBe('Navigation'); - expect(getName(Location)).toBe('Location'); - expect(getName(Search)).toBe('Search'); - expect(getName(Transaction)).toBe('Transaction'); - expect(getName(UserContent)).toBe('User Content'); - expect(getName(UserPreference)).toBe('User Preference'); - expect(getName(Social)).toBe('Social'); - expect(getName(Other)).toBe('Other'); - expect(getName(Media)).toBe('Other'); - }); - - it('returns the name of a Commerce Event Type', () => { - const { - ProductAddToCart, - ProductRemoveFromCart, - ProductCheckout, - ProductCheckoutOption, - ProductClick, - ProductViewDetail, - ProductPurchase, - ProductRefund, - PromotionView, - PromotionClick, - ProductAddToWishlist, - ProductRemoveFromWishlist, - ProductImpression, - } = CommerceEventType; - - expect(getName(ProductAddToCart)).toBe('Product Added to Cart'); - expect(getName(ProductRemoveFromCart)).toBe('Product Removed From Cart'); - expect(getName(ProductCheckout)).toBe('Product Checkout'); - expect(getName(ProductCheckoutOption)).toBe('Product Checkout Options'); - expect(getName(ProductClick)).toBe('Product Click'); - expect(getName(ProductViewDetail)).toBe('Product View Details'); - expect(getName(ProductPurchase)).toBe('Product Purchased'); - expect(getName(ProductRefund)).toBe('Product Refunded'); - expect(getName(PromotionView)).toBe('Promotion View'); - expect(getName(PromotionClick)).toBe('Promotion Click'); - expect(getName(ProductAddToWishlist)).toBe('Product Added to Wishlist'); - expect(getName(ProductRemoveFromWishlist)).toBe('Product Removed from Wishlist'); - expect(getName(ProductImpression)).toBe('Product Impression'); - }); - - it('returns other if the event type is not found', () => { - expect(getName('foo' as unknown as EventTypeEnum)).toBe('Other'); - }); -}); - -describe('#isValid', () => {}); \ No newline at end of file diff --git a/test/jest/types.spec.ts b/test/jest/types.spec.ts new file mode 100644 index 00000000..376e93ab --- /dev/null +++ b/test/jest/types.spec.ts @@ -0,0 +1,565 @@ +import { + ApplicationTransitionType, + CommerceEventType, + EventType, + IdentityType, + MessageType, + ProductActionType, + ProfileMessageType, + PromotionActionType, +} from '../../src/types'; + + + +describe('MessageType', () => { + it('returns a message type', () => { + const { + SessionStart, + SessionEnd, + PageView, + PageEvent, + CrashReport, + AppStateTransition, + Profile, + Commerce, + UserAttributeChange, + UserIdentityChange, + } = MessageType; + + expect(SessionStart).toEqual(1); + expect(SessionEnd).toEqual(2); + expect(PageView).toEqual(3); + expect(PageEvent).toEqual(4); + expect(CrashReport).toEqual(5); + expect(AppStateTransition).toEqual(10); + expect(Profile).toEqual(14); + expect(Commerce).toEqual(16); + expect(UserAttributeChange).toEqual(17); + expect(UserIdentityChange).toEqual(18); + }); +}); + +describe('EventType', () => { + const { + Unknown, + Navigation, + Location, + Search, + Transaction, + UserContent, + UserPreference, + Social, + Other, + Media, + } = EventType; + + it('returns an event type', () => { + expect(Unknown).toEqual(0); + expect(Navigation).toEqual(1); + expect(Location).toEqual(2); + expect(Search).toEqual(3); + expect(Transaction).toEqual(4); + expect(UserContent).toEqual(5); + expect(UserPreference).toEqual(6); + expect(Social).toEqual(7); + expect(Other).toEqual(8); + expect(Media).toEqual(9); + }); + + describe('#getName', () => { + it('returns the name of an Event Type', () => { + const { getName } = EventType; + + expect(getName(Unknown)).toBe('Unknown'); + expect(getName(Navigation)).toBe('Navigation'); + expect(getName(Location)).toBe('Location'); + expect(getName(Search)).toBe('Search'); + expect(getName(Transaction)).toBe('Transaction'); + expect(getName(UserContent)).toBe('User Content'); + expect(getName(UserPreference)).toBe('User Preference'); + expect(getName(Social)).toBe('Social'); + expect(getName(Other)).toBe('Other'); + expect(getName(Media)).toBe('Other'); + }); + + it('returns the name of a Commerce Event Type', () => { + const { getName } = EventType; + + const { + ProductAddToCart, + ProductRemoveFromCart, + ProductCheckout, + ProductCheckoutOption, + ProductClick, + ProductViewDetail, + ProductPurchase, + ProductRefund, + PromotionView, + PromotionClick, + ProductAddToWishlist, + ProductRemoveFromWishlist, + ProductImpression, + } = CommerceEventType; + + + expect(EventType.getName(ProductAddToCart)).toBe( + 'Product Added to Cart' + ); + expect(getName(ProductRemoveFromCart)).toBe( + 'Product Removed From Cart' + ); + expect(getName(ProductCheckout)).toBe( + 'Product Checkout' + ); + expect(getName(ProductCheckoutOption)).toBe( + 'Product Checkout Options' + ); + expect(getName(ProductClick)).toBe('Product Click'); + expect(getName(ProductViewDetail)).toBe( + 'Product View Details' + ); + expect(getName(ProductPurchase)).toBe( + 'Product Purchased' + ); + expect(getName(ProductRefund)).toBe('Product Refunded'); + expect(getName(PromotionView)).toBe('Promotion View'); + expect(getName(PromotionClick)).toBe('Promotion Click'); + expect(getName(ProductAddToWishlist)).toBe( + 'Product Added to Wishlist' + ); + expect(getName(ProductRemoveFromWishlist)).toBe( + 'Product Removed from Wishlist' + ); + expect(getName(ProductImpression)).toBe('Product Impression'); + }); + + + it('returns other if the event type is not found', () => { + const { getName } = EventType; + + expect(getName('foo' as unknown as number)).toBe('Other'); + }); + + it('returns other if the commerce event type is not found', () => { + const { getName } = EventType; + + expect(getName(NaN)).toBe('Other'); + }); + }); +}); + +describe('CommerceEventType', () => { + const { + ProductAddToCart, + ProductRemoveFromCart, + ProductCheckout, + ProductCheckoutOption, + ProductClick, + ProductViewDetail, + ProductPurchase, + ProductRefund, + PromotionView, + PromotionClick, + ProductAddToWishlist, + ProductRemoveFromWishlist, + ProductImpression, + } = CommerceEventType; + + it('returns a commerce event type', () => { + expect(ProductAddToCart).toEqual(10); + expect(ProductRemoveFromCart).toEqual(11); + expect(ProductCheckout).toEqual(12); + expect(ProductCheckoutOption).toEqual(13); + expect(ProductClick).toEqual(14); + expect(ProductViewDetail).toEqual(15); + expect(ProductPurchase).toEqual(16); + expect(ProductRefund).toEqual(17); + expect(PromotionView).toEqual(18); + expect(PromotionClick).toEqual(19); + expect(ProductAddToWishlist).toEqual(20); + expect(ProductRemoveFromWishlist).toEqual(21); + expect(ProductImpression).toEqual(22); + }); + + describe('#getName', () => { + + }); +}); + +describe('IdentityType', () => { + const { + Other, + CustomerId, + Facebook, + Twitter, + Google, + Microsoft, + Yahoo, + Email, + FacebookCustomAudienceId, + Other2, + Other3, + Other4, + Other5, + Other6, + Other7, + Other8, + Other9, + Other10, + MobileNumber, + PhoneNumber2, + PhoneNumber3, + } = IdentityType; + + it('returns an identity type', () => { + expect(Other).toEqual(0); + expect(CustomerId).toEqual(1); + expect(Facebook).toEqual(2); + expect(Twitter).toEqual(3); + expect(Google).toEqual(4); + expect(Microsoft).toEqual(5); + expect(Yahoo).toEqual(6); + expect(Email).toEqual(7); + + // There is no value for 8 + + expect(FacebookCustomAudienceId).toEqual(9); + expect(Other2).toEqual(10); + expect(Other3).toEqual(11); + expect(Other4).toEqual(12); + expect(Other5).toEqual(13); + expect(Other6).toEqual(14); + expect(Other7).toEqual(15); + expect(Other8).toEqual(16); + expect(Other9).toEqual(17); + expect(Other10).toEqual(18); + expect(MobileNumber).toEqual(19); + expect(PhoneNumber2).toEqual(20); + expect(PhoneNumber3).toEqual(21); + }); + + describe('#isValid', () => { + it('returns true if the identity type is valid', () => { + const { isValid } = IdentityType; + + expect(isValid(Other)).toBe(true); + expect(isValid(CustomerId)).toBe(true); + expect(isValid(Facebook)).toBe(true); + expect(isValid(Twitter)).toBe(true); + expect(isValid(Google)).toBe(true); + expect(isValid(Microsoft)).toBe(true); + expect(isValid(Yahoo)).toBe(true); + expect(isValid(Email)).toBe(true); + expect(isValid(FacebookCustomAudienceId)).toBe(true); + expect(isValid(Other2)).toBe(true); + expect(isValid(Other3)).toBe(true); + expect(isValid(Other4)).toBe(true); + expect(isValid(Other5)).toBe(true); + expect(isValid(Other6)).toBe(true); + expect(isValid(Other7)).toBe(true); + expect(isValid(Other8)).toBe(true); + expect(isValid(Other9)).toBe(true); + expect(isValid(Other10)).toBe(true); + expect(isValid(MobileNumber)).toBe(true); + expect(isValid(PhoneNumber2)).toBe(true); + expect(isValid(PhoneNumber3)).toBe(true); + }); + + it('returns false if the identity type is not valid', () => { + const { isValid } = IdentityType; + + expect(isValid(NaN)).toBe(false); + expect(isValid('invalid' as unknown as number)).toBe(false); + }); + }); + + describe('#getNewIdentitesByName', () => { + it('returns an identity name when passing an identity type', () => { + const { getNewIdentitiesByName } = IdentityType; + + const newIdentitiesByType = { + [CustomerId]: 'foo', + [Email]: 'bar@gmail.com', + }; + + expect(getNewIdentitiesByName(newIdentitiesByType)).toEqual({ + customerid: 'foo', + email: 'bar@gmail.com', + }); + }); + + // FIXME: We have a bug in the implementation + // FIXME: parseNumber causes unknown identity type to be returned as 0 + it.skip('returns an empty object if the identity type is not found', () => { + const { getNewIdentitiesByName } = IdentityType; + + const newIdentitiesByType = { + [NaN]: 'not-a-number', + ['invalid']: 'not-valid', + }; + + expect(getNewIdentitiesByName(newIdentitiesByType)).toEqual({}); + }); + }); + + describe('#getIdentityName', () => { + it('returns an identity name when passing an identity type', () => { + const { getIdentityName } = IdentityType; + + expect(getIdentityName(Other)).toBe('other'); + expect(getIdentityName(CustomerId)).toBe('customerid'); + expect(getIdentityName(Facebook)).toBe('facebook'); + expect(getIdentityName(Twitter)).toBe('twitter'); + expect(getIdentityName(Google)).toBe('google'); + expect(getIdentityName(Microsoft)).toBe('microsoft'); + expect(getIdentityName(Yahoo)).toBe('yahoo'); + expect(getIdentityName(Email)).toBe('email'); + expect(getIdentityName(FacebookCustomAudienceId)).toBe( + 'facebookcustomaudienceid' + ); + expect(getIdentityName(Other2)).toBe('other2'); + expect(getIdentityName(Other3)).toBe('other3'); + expect(getIdentityName(Other4)).toBe('other4'); + expect(getIdentityName(Other5)).toBe('other5'); + expect(getIdentityName(Other6)).toBe('other6'); + expect(getIdentityName(Other7)).toBe('other7'); + expect(getIdentityName(Other8)).toBe('other8'); + expect(getIdentityName(Other9)).toBe('other9'); + expect(getIdentityName(Other10)).toBe('other10'); + expect(getIdentityName(MobileNumber)).toBe('mobile_number'); + expect(getIdentityName(PhoneNumber2)).toBe('phone_number_2'); + expect(getIdentityName(PhoneNumber3)).toBe('phone_number_3'); + }); + + it('returns null if the identity type is not found', () => { + const { getIdentityName } = IdentityType; + + expect(getIdentityName('foo')).toBe(null); + }); + }); + + describe('#getName', () => { + it('returns the name of an Identity Type', () => { + const { getName } = IdentityType; + + expect(getName(CustomerId)).toBe('Customer ID'); + expect(getName(Facebook)).toBe('Facebook ID'); + expect(getName(Twitter)).toBe('Twitter ID'); + expect(getName(Google)).toBe('Google ID'); + expect(getName(Microsoft)).toBe('Microsoft ID'); + expect(getName(Yahoo)).toBe('Yahoo ID'); + expect(getName(Email)).toBe('Email'); + expect(getName(FacebookCustomAudienceId)).toBe('Facebook App User ID'); + }); + + it('returns other if the identity type is not found', () => { + const { getName } = IdentityType; + + expect(getName(Other)).toBe('Other ID'); + expect(getName(Other2)).toBe('Other ID'); + expect(getName(Other3)).toBe('Other ID'); + expect(getName(Other4)).toBe('Other ID'); + expect(getName(Other5)).toBe('Other ID'); + expect(getName(Other6)).toBe('Other ID'); + expect(getName(Other7)).toBe('Other ID'); + expect(getName(Other8)).toBe('Other ID'); + expect(getName(Other9)).toBe('Other ID'); + expect(getName(Other10)).toBe('Other ID'); + expect(getName(MobileNumber)).toBe('Other ID'); + expect(getName(PhoneNumber2)).toBe('Other ID'); + expect(getName(PhoneNumber3)).toBe('Other ID'); + + expect(getName(NaN)).toBe('Other ID'); + }); + }); + + describe('#getIdentityType', () => { + it('returns the identity type when passing an identity name', () => { + const { getIdentityType } = IdentityType; + + expect(getIdentityType('other')).toBe(Other); + expect(getIdentityType('customerid')).toBe(CustomerId); + expect(getIdentityType('facebook')).toBe(Facebook); + expect(getIdentityType('twitter')).toBe(Twitter); + expect(getIdentityType('google')).toBe(Google); + expect(getIdentityType('microsoft')).toBe(Microsoft); + expect(getIdentityType('yahoo')).toBe(Yahoo); + expect(getIdentityType('email')).toBe(Email); + expect(getIdentityType('facebookcustomaudienceid')).toBe( + FacebookCustomAudienceId + ); + expect(getIdentityType('other2')).toBe(Other2); + expect(getIdentityType('other3')).toBe(Other3); + expect(getIdentityType('other4')).toBe(Other4); + expect(getIdentityType('other5')).toBe(Other5); + expect(getIdentityType('other6')).toBe(Other6); + expect(getIdentityType('other7')).toBe(Other7); + expect(getIdentityType('other8')).toBe(Other8); + expect(getIdentityType('other9')).toBe(Other9); + expect(getIdentityType('other10')).toBe(Other10); + expect(getIdentityType('mobile_number')).toBe(MobileNumber); + expect(getIdentityType('phone_number_2')).toBe(PhoneNumber2); + expect(getIdentityType('phone_number_3')).toBe(PhoneNumber3); + }); + + it('returns false if the identity name is not found', () => { + const { getIdentityType } = IdentityType; + + expect(getIdentityType('foo')).toBe(false); + }); + }); +}); + +describe('ProductActionType', () => { + const { + Unknown, + AddToCart, + RemoveFromCart, + Checkout, + CheckoutOption, + Click, + ViewDetail, + Purchase, + Refund, + AddToWishlist, + RemoveFromWishlist, + } = ProductActionType; + + it('returns a product action type', () => { + expect(Unknown).toEqual(0); + expect(AddToCart).toEqual(1); + expect(RemoveFromCart).toEqual(2); + expect(Checkout).toEqual(3); + expect(CheckoutOption).toEqual(4); + expect(Click).toEqual(5); + expect(ViewDetail).toEqual(6); + expect(Purchase).toEqual(7); + expect(Refund).toEqual(8); + expect(AddToWishlist).toEqual(9); + expect(RemoveFromWishlist).toEqual(10); + }); + + describe('#getName', () => { + it('returns the name of a Product Action Type', () => { + expect(ProductActionType.getName(Unknown)).toBe('Unknown'); + expect(ProductActionType.getName(AddToCart)).toBe('Add to Cart'); + expect(ProductActionType.getName(RemoveFromCart)).toBe( + 'Remove from Cart' + ); + expect(ProductActionType.getName(Checkout)).toBe('Checkout'); + expect(ProductActionType.getName(CheckoutOption)).toBe( + 'Checkout Option' + ); + expect(ProductActionType.getName(Click)).toBe('Click'); + expect(ProductActionType.getName(ViewDetail)).toBe('View Detail'); + expect(ProductActionType.getName(Purchase)).toBe('Purchase'); + expect(ProductActionType.getName(Refund)).toBe('Refund'); + expect(ProductActionType.getName(AddToWishlist)).toBe( + 'Add to Wishlist' + ); + expect(ProductActionType.getName(RemoveFromWishlist)).toBe( + 'Remove from Wishlist' + ); + }); + + it('returns unknown if the product action type is not found', () => { + expect(ProductActionType.getName(NaN)).toBe('Unknown'); + }); + }); + + describe('#getExpansionName', () => { + it('returns the expanded name of a Product Action Type', () => { + expect(ProductActionType.getExpansionName(Unknown)).toBe('unknown'); + expect(ProductActionType.getExpansionName(AddToCart)).toBe( + 'add_to_cart' + ); + expect(ProductActionType.getExpansionName(RemoveFromCart)).toBe( + 'remove_from_cart' + ); + expect(ProductActionType.getExpansionName(Checkout)).toBe( + 'checkout' + ); + expect(ProductActionType.getExpansionName(CheckoutOption)).toBe( + 'checkout_option' + ); + expect(ProductActionType.getExpansionName(Click)).toBe('click'); + expect(ProductActionType.getExpansionName(ViewDetail)).toBe( + 'view_detail' + ); + expect(ProductActionType.getExpansionName(Purchase)).toBe( + 'purchase' + ); + expect(ProductActionType.getExpansionName(Refund)).toBe('refund'); + expect(ProductActionType.getExpansionName(AddToWishlist)).toBe( + 'add_to_wishlist' + ); + expect(ProductActionType.getExpansionName(RemoveFromWishlist)).toBe( + 'remove_from_wishlist' + ); + }); + + it('returns unknown if the product action type is not found', () => { + expect(ProductActionType.getExpansionName(NaN)).toBe('unknown'); + }); + }); +}); + +describe('PromotionActionType', () => { + it('returns a promotion action type', () => { + const { Unknown, PromotionView, PromotionClick } = PromotionActionType; + + expect(Unknown).toEqual(0); + expect(PromotionView).toEqual(1); + expect(PromotionClick).toEqual(2); + }); + + describe('#getName', () => { + it('returns the name of a Promotion Action Type', () => { + const { + PromotionView, + PromotionClick, + getName, + } = PromotionActionType; + + expect(getName(PromotionView)).toBe('view'); + expect(getName(PromotionClick)).toBe('click'); + }); + + it('returns unknown if the promotion action type is not found', () => { + const { getName } = PromotionActionType; + + expect(getName(0)).toBe('unknown'); + }); + }); + + describe('#getExpansionName', () => { + it('returns the name of a Promotion Action Type', () => { + const { + PromotionView, + PromotionClick, + getExpansionName, + } = PromotionActionType; + + expect(getExpansionName(PromotionView)).toBe('view'); + expect(getExpansionName(PromotionClick)).toBe('click'); + }); + + it('returns unknown if the promotion action type is not found', () => { + const { getExpansionName } = PromotionActionType; + + expect(getExpansionName(0)).toBe('unknown'); + }); + }); +}); + +describe('ProfileMessageType', () => { + it('returns a profile message type', () => { + expect(ProfileMessageType.Logout).toEqual(3); + }); +}); + +describe('ApplicationTransitionType', () => { + it('returns an application transition type', () => { + expect(ApplicationTransitionType.AppInit).toEqual(1); + }); +}); diff --git a/test/src/tests-forwarders.js b/test/src/tests-forwarders.js index 6a2bdacc..02426e3f 100644 --- a/test/src/tests-forwarders.js +++ b/test/src/tests-forwarders.js @@ -2073,7 +2073,7 @@ describe('forwarders', function() { }); }); - it('should forward event if event attribute forwarding rule is set and includeOnMatch is false but attributes do not match', function(done) { + it('should forward event if event attribute forwarding rule is set and includeOnMatch is false but attributes do not match', async () => { mParticle._resetForTests(MPConfig); const mockForwarder = new MockForwarder(); mockForwarder.register(window.mParticle.config); @@ -2090,12 +2090,12 @@ describe('forwarders', function() { window.mParticle.config.kitConfigs.push(config1); mParticle.init(apiKey, window.mParticle.config); - waitForCondition(() => { + await waitForCondition(() => { return ( window.mParticle.getInstance()?._Store?.identityCallInFlight === false ); }) - .then(() => { + window.MockForwarder1.instance.receivedEvent.EventName.should.equal(1); window.MockForwarder1.instance.receivedEvent = null; @@ -2110,9 +2110,6 @@ describe('forwarders', function() { const event = window.MockForwarder1.instance.receivedEvent; event.should.have.property('EventName', 'send this event to forwarder'); - - done(); - }); }); it('should send event to forwarder if filtering attribute and includingOnMatch is true', function(done) { diff --git a/test/src/tests-kit-blocking.ts b/test/src/tests-kit-blocking.ts index c0240ae1..74421b95 100644 --- a/test/src/tests-kit-blocking.ts +++ b/test/src/tests-kit-blocking.ts @@ -7,7 +7,7 @@ import KitBlocker from '../../src/kitBlocking'; import Types from '../../src/types'; import { DataPlanVersion } from '@mparticle/data-planning-models'; import fetchMock from 'fetch-mock/esm/client'; -const { findBatch, waitForCondition, fetchMockSuccess, hasIdentifyReturned } = Utils; +const { waitForCondition, fetchMockSuccess, hasIdentifyReturned } = Utils; let forwarderDefaultConfiguration = Utils.forwarderDefaultConfiguration, MockForwarder = Utils.MockForwarder; @@ -467,7 +467,7 @@ describe('kit blocking', () => { it('should not transform productAttributes if a product attribute is not planned, additionalProperties = false, and blok.ea = true', function(done) { event.EventName = 'eCommerce - AddToCart'; - event.EventCategory = Types.CommerceEventType.AddToCart; + event.EventCategory = Types.CommerceEventType.ProductAddToCart; event.EventDataType = Types.MessageType.Commerce; event.ProductAction = { ProductActionType: SDKProductActionType.AddToCart, diff --git a/test/src/tests-serverModel.ts b/test/src/tests-serverModel.ts index c84facee..7e3fc401 100644 --- a/test/src/tests-serverModel.ts +++ b/test/src/tests-serverModel.ts @@ -1,5 +1,4 @@ import Types from '../../src/types'; -import sinon from 'sinon'; import { urls, testMPID, apiKey } from './config/constants'; import { expect } from 'chai'; import { IUploadObject } from '../../src/serverModel'; @@ -669,7 +668,7 @@ describe('ServerModel', () => { const event: BaseEvent = { name: 'Test Event', sourceMessageId: null, - messageType: Types.MessageType.CustomEvent, + messageType: Types.MessageType.PageEvent, eventType: Types.EventType.Other, }; @@ -689,7 +688,7 @@ describe('ServerModel', () => { const event: BaseEvent = { name: 'Test Opt Out Event', - messageType: Types.MessageType.CustomEvent, + messageType: Types.MessageType.PageEvent, }; const actualEventObject = mParticle @@ -707,7 +706,7 @@ describe('ServerModel', () => { const event: BaseEvent = { name: 'Test Opt Out Event', - messageType: Types.MessageType.CustomEvent, + messageType: Types.MessageType.PageEvent, }; const actualEventObject = mParticle