Skip to content

Commit

Permalink
Track checkout session start event
Browse files Browse the repository at this point in the history
  • Loading branch information
polmiro committed Jan 30, 2025
1 parent a2f7af8 commit 2837820
Show file tree
Hide file tree
Showing 8 changed files with 473 additions and 223 deletions.
18 changes: 15 additions & 3 deletions src/behavioural-events/events-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@ export interface IEventsTracker {
export interface CheckoutSessionStartParams {
appUserId: string;
userIsAnonymous: boolean;
customizationOptions: Record<string, string | boolean> | null;
entitlementAlreadyExists: boolean;
customizationOptions: {
colorButtonsPrimary: string;
colorAccent: string;
colorError: string;
colorProductInfoBg: string;
colorFormBg: string;
colorPageBg: string;
font: string;
shapes: string;
showProductDescription: boolean;
} | null;
productInterval: string | null;
productPrice: number;
productCurrency: string;
Expand All @@ -40,6 +51,7 @@ export default class EventsTracker implements IEventsTracker {
private readonly traceId: string = uuid();
private readonly eventsUrl: string;
private readonly flushManager: FlushManager;
private checkoutSessionId: string | null = null;

constructor(
private readonly apiKey: string,
Expand Down Expand Up @@ -70,11 +82,11 @@ export default class EventsTracker implements IEventsTracker {
}

public async trackCheckoutSessionStart(params: CheckoutSessionStartParams) {
const checkoutSessionId = uuid();
this.checkoutSessionId = uuid();

const event = new CheckoutSessionStartEvent({
traceId: this.traceId,
checkoutSessionId: checkoutSessionId,
checkoutSessionId: this.checkoutSessionId,
...params,
});
this.trackEvent(event);
Expand Down
18 changes: 16 additions & 2 deletions src/behavioural-events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,22 @@ export class SDKInitializedEvent extends BaseEvent {
}
}

interface CheckoutSessionStartEventData extends CheckoutSessionEventData {
customizationOptions: Record<string, string | boolean> | null;
interface CustomizationOptionsEventData {
colorButtonsPrimary: string;
colorAccent: string;
colorError: string;
colorProductInfoBg: string;
colorFormBg: string;
colorPageBg: string;
font: string;
shapes: string;
showProductDescription: boolean;
}

export interface CheckoutSessionStartEventData
extends CheckoutSessionEventData {
entitlementAlreadyExists: boolean;
customizationOptions: CustomizationOptionsEventData | null;
productInterval: string | null;
productPrice: number;
productCurrency: string;
Expand Down
9 changes: 8 additions & 1 deletion src/helpers/camel-to-underscore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ export function camelToUnderscore(
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const underscoreKey = key.replace(/([A-Z])/g, "_$1").toLowerCase();
result[underscoreKey] = obj[key];
const value = obj[key];
if (value && typeof value === "object" && !Array.isArray(value)) {
result[underscoreKey] = camelToUnderscore(
value as Record<string, unknown>,
);
} else {
result[underscoreKey] = value;
}
}
}
return result;
Expand Down
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ export class Purchases {

const asModal = !htmlTarget;
const appUserId = this._appUserId;
const userIsAnonymous = this.userIsAnonymous(this._appUserId);

Logger.debugLog(
`Presenting purchase form for package ${rcPackage.identifier}`,
Expand All @@ -564,6 +565,7 @@ export class Purchases {
target: certainHTMLTarget,
props: {
appUserId,
userIsAnonymous,
rcPackage,
purchaseOption,
customerEmail,
Expand All @@ -588,7 +590,9 @@ export class Purchases {
reject(PurchasesError.getForPurchasesFlowError(e));
},
purchases: this,
eventsTracker: this.eventsTracker,
brandingInfo: this._brandingInfo,
customerInfo: this._customerInfo,
purchaseOperationHelper: this.purchaseOperationHelper,
asModal,
selectedLocale: localeToBeUsed,
Expand Down
7 changes: 6 additions & 1 deletion src/tests/base.purchases_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Purchases } from "../main";
import { setupServer } from "msw/node";
import { APIGetRequest, getRequestHandlers } from "./test-responses";
import {
APIGetRequest,
APIPostRequest,
getRequestHandlers,
} from "./test-responses";
import { afterAll, beforeAll, beforeEach } from "vitest";

export const testApiKey = "rcb_test_api_key";
Expand All @@ -14,6 +18,7 @@ beforeAll(() => {

beforeEach(() => {
APIGetRequest.mockReset();
APIPostRequest.mockReset();
if (Purchases.isConfigured()) {
Purchases.getSharedInstance().close();
}
Expand Down
Loading

0 comments on commit 2837820

Please sign in to comment.