Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion examples/webbilling-demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/behavioural-events/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type EventData = {
appUserId: string;
properties: EventProperties;
context: EventContext;
workflowIdentifier?: string;
};

type EventContextSingleValue = string | number | boolean;
Expand Down Expand Up @@ -67,6 +68,9 @@ export class Event {
properties: {
...this.data.properties,
traceId: this.data.traceId,
...(this.data.workflowIdentifier !== undefined
? { workflowIdentifier: this.data.workflowIdentifier }
: {}),
},
}) as EventPayload;
}
Expand Down
5 changes: 5 additions & 0 deletions src/behavioural-events/events-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
buildEventContext,
type SDKEventContextSource,
} from "./sdk-event-context";
import type { WorkflowContext } from "../entities/purchases-config";

const MIN_INTERVAL_RETRY = 2_000;
const MAX_INTERVAL_RETRY = 5 * 60_000;
Expand All @@ -26,6 +27,7 @@ export interface EventsTrackerProps {
appUserId: string;
rcSource: string | null;
silent?: boolean;
workflowContext?: WorkflowContext;
}

export interface IEventsTracker {
Expand All @@ -49,13 +51,15 @@ export default class EventsTracker implements IEventsTracker {
private appUserId: string;
private readonly isSilent: boolean;
private rcSource: string | null;
private readonly workflowContext?: WorkflowContext;

constructor(props: EventsTrackerProps) {
this.apiKey = props.apiKey;
this.eventsUrl = `${RC_ANALYTICS_ENDPOINT}/v1/events`;
this.appUserId = props.appUserId;
this.isSilent = props.silent || false;
this.rcSource = props.rcSource;
this.workflowContext = props.workflowContext;
this.flushManager = new FlushManager(
MIN_INTERVAL_RETRY,
MAX_INTERVAL_RETRY,
Expand Down Expand Up @@ -91,6 +95,7 @@ export default class EventsTracker implements IEventsTracker {
traceId: this.traceId,
appUserId: this.appUserId,
context: buildEventContext(props.source, this.rcSource),
workflowIdentifier: this.workflowContext?.workflowIdentifier,
properties: props.properties || {},
});
this.eventsQueue.push(event);
Expand Down
27 changes: 27 additions & 0 deletions src/entities/purchases-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import type { FlagsConfig } from "./flags-config";
import type { HttpConfig } from "./http-config";

/**
* Contextual information specific to workflows.
* @internal
*/
export interface WorkflowContext {
/**
* Optional identifier to group events emitted by the SDK.
*/
workflowIdentifier?: string;
}

/**
* Additional context to be associated with the configured Purchases instance.
* @internal
*/
export interface PurchasesContext {
/**
* Optional workflow-specific context shared across SDK components.
*/
workflowContext?: WorkflowContext;
}

/**
* Configuration object for initializing the Purchases SDK.
*
Expand Down Expand Up @@ -42,4 +64,9 @@ export interface PurchasesConfig {
* Advanced functionality configuration {@link FlagsConfig}.
*/
flags?: FlagsConfig;
/**
* Additional contextual information for the Purchases instance.
* @internal
*/
context?: PurchasesContext;
}
14 changes: 12 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ import {
type FlagsConfig,
supportedRCSources,
} from "./entities/flags-config";
import { type PurchasesConfig } from "./entities/purchases-config";
import {
type PurchasesConfig,
type PurchasesContext,
} from "./entities/purchases-config";
import { generateUUID } from "./helpers/uuid-helper";
import type { PlatformInfo } from "./entities/platform-info";
import type { ReservedCustomerAttribute } from "./entities/attributes";
Expand Down Expand Up @@ -164,6 +167,9 @@ export class Purchases {
/** @internal */
private readonly _flags: FlagsConfig;

/** @internal */
private readonly _context?: PurchasesContext;

/** @internal */
private readonly backend: Backend;

Expand Down Expand Up @@ -304,7 +310,7 @@ export class Purchases {
}

private static configureInternal(config: PurchasesConfig): void {
const { apiKey, appUserId, httpConfig, flags } = config;
const { apiKey, appUserId, httpConfig, flags, context } = config;
const finalHttpConfig = httpConfig ?? defaultHttpConfig;
const finalFlags = flags ?? defaultFlagsConfig;

Expand All @@ -314,6 +320,7 @@ export class Purchases {
appUserId,
finalHttpConfig,
finalFlags,
context,
);
}

Expand Down Expand Up @@ -376,10 +383,12 @@ export class Purchases {
appUserId: string,
httpConfig: HttpConfig = defaultHttpConfig,
flags: FlagsConfig = defaultFlagsConfig,
context?: PurchasesContext,
) {
this._API_KEY = apiKey;
this._appUserId = appUserId;
this._flags = { ...defaultFlagsConfig, ...flags };
this._context = context;
if (RC_ENDPOINT === undefined) {
Logger.errorLog(
"Project was build without some of the environment variables set",
Expand All @@ -397,6 +406,7 @@ export class Purchases {
appUserId: this._appUserId,
silent: !this._flags.collectAnalyticsEvents,
rcSource: this._flags.rcSource ?? null,
workflowContext: this._context?.workflowContext,
});
this.backend = new Backend(this._API_KEY, httpConfig);
this.inMemoryCache = new InMemoryCache();
Expand Down
33 changes: 33 additions & 0 deletions src/tests/behavioral-events/events-tracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,39 @@ describe("EventsTracker", (test) => {
);
});

test("passes the workflow identifier to the event", async () => {
const eventsTracker = new EventsTracker({
apiKey: testApiKey,
appUserId: "someAppUserId",
rcSource: "rcSource",
workflowContext: { workflowIdentifier: "workflow-id" },
});

eventsTracker.trackExternalEvent({
eventName: "external",
source: "sdk",
properties: { a: "b" },
});

await vi.advanceTimersToNextTimerAsync();

expect(APIPostRequest).toHaveBeenCalledWith(
expect.objectContaining({
json: expect.objectContaining({
events: expect.arrayContaining([
expect.objectContaining({
properties: expect.objectContaining({
workflow_identifier: "workflow-id",
}),
}),
]),
}),
}),
);

eventsTracker.dispose();
});

test<EventsTrackerFixtures>("retries tracking events exponentially", async ({
eventsTracker,
}) => {
Expand Down
1 change: 1 addition & 0 deletions src/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe("Purchases.configure()", () => {
appUserId: testUserId,
httpConfig: {},
flags: { autoCollectUTMAsMetadata: false },
context: { workflowContext: { workflowIdentifier: "workflow-id" } },
});
expect(purchases).toBeDefined();
});
Expand Down