Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Create Interfaces for eCommerce #964

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions src/ecommerce.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import {
ProductAction,
Product,
Promotion,
CommerceEvent,
} from '@mparticle/event-models';
import {
SDKEventAttrs,
SDKEventOptions,
TransactionAttributes,
} from '@mparticle/web-sdk';
import { valueof } from './utils';
import {
ProductActionType,
PromotionActionType,
CommerceEventType,
EventType,
} from './types';
import {
SDKEvent,
SDKEventCustomFlags,
SDKImpression,
SDKProduct,
SDKProductImpression,
SDKPromotion,
} from './sdkRuntimeModels';

interface IECommerceShared {
createProduct(
name: string,
sku: string | number,
price: string | number,
quantity?: string | number,
variant?: string,
category?: string,
brand?: string,
position?: number,
couponCode?: string,
attributes?: Record<string, string | number>
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
): SDKProduct | null;
createImpression(name: string, product: Product): SDKImpression | null;
createPromotion(
id: string | number,
creative?: string,
name?: string,
position?: string
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
): SDKPromotion | null;
createTransactionAttributes(
id: string | number,
affiliation?: string,
couponCode?: string,
revenue?: string | number,
shipping?: string | number,
tax?: string | number
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
): TransactionAttributes | null;
expandCommerceEvent(event: CommerceEvent): SDKEvent[] | null;
}

export interface SDKCart {
add(product: SDKProduct | SDKProduct[], logEvent?: boolean): void;
remove(product: SDKProduct | SDKProduct[], logEvent?: boolean): void;
clear(): void;
}

// Used for the public `eCommerce` namespace
export interface SDKECommerceAPI extends IECommerceShared {
/*
* @deprecated
*/
Cart: SDKCart;

logCheckout(
step: number,
option?: string,
attrs?: SDKEventAttrs,
customFlags?: SDKEventCustomFlags
): void;
logImpression(
impression: SDKProductImpression,
attrs?: SDKEventAttrs,
customFlags?: SDKEventCustomFlags,
eventOptions?: SDKEventOptions
): void;
logProductAction(
productActionType: valueof<typeof ProductActionType>,
product: SDKProduct | SDKProduct[],
attrs?: SDKEventAttrs,
customFlags?: SDKEventCustomFlags,
transactionAttributes?: TransactionAttributes,
eventOptions?: SDKEventOptions
): void;
logPromotion(
type: valueof<typeof PromotionActionType>,
promotion: SDKPromotion,
attrs?: SDKEventAttrs,
customFlags?: SDKEventCustomFlags,
eventOptions?: SDKEventOptions
): void;
logPurchase(
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
transactionAttributes: TransactionAttributes,
product: SDKProduct | SDKProduct[],
clearCart?: boolean,
attrs?: SDKEventAttrs,
customFlags?: SDKEventCustomFlags
): void;
logRefund(
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
transactionAttributes: TransactionAttributes,
product: SDKProduct | SDKProduct[],
clearCart?: boolean,
attrs?: SDKEventAttrs,
customFlags?: SDKEventCustomFlags
): void;
setCurrencyCode(code: string): void;
}

// Used for the private `_Ecommerce` namespace
export interface IECommerce extends IECommerceShared {
buildProductList(event: any, product: Product | Product[]): Product[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
buildProductList(event: any, product: Product | Product[]): Product[];
buildProductList(event: SDKEvent, product: Product | Product[]): Product[];

convertProductActionToEventType(
productActionType: valueof<typeof ProductActionType>
): typeof CommerceEventType | typeof EventType | null;
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
convertPromotionActionToEventType(
promotionActionType: valueof<typeof PromotionActionType>
): typeof CommerceEventType | null;
convertTransactionAttributesToProductAction(
transactionAttributes: TransactionAttributes,
productAction: ProductAction
): void;
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
createCommerceEventObject(
customFlags: SDKEventCustomFlags,
options?: SDKEventOptions
): SDKEvent | null;
expandProductAction(commerceEvent: CommerceEvent): SDKEvent[];
expandProductImpression(commerceEvent: CommerceEvent): SDKEvent[];
expandPromotionAction(commerceEvent: CommerceEvent): SDKEvent[];
extractActionAttributes(
attributes: Record<string, string | number>,
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
productAction: ProductAction
): void;
extractProductAttributes(
attributes: Record<string, string | number>,
product: Product
): void;
extractPromotionAttributes(
attributes: Record<string, string | number>,
promotion: Promotion
): void;
extractTransactionId(
attributes: Record<string, string | number>,
productAction: ProductAction
): void;
generateExpandedEcommerceName(eventName: string, plusOne: boolean): string;
getProductActionEventName(
productActionType: valueof<typeof ProductActionType>
): string;
getPromotionActionEventName(
promotionActionType: valueof<typeof PromotionActionType>
): string;
sanitizeAmount(amount: string | number, category: string): number;
}
9 changes: 8 additions & 1 deletion src/sdkRuntimeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import IntegrationCapture from './integrationCapture';
import { INativeSdkHelpers } from './nativeSdkHelpers.interfaces';
import { ICookieSyncManager, IPixelConfiguration } from './cookieSyncManager';
import { IEvents } from './events.interfaces';
import { IECommerce, SDKECommerceAPI } from './ecommerce.interfaces';

// TODO: Resolve this with version in @mparticle/web-sdk
export type SDKEventCustomFlags = Dictionary<any>;
Expand Down Expand Up @@ -107,6 +108,11 @@ export interface SDKPromotion {
Position?: string;
}

export interface SDKImpression {
Name: string;
Product: SDKProduct;
}

export interface SDKProductImpression {
ProductImpressionList?: string;
ProductList?: SDKProduct[];
Expand Down Expand Up @@ -170,6 +176,7 @@ export interface MParticleWebSDK {
_Forwarders: any;
_Helpers: SDKHelpersApi;
_Events: IEvents;
_Ecommerce: IECommerce;
config: SDKInitConfig;
_ServerModel: IServerModel;
_SessionManager: ISessionManager;
Expand Down Expand Up @@ -208,7 +215,7 @@ export interface MParticleWebSDK {
eventOptions?: SDKEventOptions
): void;
logBaseEvent(event: BaseEvent, eventOptions?: SDKEventOptions): void;
eCommerce: any;
eCommerce: SDKECommerceAPI;
logLevel: string;
ProductActionType: SDKProductActionType;
generateHash(value: string): string;
Expand Down
10 changes: 2 additions & 8 deletions test/src/tests-batchUploader_3.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import sinon from 'sinon';
import { urls, apiKey, MPConfig, testMPID } from './config/constants';
import {
BaseEvent,
MParticleWebSDK,
SDKEvent,
SDKProductActionType,
} from '../../src/sdkRuntimeModels';
import { Batch, CustomEventData } from '@mparticle/event-models';
import Utils from './config/utils';
import { BatchUploader } from '../../src/batchUploader';
import { expect } from 'chai';
import _BatchValidator from '../../src/mockBatchCreator';
import Logger from '../../src/logger.js';
import { event0, event1, event2, event3 } from '../fixtures/events';
import fetchMock from 'fetch-mock/esm/client';
import { ProductActionType } from '../../src/types';
const { fetchMockSuccess, waitForCondition, hasIdentifyReturned } = Utils;

declare global {
Expand Down Expand Up @@ -137,7 +131,7 @@ describe('batch uploader', () => {
window.mParticle.logEvent('Test Event');

var product1 = window.mParticle.eCommerce.createProduct('iphone', 'iphoneSKU', 999);
window.mParticle.eCommerce.logProductAction(SDKProductActionType.AddToCart, product1);
window.mParticle.eCommerce.logProductAction(ProductActionType.AddToCart, product1);

const lastCall = fetchMock.lastCall();
const endpoint = lastCall[0];
Expand Down
9 changes: 2 additions & 7 deletions test/src/tests-batchUploader_4.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import sinon from 'sinon';
import { urls, apiKey, MPConfig, testMPID } from './config/constants';
import {
BaseEvent,
MParticleWebSDK,
SDKEvent,
SDKProductActionType,
} from '../../src/sdkRuntimeModels';
import { Batch, CustomEventData } from '@mparticle/event-models';
import Utils from './config/utils';
import { BatchUploader } from '../../src/batchUploader';
import { expect } from 'chai';
import _BatchValidator from '../../src/mockBatchCreator';
import Logger from '../../src/logger.js';
import { event0, event1, event2, event3 } from '../fixtures/events';
import fetchMock from 'fetch-mock/esm/client';
import { ProductActionType } from '../../src/types';
const { fetchMockSuccess, waitForCondition, hasIdentifyReturned } = Utils;

declare global {
Expand Down Expand Up @@ -138,7 +133,7 @@ describe('batch uploader', () => {
mockServer.requests.length.should.equal(1);

var product1 = window.mParticle.eCommerce.createProduct('iphone', 'iphoneSKU', 999);
window.mParticle.eCommerce.logProductAction(SDKProductActionType.AddToCart, product1);
window.mParticle.eCommerce.logProductAction(ProductActionType.AddToCart, product1);
// 1st request is /Identity call, 2nd request is /Event call
const batch = JSON.parse(mockServer.secondRequest.requestBody);

Expand Down
2 changes: 1 addition & 1 deletion test/src/tests-identities-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ describe('identities and attributes', function() {
product2 = mParticle.eCommerce.createProduct('Android', 'SKU2', 1);
waitForCondition(hasIdentifyReturned)
.then(() => {
mParticle.eCommerce.Cart.add([product1, product2]);
mParticle.eCommerce.Cart.add([product1, product2], null);
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved

const cartProducts = mParticle.Identity.getCurrentUser()
.getCart()
Expand Down
12 changes: 6 additions & 6 deletions test/src/tests-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fetchMock from 'fetch-mock/esm/client';
import { expect } from 'chai';
import Utils from './config/utils';
import Constants, { HTTP_ACCEPTED } from '../../src/constants';
import { MParticleWebSDK } from '../../src/sdkRuntimeModels';
import { MParticleWebSDK, SDKProduct } from '../../src/sdkRuntimeModels';
import {
urls,
apiKey,
Expand Down Expand Up @@ -2029,7 +2029,7 @@ describe('identity', function() {
mParticle.Identity.getCurrentUser().setUserAttribute('foo1', 'bar1');
expect(fetchMock.calls().length).to.equal(7);

const product1: Product = mParticle.eCommerce.createProduct(
const product1: SDKProduct = mParticle.eCommerce.createProduct(
'iPhone',
'12345',
'1000',
Expand Down Expand Up @@ -4799,11 +4799,11 @@ describe('identity', function() {
waitForCondition(hasIdentifyReturned)
.then(() => {

const product = mParticle.eCommerce.createProduct(
const product: Product = mParticle.eCommerce.createProduct(
alexs-mparticle marked this conversation as resolved.
Show resolved Hide resolved
'iPhone',
'12345',
400
);
) as Product;
mParticle
.getInstance()
.Identity.getCurrentUser()
Expand Down Expand Up @@ -4831,11 +4831,11 @@ describe('identity', function() {
waitForCondition(hasIdentifyReturned)
.then(() => {

const product = mParticle.eCommerce.createProduct(
const product: Product = mParticle.eCommerce.createProduct(
'iPhone',
'12345',
400
);
) as Product;

mParticle
.getInstance()
Expand Down
Loading