@funnelsgrove/analytics 0.1.32 → 0.1.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type FunnelStepExitReason, type FunnelStepType } from '@funnelsgrove/runtime';
|
|
1
2
|
export type PublicSdkTrackEventInput = {
|
|
2
3
|
eventType: string;
|
|
3
4
|
userId?: string;
|
|
@@ -9,6 +10,9 @@ export type PublicSdkTrackEventInput = {
|
|
|
9
10
|
stepType?: string;
|
|
10
11
|
startedAt?: string;
|
|
11
12
|
endedAt?: string;
|
|
13
|
+
durationMs?: number;
|
|
14
|
+
exitReason?: FunnelStepExitReason;
|
|
15
|
+
stepContractVersion?: number;
|
|
12
16
|
selected?: Record<string, unknown>;
|
|
13
17
|
payload?: Record<string, unknown>;
|
|
14
18
|
context?: Record<string, unknown>;
|
|
@@ -20,8 +24,10 @@ export type PublicSdkStepStartInput = {
|
|
|
20
24
|
environment?: PublicSdkAnalyticsEnvironment;
|
|
21
25
|
stepId: string;
|
|
22
26
|
stepName: string;
|
|
23
|
-
stepType
|
|
27
|
+
stepType: FunnelStepType;
|
|
28
|
+
stepContractVersion: number;
|
|
24
29
|
startedAt: string;
|
|
30
|
+
metadata?: Record<string, unknown>;
|
|
25
31
|
featureFlags?: Record<string, string | null | undefined>;
|
|
26
32
|
};
|
|
27
33
|
export type PublicSdkStepEndInput = {
|
|
@@ -29,10 +35,26 @@ export type PublicSdkStepEndInput = {
|
|
|
29
35
|
environment?: PublicSdkAnalyticsEnvironment;
|
|
30
36
|
stepId: string;
|
|
31
37
|
stepName: string;
|
|
32
|
-
stepType
|
|
38
|
+
stepType: FunnelStepType;
|
|
39
|
+
stepContractVersion: number;
|
|
33
40
|
startedAt: string;
|
|
34
41
|
endedAt: string;
|
|
35
42
|
selected?: Record<string, unknown>;
|
|
43
|
+
metadata?: Record<string, unknown>;
|
|
44
|
+
featureFlags?: Record<string, string | null | undefined>;
|
|
45
|
+
};
|
|
46
|
+
export type PublicSdkStepExitInput = {
|
|
47
|
+
userId?: string;
|
|
48
|
+
environment?: PublicSdkAnalyticsEnvironment;
|
|
49
|
+
eventId?: string;
|
|
50
|
+
stepId: string;
|
|
51
|
+
stepName: string;
|
|
52
|
+
stepType: FunnelStepType;
|
|
53
|
+
startedAt: string;
|
|
54
|
+
endedAt: string;
|
|
55
|
+
exitReason: FunnelStepExitReason;
|
|
56
|
+
stepContractVersion: number;
|
|
57
|
+
metadata?: Record<string, unknown>;
|
|
36
58
|
featureFlags?: Record<string, string | null | undefined>;
|
|
37
59
|
};
|
|
38
60
|
export type PublicSdkStepEngagedInput = {
|
|
@@ -40,11 +62,13 @@ export type PublicSdkStepEngagedInput = {
|
|
|
40
62
|
environment?: PublicSdkAnalyticsEnvironment;
|
|
41
63
|
stepId: string;
|
|
42
64
|
stepName: string;
|
|
43
|
-
stepType
|
|
65
|
+
stepType: FunnelStepType;
|
|
66
|
+
stepContractVersion: number;
|
|
44
67
|
startedAt: string;
|
|
45
68
|
engagedAt: string;
|
|
46
69
|
durationMs: number;
|
|
47
70
|
engagementThresholdMs: number;
|
|
71
|
+
metadata?: Record<string, unknown>;
|
|
48
72
|
featureFlags?: Record<string, string | null | undefined>;
|
|
49
73
|
};
|
|
50
74
|
export type PublicSdkConversionCallbackInput = {
|
|
@@ -55,9 +79,22 @@ export type PublicSdkConversionCallbackInput = {
|
|
|
55
79
|
stepId?: string;
|
|
56
80
|
stepName?: string;
|
|
57
81
|
stepType?: string;
|
|
82
|
+
stepContractVersion?: number;
|
|
58
83
|
metadata?: Record<string, unknown>;
|
|
59
84
|
featureFlags?: Record<string, string | null | undefined>;
|
|
60
85
|
};
|
|
86
|
+
export type PublicSdkFunnelConversionCallbackInput = PublicSdkConversionCallbackInput & {
|
|
87
|
+
stepContractVersion: number;
|
|
88
|
+
};
|
|
89
|
+
export type PublicSdkStepConversionCallbackInput = Omit<PublicSdkConversionCallbackInput, 'stepContractVersion' | 'stepId' | 'stepName' | 'stepType'> & {
|
|
90
|
+
stepContractVersion: number;
|
|
91
|
+
stepId: string;
|
|
92
|
+
stepName: string;
|
|
93
|
+
stepType: FunnelStepType;
|
|
94
|
+
};
|
|
95
|
+
export type PublicSdkEmailCapturedBrowserInput = Omit<PublicSdkConversionCallbackInput, 'eventId'> & {
|
|
96
|
+
eventId: string;
|
|
97
|
+
};
|
|
61
98
|
export type PublicSdkIdentifyInput = {
|
|
62
99
|
user_id?: string;
|
|
63
100
|
email?: string;
|
|
@@ -87,19 +124,25 @@ export declare class PublicAnalyticsSdkService {
|
|
|
87
124
|
payments: unknown[];
|
|
88
125
|
} | null>;
|
|
89
126
|
track(event: PublicSdkTrackEventInput): string | null;
|
|
127
|
+
trackEmailCapturedBrowserDestinations(input: PublicSdkEmailCapturedBrowserInput): void;
|
|
128
|
+
private trackBrowserDestinations;
|
|
129
|
+
private trackTrusted;
|
|
130
|
+
private trackContractCallback;
|
|
90
131
|
trackStepStarted(input: PublicSdkStepStartInput): string | null;
|
|
91
132
|
trackStepCompleted(input: PublicSdkStepEndInput): string | null;
|
|
133
|
+
trackStepExited(input: PublicSdkStepExitInput): string | null;
|
|
92
134
|
trackStepEngaged(input: PublicSdkStepEngagedInput): string | null;
|
|
93
|
-
trackFunnelStarted(input:
|
|
94
|
-
trackFirstStepClicked(input:
|
|
95
|
-
trackFirstStepViewed(input:
|
|
135
|
+
trackFunnelStarted(input: PublicSdkFunnelConversionCallbackInput): string | null;
|
|
136
|
+
trackFirstStepClicked(input: PublicSdkStepConversionCallbackInput): string | null;
|
|
137
|
+
trackFirstStepViewed(input: PublicSdkStepConversionCallbackInput): string | null;
|
|
138
|
+
/** @deprecated Legacy contract-v1 compatibility only; v2 funnels use submitEmailCapture. */
|
|
96
139
|
trackLead(input: PublicSdkConversionCallbackInput): string | null;
|
|
97
|
-
trackFunnelCompleted(input:
|
|
98
|
-
trackCheckoutStarted(input:
|
|
99
|
-
trackPaymentInfoSubmitted(input:
|
|
100
|
-
trackCheckoutCompleted(input:
|
|
101
|
-
trackPaymentCheckoutSucceeded(input:
|
|
102
|
-
trackPaymentCheckoutReturned(input:
|
|
140
|
+
trackFunnelCompleted(input: PublicSdkStepConversionCallbackInput): string | null;
|
|
141
|
+
trackCheckoutStarted(input: PublicSdkStepConversionCallbackInput): string | null;
|
|
142
|
+
trackPaymentInfoSubmitted(input: PublicSdkStepConversionCallbackInput): string | null;
|
|
143
|
+
trackCheckoutCompleted(input: PublicSdkStepConversionCallbackInput): string | null;
|
|
144
|
+
trackPaymentCheckoutSucceeded(input: PublicSdkStepConversionCallbackInput): string | null;
|
|
145
|
+
trackPaymentCheckoutReturned(input: PublicSdkStepConversionCallbackInput): string | null;
|
|
103
146
|
flush(): Promise<number>;
|
|
104
147
|
private ensureUserId;
|
|
105
148
|
private resolveBootstrapUserId;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FUNNEL_ANALYTICS_EVENT_CONTRACTS, FUNNEL_STEP_CONTRACTS, } from '@funnelsgrove/runtime';
|
|
1
2
|
import { buildMainApiUrl, buildSdkHeaders, FUNNEL_ID, FUNNEL_VERSION_ID, getFunnelSdkPublishableKey, hasPostHogRuntimeConfig, POSTHOG_API_HOST, POSTHOG_PROJECT_API_KEY, PROJECT_ID, } from './runtime-api.config.js';
|
|
2
3
|
import { canUseDom, getOrCreateUserId, persistUserId, readStoredUserId, } from './sdk-user-id.storage.js';
|
|
3
4
|
import { mapAnalyticsEventToMetaPixelEvent, metaPixelService, } from './meta-pixel.service.js';
|
|
@@ -9,6 +10,10 @@ const DEFAULT_OPTIONS = {
|
|
|
9
10
|
};
|
|
10
11
|
const POSTHOG_BATCH_ENDPOINT = '/batch/';
|
|
11
12
|
const DEFAULT_FUNNEL_DOMAIN_SIGNAL = 'funnelsgrove';
|
|
13
|
+
const RESERVED_ANALYTICS_EVENT_TYPES = new Set(Object.values(FUNNEL_ANALYTICS_EVENT_CONTRACTS).flatMap((contract) => [
|
|
14
|
+
contract.rawName,
|
|
15
|
+
contract.publicName,
|
|
16
|
+
]));
|
|
12
17
|
const normalizeEnvironment = (value) => {
|
|
13
18
|
if (typeof value !== 'string') {
|
|
14
19
|
return null;
|
|
@@ -85,6 +90,11 @@ const toFiniteNumber = (value) => {
|
|
|
85
90
|
}
|
|
86
91
|
return value;
|
|
87
92
|
};
|
|
93
|
+
const toStepContractVersion = (value) => {
|
|
94
|
+
const version = toFiniteNumber(value);
|
|
95
|
+
return Number.isSafeInteger(version) && (version !== null && version !== void 0 ? version : 0) >= 1 ? version : null;
|
|
96
|
+
};
|
|
97
|
+
const isKnownStepType = (value) => (Boolean(value && Object.prototype.hasOwnProperty.call(FUNNEL_STEP_CONTRACTS, value)));
|
|
88
98
|
const toDurationMs = (startedAt, endedAt) => {
|
|
89
99
|
if (!startedAt || !endedAt) {
|
|
90
100
|
return null;
|
|
@@ -111,6 +121,8 @@ const toPostHogEventName = (eventType) => {
|
|
|
111
121
|
return 'step_started';
|
|
112
122
|
case 'step_end':
|
|
113
123
|
return 'step_completed';
|
|
124
|
+
case 'step_exit':
|
|
125
|
+
return 'step_exited';
|
|
114
126
|
default:
|
|
115
127
|
return eventType;
|
|
116
128
|
}
|
|
@@ -228,30 +240,57 @@ export class PublicAnalyticsSdkService {
|
|
|
228
240
|
}
|
|
229
241
|
}
|
|
230
242
|
track(event) {
|
|
231
|
-
var _a, _b, _c, _d;
|
|
232
243
|
const eventType = asString(event.eventType);
|
|
233
|
-
if (!eventType) {
|
|
244
|
+
if (!eventType || RESERVED_ANALYTICS_EVENT_TYPES.has(eventType)) {
|
|
234
245
|
return null;
|
|
235
246
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
247
|
+
return this.trackTrusted(event);
|
|
248
|
+
}
|
|
249
|
+
trackEmailCapturedBrowserDestinations(input) {
|
|
250
|
+
const eventId = asString(input.eventId);
|
|
251
|
+
if (!eventId) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
this.trackBrowserDestinations('email_captured', eventId, buildDefinedRecord(Object.assign(Object.assign({}, asRecord(input.metadata)), { stepId: asString(input.stepId) || undefined, stepName: asString(input.stepName) || undefined, stepType: asString(input.stepType) || undefined })));
|
|
255
|
+
}
|
|
256
|
+
trackBrowserDestinations(eventType, eventId, metadata) {
|
|
239
257
|
const metaPixelEvent = mapAnalyticsEventToMetaPixelEvent({
|
|
240
258
|
eventType,
|
|
241
259
|
eventId,
|
|
242
|
-
metadata
|
|
260
|
+
metadata,
|
|
243
261
|
});
|
|
244
262
|
if (metaPixelEvent) {
|
|
245
|
-
|
|
263
|
+
try {
|
|
264
|
+
metaPixelService.track(metaPixelEvent);
|
|
265
|
+
}
|
|
266
|
+
catch (_a) {
|
|
267
|
+
// Browser destinations are best effort and independent.
|
|
268
|
+
}
|
|
246
269
|
}
|
|
247
270
|
const googleTagEvents = mapAnalyticsEventToGoogleTagEvents({
|
|
248
271
|
eventType,
|
|
249
272
|
eventId,
|
|
250
|
-
metadata
|
|
273
|
+
metadata,
|
|
251
274
|
});
|
|
252
275
|
for (const googleTagEvent of googleTagEvents) {
|
|
253
|
-
|
|
276
|
+
try {
|
|
277
|
+
googleTagService.track(googleTagEvent);
|
|
278
|
+
}
|
|
279
|
+
catch (_b) {
|
|
280
|
+
// Browser destinations are best effort and independent.
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
trackTrusted(event) {
|
|
285
|
+
var _a, _b, _c, _d, _e, _f;
|
|
286
|
+
const eventType = asString(event.eventType);
|
|
287
|
+
if (!eventType) {
|
|
288
|
+
return null;
|
|
254
289
|
}
|
|
290
|
+
const eventId = asString(event.eventId) || buildEventId();
|
|
291
|
+
const metadata = buildEventMetadata(eventType, asRecord(event.metadata));
|
|
292
|
+
const providerMetadata = buildDefinedRecord(Object.assign(Object.assign({}, metadata), { stepId: asString(event.stepId) || metadata.stepId, stepName: asString(event.stepName) || metadata.stepName, stepType: asString(event.stepType) || metadata.stepType }));
|
|
293
|
+
this.trackBrowserDestinations(eventType, eventId, providerMetadata);
|
|
255
294
|
if (!getFunnelSdkPublishableKey()) {
|
|
256
295
|
return eventId;
|
|
257
296
|
}
|
|
@@ -266,8 +305,10 @@ export class PublicAnalyticsSdkService {
|
|
|
266
305
|
stepType: asString(event.stepType) || asString(metadata.stepType) || asString(metadata.step_type),
|
|
267
306
|
startedAt: asString(event.startedAt),
|
|
268
307
|
endedAt: asString(event.endedAt),
|
|
269
|
-
durationMs: (
|
|
270
|
-
|
|
308
|
+
durationMs: (_c = (_a = toFiniteNumber(event.durationMs)) !== null && _a !== void 0 ? _a : toFiniteNumber((_b = event.metadata) === null || _b === void 0 ? void 0 : _b.durationMs)) !== null && _c !== void 0 ? _c : null,
|
|
309
|
+
exitReason: (_d = event.exitReason) !== null && _d !== void 0 ? _d : null,
|
|
310
|
+
stepContractVersion: toFiniteNumber(event.stepContractVersion),
|
|
311
|
+
engagementThresholdMs: (_f = toFiniteNumber((_e = event.metadata) === null || _e === void 0 ? void 0 : _e.engagementThresholdMs)) !== null && _f !== void 0 ? _f : null,
|
|
271
312
|
selected: asRecord(event.selected),
|
|
272
313
|
payload: asRecord(event.payload),
|
|
273
314
|
context: asRecord(event.context),
|
|
@@ -282,16 +323,56 @@ export class PublicAnalyticsSdkService {
|
|
|
282
323
|
this.scheduleFlush();
|
|
283
324
|
return envelope.eventId;
|
|
284
325
|
}
|
|
326
|
+
trackContractCallback(eventName, input) {
|
|
327
|
+
const contract = FUNNEL_ANALYTICS_EVENT_CONTRACTS[eventName];
|
|
328
|
+
const stepContractVersion = toStepContractVersion(input.stepContractVersion);
|
|
329
|
+
if (stepContractVersion === null) {
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
const stepId = asString(input.stepId);
|
|
333
|
+
const stepName = asString(input.stepName);
|
|
334
|
+
const stepType = asString(input.stepType);
|
|
335
|
+
const requiresStepMetadata = contract.requiredMetadata.some((field) => field === 'stepId');
|
|
336
|
+
if (requiresStepMetadata && (!stepId || !stepName || !stepType)) {
|
|
337
|
+
return null;
|
|
338
|
+
}
|
|
339
|
+
if (stepType && !isKnownStepType(stepType)) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
if (Array.isArray(contract.allowedStepTypes)
|
|
343
|
+
&& !contract.allowedStepTypes.some((allowedType) => allowedType === stepType)) {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
if (Array.isArray(contract.allowedStepIdentities)
|
|
347
|
+
&& !contract.allowedStepIdentities.includes(stepId !== null && stepId !== void 0 ? stepId : '')) {
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
return this.trackTrusted({
|
|
351
|
+
eventType: contract.rawName,
|
|
352
|
+
userId: input.userId,
|
|
353
|
+
environment: input.environment,
|
|
354
|
+
eventId: input.eventId,
|
|
355
|
+
occurredAt: input.occurredAt,
|
|
356
|
+
stepId: stepId !== null && stepId !== void 0 ? stepId : undefined,
|
|
357
|
+
stepName: stepName !== null && stepName !== void 0 ? stepName : undefined,
|
|
358
|
+
stepType: stepType !== null && stepType !== void 0 ? stepType : undefined,
|
|
359
|
+
stepContractVersion: stepContractVersion,
|
|
360
|
+
metadata: input.metadata,
|
|
361
|
+
featureFlags: input.featureFlags,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
285
364
|
trackStepStarted(input) {
|
|
286
|
-
return this.
|
|
365
|
+
return this.trackTrusted({
|
|
287
366
|
eventType: 'step_start',
|
|
288
367
|
userId: input.userId,
|
|
289
368
|
environment: input.environment,
|
|
290
369
|
stepId: input.stepId,
|
|
291
370
|
stepName: input.stepName,
|
|
292
371
|
stepType: input.stepType,
|
|
372
|
+
stepContractVersion: input.stepContractVersion,
|
|
293
373
|
startedAt: input.startedAt,
|
|
294
374
|
occurredAt: input.startedAt,
|
|
375
|
+
metadata: input.metadata,
|
|
295
376
|
featureFlags: input.featureFlags,
|
|
296
377
|
});
|
|
297
378
|
}
|
|
@@ -299,85 +380,93 @@ export class PublicAnalyticsSdkService {
|
|
|
299
380
|
var _a;
|
|
300
381
|
const startedAt = asString(input.startedAt);
|
|
301
382
|
const endedAt = asString(input.endedAt);
|
|
302
|
-
return this.
|
|
383
|
+
return this.trackTrusted({
|
|
303
384
|
eventType: 'step_end',
|
|
304
385
|
userId: input.userId,
|
|
305
386
|
environment: input.environment,
|
|
306
387
|
stepId: input.stepId,
|
|
307
388
|
stepName: input.stepName,
|
|
308
389
|
stepType: input.stepType,
|
|
390
|
+
stepContractVersion: input.stepContractVersion,
|
|
309
391
|
startedAt: startedAt || input.startedAt,
|
|
310
392
|
endedAt: endedAt || input.endedAt,
|
|
311
393
|
occurredAt: input.endedAt,
|
|
312
394
|
selected: asRecord(input.selected),
|
|
313
|
-
metadata: {
|
|
314
|
-
durationMs: (_a = toDurationMs(startedAt, endedAt)) !== null && _a !== void 0 ? _a : undefined,
|
|
315
|
-
},
|
|
395
|
+
metadata: Object.assign(Object.assign({}, asRecord(input.metadata)), { durationMs: (_a = toDurationMs(startedAt, endedAt)) !== null && _a !== void 0 ? _a : undefined }),
|
|
316
396
|
featureFlags: input.featureFlags,
|
|
317
397
|
});
|
|
318
398
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
stepName: input.stepName,
|
|
326
|
-
stepType: input.stepType,
|
|
327
|
-
startedAt: input.startedAt,
|
|
328
|
-
endedAt: input.engagedAt,
|
|
329
|
-
occurredAt: input.engagedAt,
|
|
330
|
-
metadata: {
|
|
331
|
-
durationMs: input.durationMs,
|
|
332
|
-
engagementThresholdMs: input.engagementThresholdMs,
|
|
333
|
-
},
|
|
334
|
-
featureFlags: input.featureFlags,
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
trackFunnelStarted(input) {
|
|
338
|
-
return this.track({
|
|
339
|
-
eventType: 'funnel_start',
|
|
399
|
+
trackStepExited(input) {
|
|
400
|
+
var _a;
|
|
401
|
+
const startedAt = asString(input.startedAt);
|
|
402
|
+
const endedAt = asString(input.endedAt);
|
|
403
|
+
const eventId = this.trackTrusted({
|
|
404
|
+
eventType: 'step_exit',
|
|
340
405
|
userId: input.userId,
|
|
341
406
|
environment: input.environment,
|
|
342
407
|
eventId: input.eventId,
|
|
343
|
-
occurredAt: input.occurredAt,
|
|
344
408
|
stepId: input.stepId,
|
|
345
409
|
stepName: input.stepName,
|
|
346
410
|
stepType: input.stepType,
|
|
411
|
+
startedAt: startedAt || input.startedAt,
|
|
412
|
+
endedAt: endedAt || input.endedAt,
|
|
413
|
+
occurredAt: input.endedAt,
|
|
414
|
+
durationMs: (_a = toDurationMs(startedAt, endedAt)) !== null && _a !== void 0 ? _a : undefined,
|
|
415
|
+
exitReason: input.exitReason,
|
|
416
|
+
stepContractVersion: input.stepContractVersion,
|
|
347
417
|
metadata: input.metadata,
|
|
348
418
|
featureFlags: input.featureFlags,
|
|
349
419
|
});
|
|
420
|
+
if (input.exitReason === 'abandon') {
|
|
421
|
+
void this.flush();
|
|
422
|
+
}
|
|
423
|
+
return eventId;
|
|
350
424
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
425
|
+
trackStepEngaged(input) {
|
|
426
|
+
const stepId = asString(input.stepId);
|
|
427
|
+
const stepName = asString(input.stepName);
|
|
428
|
+
const stepType = asString(input.stepType);
|
|
429
|
+
const stepContractVersion = toStepContractVersion(input.stepContractVersion);
|
|
430
|
+
const durationMs = toFiniteNumber(input.durationMs);
|
|
431
|
+
const engagementThresholdMs = toFiniteNumber(input.engagementThresholdMs);
|
|
432
|
+
if (!stepId
|
|
433
|
+
|| !stepName
|
|
434
|
+
|| !isKnownStepType(stepType)
|
|
435
|
+
|| stepContractVersion === null
|
|
436
|
+
|| durationMs === null
|
|
437
|
+
|| durationMs < 0
|
|
438
|
+
|| engagementThresholdMs === null
|
|
439
|
+
|| engagementThresholdMs < 0) {
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
return this.trackTrusted({
|
|
443
|
+
eventType: 'step_engaged',
|
|
354
444
|
userId: input.userId,
|
|
355
445
|
environment: input.environment,
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
446
|
+
stepId,
|
|
447
|
+
stepName,
|
|
448
|
+
stepType,
|
|
449
|
+
stepContractVersion,
|
|
450
|
+
startedAt: input.startedAt,
|
|
451
|
+
endedAt: input.engagedAt,
|
|
452
|
+
occurredAt: input.engagedAt,
|
|
453
|
+
metadata: Object.assign(Object.assign({}, asRecord(input.metadata)), { durationMs,
|
|
454
|
+
engagementThresholdMs }),
|
|
362
455
|
featureFlags: input.featureFlags,
|
|
363
456
|
});
|
|
364
457
|
}
|
|
458
|
+
trackFunnelStarted(input) {
|
|
459
|
+
return this.trackContractCallback('funnel_started', input);
|
|
460
|
+
}
|
|
461
|
+
trackFirstStepClicked(input) {
|
|
462
|
+
return this.trackContractCallback('first_step_clicked', input);
|
|
463
|
+
}
|
|
365
464
|
trackFirstStepViewed(input) {
|
|
366
|
-
return this.
|
|
367
|
-
eventType: 'first_step_viewed',
|
|
368
|
-
userId: input.userId,
|
|
369
|
-
environment: input.environment,
|
|
370
|
-
eventId: input.eventId,
|
|
371
|
-
occurredAt: input.occurredAt,
|
|
372
|
-
stepId: input.stepId,
|
|
373
|
-
stepName: input.stepName,
|
|
374
|
-
stepType: input.stepType,
|
|
375
|
-
metadata: input.metadata,
|
|
376
|
-
featureFlags: input.featureFlags,
|
|
377
|
-
});
|
|
465
|
+
return this.trackContractCallback('first_step_viewed', input);
|
|
378
466
|
}
|
|
467
|
+
/** @deprecated Legacy contract-v1 compatibility only; v2 funnels use submitEmailCapture. */
|
|
379
468
|
trackLead(input) {
|
|
380
|
-
return this.
|
|
469
|
+
return this.trackTrusted({
|
|
381
470
|
eventType: 'email_captured',
|
|
382
471
|
userId: input.userId,
|
|
383
472
|
environment: input.environment,
|
|
@@ -386,68 +475,25 @@ export class PublicAnalyticsSdkService {
|
|
|
386
475
|
stepId: input.stepId,
|
|
387
476
|
stepName: input.stepName,
|
|
388
477
|
stepType: input.stepType,
|
|
478
|
+
stepContractVersion: input.stepContractVersion,
|
|
389
479
|
metadata: input.metadata,
|
|
390
480
|
featureFlags: input.featureFlags,
|
|
391
481
|
});
|
|
392
482
|
}
|
|
393
483
|
trackFunnelCompleted(input) {
|
|
394
|
-
return this.
|
|
395
|
-
eventType: 'funnel_completed',
|
|
396
|
-
userId: input.userId,
|
|
397
|
-
environment: input.environment,
|
|
398
|
-
eventId: input.eventId,
|
|
399
|
-
occurredAt: input.occurredAt,
|
|
400
|
-
stepId: input.stepId,
|
|
401
|
-
stepName: input.stepName,
|
|
402
|
-
stepType: input.stepType,
|
|
403
|
-
metadata: input.metadata,
|
|
404
|
-
featureFlags: input.featureFlags,
|
|
405
|
-
});
|
|
484
|
+
return this.trackContractCallback('funnel_completed', input);
|
|
406
485
|
}
|
|
407
486
|
trackCheckoutStarted(input) {
|
|
408
|
-
return this.
|
|
409
|
-
eventType: 'checkout_started',
|
|
410
|
-
userId: input.userId,
|
|
411
|
-
environment: input.environment,
|
|
412
|
-
eventId: input.eventId,
|
|
413
|
-
occurredAt: input.occurredAt,
|
|
414
|
-
stepId: input.stepId,
|
|
415
|
-
stepName: input.stepName,
|
|
416
|
-
stepType: input.stepType,
|
|
417
|
-
metadata: input.metadata,
|
|
418
|
-
featureFlags: input.featureFlags,
|
|
419
|
-
});
|
|
487
|
+
return this.trackContractCallback('checkout_started', input);
|
|
420
488
|
}
|
|
421
489
|
trackPaymentInfoSubmitted(input) {
|
|
422
|
-
return this.
|
|
423
|
-
eventType: 'add_payment_info',
|
|
424
|
-
userId: input.userId,
|
|
425
|
-
environment: input.environment,
|
|
426
|
-
eventId: input.eventId,
|
|
427
|
-
occurredAt: input.occurredAt,
|
|
428
|
-
stepId: input.stepId,
|
|
429
|
-
stepName: input.stepName,
|
|
430
|
-
stepType: input.stepType,
|
|
431
|
-
metadata: input.metadata,
|
|
432
|
-
featureFlags: input.featureFlags,
|
|
433
|
-
});
|
|
490
|
+
return this.trackContractCallback('add_payment_info', input);
|
|
434
491
|
}
|
|
435
492
|
trackCheckoutCompleted(input) {
|
|
436
|
-
return this.
|
|
437
|
-
eventType: 'checkout_completed',
|
|
438
|
-
userId: input.userId,
|
|
439
|
-
environment: input.environment,
|
|
440
|
-
eventId: input.eventId,
|
|
441
|
-
occurredAt: input.occurredAt,
|
|
442
|
-
stepId: input.stepId,
|
|
443
|
-
stepName: input.stepName,
|
|
444
|
-
stepType: input.stepType,
|
|
445
|
-
metadata: input.metadata,
|
|
446
|
-
featureFlags: input.featureFlags,
|
|
447
|
-
});
|
|
493
|
+
return this.trackContractCallback('checkout_completed', input);
|
|
448
494
|
}
|
|
449
495
|
trackPaymentCheckoutSucceeded(input) {
|
|
450
|
-
return this.
|
|
496
|
+
return this.trackTrusted({
|
|
451
497
|
eventType: 'payment_checkout_succeeded',
|
|
452
498
|
userId: input.userId,
|
|
453
499
|
environment: input.environment,
|
|
@@ -456,12 +502,13 @@ export class PublicAnalyticsSdkService {
|
|
|
456
502
|
stepId: input.stepId,
|
|
457
503
|
stepName: input.stepName,
|
|
458
504
|
stepType: input.stepType,
|
|
505
|
+
stepContractVersion: input.stepContractVersion,
|
|
459
506
|
metadata: input.metadata,
|
|
460
507
|
featureFlags: input.featureFlags,
|
|
461
508
|
});
|
|
462
509
|
}
|
|
463
510
|
trackPaymentCheckoutReturned(input) {
|
|
464
|
-
return this.
|
|
511
|
+
return this.trackTrusted({
|
|
465
512
|
eventType: 'payment_checkout_returned',
|
|
466
513
|
userId: input.userId,
|
|
467
514
|
environment: input.environment,
|
|
@@ -470,6 +517,7 @@ export class PublicAnalyticsSdkService {
|
|
|
470
517
|
stepId: input.stepId,
|
|
471
518
|
stepName: input.stepName,
|
|
472
519
|
stepType: input.stepType,
|
|
520
|
+
stepContractVersion: input.stepContractVersion,
|
|
473
521
|
metadata: input.metadata,
|
|
474
522
|
featureFlags: input.featureFlags,
|
|
475
523
|
});
|
|
@@ -552,13 +600,13 @@ export class PublicAnalyticsSdkService {
|
|
|
552
600
|
}
|
|
553
601
|
const analyticsEnvironment = resolveAnalyticsEnvironment();
|
|
554
602
|
const batch = events.map((event) => {
|
|
555
|
-
var _a, _b;
|
|
603
|
+
var _a, _b, _c;
|
|
556
604
|
const eventEnvironment = event.environment || analyticsEnvironment;
|
|
557
605
|
return {
|
|
558
606
|
event: toPostHogEventName(event.eventType),
|
|
559
607
|
properties: buildDefinedRecord(Object.assign(Object.assign({ distinct_id: event.userId || this.ensureUserId(), $groups: {
|
|
560
608
|
project: PROJECT_ID,
|
|
561
|
-
}, $insert_id: event.eventId, project_id: PROJECT_ID, funnel_id: FUNNEL_ID || undefined, funnel_version_id: FUNNEL_VERSION_ID || undefined, environment: eventEnvironment, test: eventEnvironment === 'test' || undefined, step_id: event.stepId || undefined, step_name: event.stepName || undefined, step_type: event.stepType || undefined, started_at: event.startedAt || undefined, ended_at: event.endedAt || undefined, duration_ms: (_a = event.durationMs) !== null && _a !== void 0 ? _a : undefined,
|
|
609
|
+
}, $insert_id: event.eventId, project_id: PROJECT_ID, funnel_id: FUNNEL_ID || undefined, funnel_version_id: FUNNEL_VERSION_ID || undefined, environment: eventEnvironment, test: eventEnvironment === 'test' || undefined, step_id: event.stepId || undefined, step_name: event.stepName || undefined, step_type: event.stepType || undefined, started_at: event.startedAt || undefined, ended_at: event.endedAt || undefined, duration_ms: (_a = event.durationMs) !== null && _a !== void 0 ? _a : undefined, exit_reason: event.exitReason || undefined, step_contract_version: (_b = event.stepContractVersion) !== null && _b !== void 0 ? _b : undefined, engagement_threshold_ms: (_c = event.engagementThresholdMs) !== null && _c !== void 0 ? _c : undefined, user_agent: currentBrowserUserAgent(), event_source: 'client' }, buildFeatureFlagProperties(event.featureFlags)), { selected: Object.keys(event.selected).length > 0 ? event.selected : undefined, payload: Object.keys(event.payload).length > 0 ? event.payload : undefined, context: Object.keys(event.context).length > 0 ? event.context : undefined, metadata: Object.keys(event.metadata).length > 0 ? event.metadata : undefined })),
|
|
562
610
|
timestamp: event.occurredAt,
|
|
563
611
|
};
|
|
564
612
|
});
|
|
@@ -14,6 +14,14 @@ export type PurchaseAnalyticsAttributesInput = {
|
|
|
14
14
|
planId?: unknown;
|
|
15
15
|
projectId?: unknown;
|
|
16
16
|
providerPlanId?: unknown;
|
|
17
|
+
checkoutStartSource?: unknown;
|
|
18
|
+
checkout_start_source?: unknown;
|
|
19
|
+
stepId?: unknown;
|
|
20
|
+
step_id?: unknown;
|
|
21
|
+
stepName?: unknown;
|
|
22
|
+
step_name?: unknown;
|
|
23
|
+
stepType?: unknown;
|
|
24
|
+
step_type?: unknown;
|
|
17
25
|
transactionId?: unknown;
|
|
18
26
|
timezone?: unknown;
|
|
19
27
|
url?: unknown;
|
|
@@ -135,7 +135,7 @@ export const buildPurchaseAnalyticsAttributes = (input = {}, options = {}) => {
|
|
|
135
135
|
const amountCents = toNumber(input.amountCents);
|
|
136
136
|
const value = (_b = toNumber(input.value)) !== null && _b !== void 0 ? _b : (amountCents === undefined ? undefined : amountCents / 100);
|
|
137
137
|
const funnelUserId = firstString(input.userId, input.user_id, input.cwClientUserId, input.cw_client_user_id);
|
|
138
|
-
return buildDefinedRecord(Object.assign(Object.assign(Object.assign({}, normalizeFlatAttributionTouch(input)), normalizeAttributionTouch(attribution.firstTouch)), { currency: toCurrency(input.currency), value, plan_id: firstString(input.planId, input.plan_id), provider_plan_id: firstString(input.providerPlanId, input.provider_plan_id), coupon_id: firstString(input.couponId, input.coupon_id), environment: firstString(input.environment), user_id: funnelUserId, cw_client_user_id: funnelUserId, project_id: firstString(input.projectId, input.project_id), funnel_id: firstString(input.funnelId, input.funnel_id), funnel_version: firstString(input.funnelVersion, input.funnelVersionId, input.funnel_version), ip_address: firstString(input.ipAddress, input.ip_address, transactionInfo.ipAddress, transactionInfo.ip_address), user_agent: firstString(input.userAgent, input.user_agent, attributionContext.userAgent), timezone: firstString(input.timezone, input.time_zone, progressAttributes.timezone, progressAttributes.time_zone, document.timezone, document.time_zone, attributionContext.timezone, attributionContext.time_zone), geo_city: firstString(input.geoCity, input.geo_city, progressAttributes.geo_city, progressAttributes.geoCity, document.geo_city, document.geoCity, asRecord(document.geo).city, attributionContext.geo_city, attributionContext.geoCity, attributionContext.cityName, attributionContext.city), geo_country: firstString(input.geoCountry, input.geo_country, progressAttributes.geo_country, progressAttributes.geoCountry, document.geo_country, document.geoCountry, asRecord(document.geo).country, attributionContext.geo_country, attributionContext.geoCountry, attributionContext.countryName, attributionContext.countryCode, attributionContext.country), transaction_id: firstString(input.transactionId, input.transaction_id), url: options.includeUrl
|
|
138
|
+
return buildDefinedRecord(Object.assign(Object.assign(Object.assign({}, normalizeFlatAttributionTouch(input)), normalizeAttributionTouch(attribution.firstTouch)), { currency: toCurrency(input.currency), value, plan_id: firstString(input.planId, input.plan_id), provider_plan_id: firstString(input.providerPlanId, input.provider_plan_id), coupon_id: firstString(input.couponId, input.coupon_id), environment: firstString(input.environment), checkout_start_source: firstString(input.checkoutStartSource, input.checkout_start_source), step_id: firstString(input.stepId, input.step_id), step_name: firstString(input.stepName, input.step_name), step_type: firstString(input.stepType, input.step_type), user_id: funnelUserId, cw_client_user_id: funnelUserId, project_id: firstString(input.projectId, input.project_id), funnel_id: firstString(input.funnelId, input.funnel_id), funnel_version: firstString(input.funnelVersion, input.funnelVersionId, input.funnel_version), ip_address: firstString(input.ipAddress, input.ip_address, transactionInfo.ipAddress, transactionInfo.ip_address), user_agent: firstString(input.userAgent, input.user_agent, attributionContext.userAgent), timezone: firstString(input.timezone, input.time_zone, progressAttributes.timezone, progressAttributes.time_zone, document.timezone, document.time_zone, attributionContext.timezone, attributionContext.time_zone), geo_city: firstString(input.geoCity, input.geo_city, progressAttributes.geo_city, progressAttributes.geoCity, document.geo_city, document.geoCity, asRecord(document.geo).city, attributionContext.geo_city, attributionContext.geoCity, attributionContext.cityName, attributionContext.city), geo_country: firstString(input.geoCountry, input.geo_country, progressAttributes.geo_country, progressAttributes.geoCountry, document.geo_country, document.geoCountry, asRecord(document.geo).country, attributionContext.geo_country, attributionContext.geoCountry, attributionContext.countryName, attributionContext.countryCode, attributionContext.country), transaction_id: firstString(input.transactionId, input.transaction_id), url: options.includeUrl
|
|
139
139
|
? sanitizePurchaseUrl(firstString(input.url, currentBrowserPurchaseUrl()))
|
|
140
140
|
: undefined }));
|
|
141
141
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@funnelsgrove/analytics",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.36",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"test:run": "vitest run --passWithNoTests"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@funnelsgrove/runtime": "0.1.
|
|
34
|
+
"@funnelsgrove/runtime": "0.1.59"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/node": "^20",
|