Skip to content

Commit

Permalink
DINT-974: include commerceScope in all AEP events (#109)
Browse files Browse the repository at this point in the history
Co-authored-by: ani hammond <ani hammond>
  • Loading branch information
aniham authored Aug 16, 2023
1 parent da39dfc commit 322259c
Show file tree
Hide file tree
Showing 25 changed files with 128 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import * as sdkSchemas from "@adobe/magento-storefront-events-sdk/src/types/sche

import { sendEvent } from "../../alloy";
import { createAccountPayload } from "../../../src/utils/aep/account";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "userAccount.createProfile";
const aepHandler = async (event: Event): Promise<void> => {
const { debugContext, accountContext, customContext } = event.eventInfo;
const { debugContext, accountContext, storefrontInstanceContext, customContext } = event.eventInfo;

const payload = createAccountPayload(customContext, accountContext as sdkSchemas.Account);

payload.userAccount = {
createProfile: 1,
};

payload.commerce = payload.commerce || {};
payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import * as sdkSchemas from "@adobe/magento-storefront-events-sdk/src/types/sche

import { sendEvent } from "../../alloy";
import { createAccountPayload } from "../../../src/utils/aep/account";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "userAccount.updateProfile";
const aepHandler = async (event: Event): Promise<void> => {
const { debugContext, accountContext, customContext } = event.eventInfo;
const { debugContext, accountContext, storefrontInstanceContext, customContext } = event.eventInfo;

const payload = createAccountPayload(customContext, accountContext as sdkSchemas.Account);

payload.userAccount = {
updateProfile: 1,
};

payload.commerce = payload.commerce || {};
payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import * as sdkSchemas from "@adobe/magento-storefront-events-sdk/src/types/sche

import { sendEvent } from "../../alloy";
import { createAccountPayload } from "../../../src/utils/aep/account";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "userAccount.login";
const aepHandler = async (event: Event): Promise<void> => {
const { debugContext, accountContext, customContext } = event.eventInfo;
const { debugContext, accountContext, storefrontInstanceContext, customContext } = event.eventInfo;

const payload = createAccountPayload(customContext, accountContext as sdkSchemas.Account);

payload.userAccount = {
login: 1,
};

payload.commerce = payload.commerce || {};
payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Event } from "@adobe/magento-storefront-events-sdk/dist/types/types/eve

import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "userAccount.logout";
const aepHandler = async (event: Event): Promise<void> => {
const { debugContext, customContext } = event.eventInfo;
const { debugContext, storefrontInstanceContext, customContext } = event.eventInfo;

let payload: BeaconSchema = {};
if (customContext && Object.keys(customContext as BeaconSchema).length !== 0) {
Expand All @@ -17,6 +18,9 @@ const aepHandler = async (event: Event): Promise<void> => {
logout: 1,
};

payload.commerce = payload.commerce || {};
payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;
sendEvent(payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";
import { createOrder } from "../../utils/aep/order";
import { createProductListItems } from "../../utils/aep/productListItems";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.purchases";

Expand Down Expand Up @@ -48,6 +49,8 @@ const aepHandler = async (event: Event): Promise<void> => {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Event } from "@adobe/magento-storefront-events-sdk/dist/types/types/eve
import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";
import { createProductListItems } from "../../utils/aep/productListItems";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.productListAdds";

Expand Down Expand Up @@ -32,6 +33,8 @@ const aepHandler = async (event: Event): Promise<void> => {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;
sendEvent(payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Event } from "@adobe/magento-storefront-events-sdk/dist/types/types/eve
import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";
import { createProductListItems } from "../../utils/aep/productListItems";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.productListRemovals";

Expand Down Expand Up @@ -32,6 +33,8 @@ const aepHandler = async (event: Event): Promise<void> => {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Event } from "@adobe/magento-storefront-events-sdk/dist/types/types/eve
import { sendEvent } from "../../alloy";
import { BeaconSchema, ProductListItem } from "../../types/aep";
import { getDiscountAmount } from "../../utils/discount";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.productViews";

Expand Down Expand Up @@ -38,6 +39,8 @@ const aepHandler = async (event: Event): Promise<void> => {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";
import { createProductListItems } from "../../utils/aep/productListItems";
import { createRequisitionList } from "../../utils/aep/requisitionList";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.requisitionListAdds";

Expand Down Expand Up @@ -41,6 +42,8 @@ const handler = async (event: Event): Promise<void> => {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";

import { createRequisitionList } from "../../utils/aep/requisitionList";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.requisitionListOpens";

const handler = async (event: Event): Promise<void> => {
const { accountContext, debugContext, requisitionListContext, customContext } = event.eventInfo;
const { accountContext, debugContext, requisitionListContext, customContext, storefrontInstanceContext } =
event.eventInfo;
let payload: BeaconSchema = {};
if (customContext && Object.keys(customContext as BeaconSchema).length !== 0) {
// override payload on custom context
Expand All @@ -25,6 +27,8 @@ const handler = async (event: Event): Promise<void> => {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";
import { createRequisitionList } from "../../utils/aep/requisitionList";
import { createProductListItems } from "../../utils/aep/productListItems";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.requisitionListRemovals";

Expand Down Expand Up @@ -42,6 +43,8 @@ const aepHandler = async (event: Event): Promise<void> => {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;
sendEvent(payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { SearchFilter, SearchSort } from "@adobe/magento-storefront-events-sdk/d
import { sendEvent } from "../../alloy";
import { createSearchInputCtx } from "../../contexts";
import { BeaconSchema, Filter, Sort } from "../../types/aep";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "searchRequest";

const handler = async (event: Event): Promise<void> => {
const { searchUnitId, searchInputContext, debugContext, customContext } = event.eventInfo;
const { searchUnitId, searchInputContext, debugContext, storefrontInstanceContext, customContext } =
event.eventInfo;

let payload: BeaconSchema = {};

Expand Down Expand Up @@ -63,6 +65,9 @@ const handler = async (event: Event): Promise<void> => {
payload.searchRequest.id = searchInputCtx.data.searchRequestId as string;
}

payload.commerce = payload.commerce || {};
payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { sendEvent } from "../../alloy";
import { createSearchResultsCtx } from "../../contexts";
import { BeaconSchema } from "../../types/aep";
import { SearchResultProduct } from "../../types/contexts";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "searchResponse";

const handler = async (event: Event): Promise<void> => {
const { searchUnitId, searchResultsContext, debugContext, customContext } = event.eventInfo;
const { searchUnitId, searchResultsContext, debugContext, storefrontInstanceContext, customContext } =
event.eventInfo;

const searchResultsCtx = createSearchResultsCtx(searchUnitId as string, searchResultsContext);

Expand Down Expand Up @@ -44,6 +46,9 @@ const handler = async (event: Event): Promise<void> => {
payload.searchResponse.id = searchResultsCtx.data.searchRequestId as string;
}

payload.commerce = payload.commerce || {};
payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Event } from "@adobe/magento-storefront-events-sdk/dist/types/types/eve
import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";
import { createProductListItems } from "../../utils/aep/productListItems";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.checkouts";

Expand All @@ -26,10 +27,13 @@ const handler = async (event: Event): Promise<void> => {
undefined,
storefrontInstanceContext,
);

payload.commerce.checkouts = {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Event } from "@adobe/magento-storefront-events-sdk/dist/types/types/eve
import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";
import { createProductListItems } from "../../utils/aep/productListItems";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.productListOpens";

Expand Down Expand Up @@ -31,6 +32,8 @@ const handler = async (event: Event): Promise<void> => {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Event } from "@adobe/magento-storefront-events-sdk/dist/types/types/eve
import { sendEvent } from "../../alloy";
import { BeaconSchema } from "../../types/aep";
import { createProductListItems } from "../../utils/aep/productListItems";
import { createCommerceScope } from "../../utils/aep/commerceScope";

const XDM_EVENT_TYPE = "commerce.productListViews";

Expand Down Expand Up @@ -32,6 +33,8 @@ const aepHandler = async (event: Event): Promise<void> => {
value: 1,
};

payload.commerce.commerceScope = createCommerceScope(storefrontInstanceContext);

payload._id = debugContext?.eventId;
payload.eventType = XDM_EVENT_TYPE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type Commerce = {
productListViews?: ProductListViews;
cart?: Cart;
checkouts?: Checkout;
commerceScope?: CommerceScope;
purchases?: Purchases;
order?: Order;
promotionID?: string;
Expand All @@ -17,6 +18,13 @@ export type Commerce = {
shipping?: Shipping;
};

export type CommerceScope = {
environmentID: string;
storeCode?: string;
storeViewCode?: string;
websiteCode?: string;
};

export type ProductView = {
value: number;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { StorefrontInstance } from "@adobe/magento-storefront-events-sdk/src/types/schemas";
import { CommerceScope } from "../../types/aep";

const createCommerceScope = (storefrontInstanceContext: StorefrontInstance): CommerceScope => {
return {
environmentID: storefrontInstanceContext.environmentId,
storeCode: storefrontInstanceContext.storeCode,
storeViewCode: storefrontInstanceContext.storeViewCode,
websiteCode: storefrontInstanceContext.websiteCode,
};
};

export { createCommerceScope };
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ test("correctly structures AEP event and calls alloy.sendEvent", () => {

expect(sendEvent).toHaveBeenCalledWith({
commerce: {
commerceScope: {
environmentID: "aaaaaa",
storeCode: "magento",
storeViewCode: "default",
websiteCode: "website",
},
order: {
purchaseID: "111111",
currencyCode: "USD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ test("correctly structures AEP event and calls alloy.sendEvent", () => {

expect(sendEvent).toHaveBeenCalledWith({
commerce: {
commerceScope: {
environmentID: "aaaaaa",
storeCode: "magento",
storeViewCode: "default",
websiteCode: "website",
},
cart: {
cartID: "111111",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ test("correctly structures AEP event and calls alloy.sendEvent", () => {

expect(sendEvent).toHaveBeenCalledWith({
commerce: {
commerceScope: {
environmentID: "aaaaaa",
storeCode: "magento",
storeViewCode: "default",
websiteCode: "website",
},
cart: {
cartID: "111111",
},
Expand Down
Loading

0 comments on commit 322259c

Please # to comment.