Skip to content

Commit

Permalink
feat: add support for conversion subtypes (#486)
Browse files Browse the repository at this point in the history
This adds support for the addToCart and purchase subtypes of the
conversion event type.

EEX-696
  • Loading branch information
benhinchley authored Aug 30, 2023
1 parent ed6ab85 commit beb9ef2
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 2 deletions.
132 changes: 132 additions & 0 deletions lib/__tests__/conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,76 @@ describe("convertedObjectIDsAfterSearch", () => {
});
});

describe("addedToCartObjectIDsAfterSearch", () => {
const convertParams = {
index: "index1",
eventName: "Product added to cart",
objectIDs: ["12345"],
queryID: "test"
};

it("should call sendEvents with proper params", () => {
analyticsInstance.addedToCartObjectIDsAfterSearch(convertParams);
expect((analyticsInstance as any).sendEvents).toHaveBeenCalledWith(
[
{
eventType: "conversion",
eventSubtype: "addToCart",
...convertParams
}
],
undefined
);
});

it("should call sendEvents with additional params if provided", () => {
analyticsInstance.addedToCartObjectIDsAfterSearch(
convertParams,
additionalParameters
);

expect(analyticsInstance.sendEvents).toHaveBeenCalledWith(
expect.any(Array),
additionalParameters
);
});
});

describe("purchasedObjectIDsAfterSearch", () => {
const convertParams = {
index: "index1",
eventName: "Product added to cart",
objectIDs: ["12345"],
queryID: "test"
};

it("should call sendEvents with proper params", () => {
analyticsInstance.purchasedObjectIDsAfterSearch(convertParams);
expect((analyticsInstance as any).sendEvents).toHaveBeenCalledWith(
[
{
eventType: "conversion",
eventSubtype: "purchase",
...convertParams
}
],
undefined
);
});

it("should call sendEvents with additional params if provided", () => {
analyticsInstance.purchasedObjectIDsAfterSearch(
convertParams,
additionalParameters
);

expect(analyticsInstance.sendEvents).toHaveBeenCalledWith(
expect.any(Array),
additionalParameters
);
});
});

describe("convertedObjectIDs", () => {
const convertParams = {
index: "index1",
Expand Down Expand Up @@ -86,6 +156,68 @@ describe("convertedObjectIDs", () => {
});
});

describe("addedToCartObjectIDs", () => {
const convertParams = {
index: "index1",
eventName: "hit converted",
objectIDs: ["12345"]
};

it("should call sendEvents with proper params", () => {
analyticsInstance.addedToCartObjectIDs(convertParams);
expect((analyticsInstance as any).sendEvents).toHaveBeenCalledWith(
[
{
eventType: "conversion",
eventSubtype: "addToCart",
...convertParams
}
],
undefined
);
});

it("should call sendEvents with additional params if provided", () => {
analyticsInstance.addedToCartObjectIDs(convertParams, additionalParameters);

expect(analyticsInstance.sendEvents).toHaveBeenCalledWith(
expect.any(Array),
additionalParameters
);
});
});

describe("purchasedObjectIDs", () => {
const convertParams = {
index: "index1",
eventName: "hit converted",
objectIDs: ["12345"]
};

it("should call sendEvents with proper params", () => {
analyticsInstance.purchasedObjectIDs(convertParams);
expect((analyticsInstance as any).sendEvents).toHaveBeenCalledWith(
[
{
eventType: "conversion",
eventSubtype: "purchase",
...convertParams
}
],
undefined
);
});

it("should call sendEvents with additional params if provided", () => {
analyticsInstance.purchasedObjectIDs(convertParams, additionalParameters);

expect(analyticsInstance.sendEvents).toHaveBeenCalledWith(
expect.any(Array),
additionalParameters
);
});
});

describe("convertedFilters", () => {
const convertParams = {
index: "index1",
Expand Down
18 changes: 17 additions & 1 deletion lib/_addEventType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { InsightsEvent, InsightsEventType } from "./types";
import {
InsightsEvent,
InsightsEventConversionSubType,
InsightsEventType
} from "./types";

export function addEventType(
eventType: InsightsEventType,
Expand All @@ -9,3 +13,15 @@ export function addEventType(
...event
}));
}

export function addEventTypeAndSubtype(
eventType: InsightsEventType,
eventSubtype: InsightsEventConversionSubType,
params: Array<Omit<InsightsEvent, "eventType" | "eventSubtype">>
): InsightsEvent[] {
return params.map((event) => ({
eventType,
eventSubtype,
...event
}));
}
57 changes: 56 additions & 1 deletion lib/conversion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { addEventType } from "./_addEventType";
import { addEventType, addEventTypeAndSubtype } from "./_addEventType";
import AlgoliaAnalytics from "./insights";
import { extractAdditionalParams, WithAdditionalParams } from "./utils";
import { InsightsEvent } from "./types";

export interface InsightsSearchConversionEvent {
eventName: string;
Expand All @@ -10,6 +11,7 @@ export interface InsightsSearchConversionEvent {

queryID: string;
objectIDs: string[];
objectData?: InsightsEvent["objectData"];
}

export function convertedObjectIDsAfterSearch(
Expand All @@ -22,13 +24,40 @@ export function convertedObjectIDsAfterSearch(
return this.sendEvents(addEventType("conversion", events), additionalParams);
}

export function addedToCartObjectIDsAfterSearch(
this: AlgoliaAnalytics,
...params: WithAdditionalParams<InsightsSearchConversionEvent>[]
) {
const { events, additionalParams } =
extractAdditionalParams<InsightsSearchConversionEvent>(params);

return this.sendEvents(
addEventTypeAndSubtype("conversion", "addToCart", events),
additionalParams
);
}

export function purchasedObjectIDsAfterSearch(
this: AlgoliaAnalytics,
...params: WithAdditionalParams<InsightsSearchConversionEvent>[]
) {
const { events, additionalParams } =
extractAdditionalParams<InsightsSearchConversionEvent>(params);

return this.sendEvents(
addEventTypeAndSubtype("conversion", "purchase", events),
additionalParams
);
}

export interface InsightsSearchConversionObjectIDsEvent {
eventName: string;
userToken?: string;
timestamp?: number;
index: string;

objectIDs: string[];
objectData?: InsightsEvent["objectData"];
}

export function convertedObjectIDs(
Expand All @@ -41,6 +70,32 @@ export function convertedObjectIDs(
return this.sendEvents(addEventType("conversion", events), additionalParams);
}

export function addedToCartObjectIDs(
this: AlgoliaAnalytics,
...params: WithAdditionalParams<InsightsSearchConversionObjectIDsEvent>[]
) {
const { events, additionalParams } =
extractAdditionalParams<InsightsSearchConversionObjectIDsEvent>(params);

return this.sendEvents(
addEventTypeAndSubtype("conversion", "addToCart", events),
additionalParams
);
}

export function purchasedObjectIDs(
this: AlgoliaAnalytics,
...params: WithAdditionalParams<InsightsSearchConversionObjectIDsEvent>[]
) {
const { events, additionalParams } =
extractAdditionalParams<InsightsSearchConversionObjectIDsEvent>(params);

return this.sendEvents(
addEventTypeAndSubtype("conversion", "purchase", events),
additionalParams
);
}

export interface InsightsSearchConversionFiltersEvent {
eventName: string;
userToken?: string;
Expand Down
14 changes: 14 additions & 0 deletions lib/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import {
} from "./click";
import {
convertedObjectIDsAfterSearch,
addedToCartObjectIDsAfterSearch,
purchasedObjectIDsAfterSearch,
convertedObjectIDs,
addedToCartObjectIDs,
purchasedObjectIDs,
convertedFilters
} from "./conversion";
import { viewedObjectIDs, viewedFilters } from "./view";
Expand Down Expand Up @@ -87,7 +91,11 @@ class AlgoliaAnalytics {
public clickedFilters: typeof clickedFilters;

public convertedObjectIDsAfterSearch: typeof convertedObjectIDsAfterSearch;
public purchasedObjectIDsAfterSearch: typeof purchasedObjectIDsAfterSearch;
public addedToCartObjectIDsAfterSearch: typeof addedToCartObjectIDsAfterSearch;
public convertedObjectIDs: typeof convertedObjectIDs;
public addedToCartObjectIDs: typeof addedToCartObjectIDs;
public purchasedObjectIDs: typeof purchasedObjectIDs;
public convertedFilters: typeof convertedFilters;

public viewedObjectIDs: typeof viewedObjectIDs;
Expand All @@ -111,7 +119,13 @@ class AlgoliaAnalytics {

this.convertedObjectIDsAfterSearch =
convertedObjectIDsAfterSearch.bind(this);
this.purchasedObjectIDsAfterSearch =
purchasedObjectIDsAfterSearch.bind(this);
this.addedToCartObjectIDsAfterSearch =
addedToCartObjectIDsAfterSearch.bind(this);
this.convertedObjectIDs = convertedObjectIDs.bind(this);
this.addedToCartObjectIDs = addedToCartObjectIDs.bind(this);
this.purchasedObjectIDs = purchasedObjectIDs.bind(this);
this.convertedFilters = convertedFilters.bind(this);

this.viewedObjectIDs = viewedObjectIDs.bind(this);
Expand Down
9 changes: 9 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,17 @@ export type InsightsClient = (<MethodName extends keyof InsightsMethodMap>(
) => void) & { version?: string };

export type InsightsEventType = "click" | "conversion" | "view";
export type InsightsEventConversionSubType = "addToCart" | "purchase";

export type InsightsEventObjectData = {
price?: number;
discount?: number;
quantity?: number;
};

export type InsightsEvent = {
eventType: InsightsEventType;
eventSubtype?: InsightsEventConversionSubType;

eventName: string;
userToken?: string | number;
Expand All @@ -90,6 +98,7 @@ export type InsightsEvent = {
queryID?: string;
objectIDs?: string[];
positions?: number[];
objectData?: InsightsEventObjectData[];

filters?: string[];
};
Expand Down

0 comments on commit beb9ef2

Please sign in to comment.