@funnelsgrove/analytics 0.1.19 → 0.1.21

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,5 +1,7 @@
1
1
  export type PublicSdkTrackEventInput = {
2
2
  eventType: string;
3
+ userId?: string;
4
+ environment?: PublicSdkAnalyticsEnvironment;
3
5
  eventId?: string;
4
6
  occurredAt?: string;
5
7
  stepId?: string;
@@ -14,6 +16,8 @@ export type PublicSdkTrackEventInput = {
14
16
  featureFlags?: Record<string, string | null | undefined>;
15
17
  };
16
18
  export type PublicSdkStepStartInput = {
19
+ userId?: string;
20
+ environment?: PublicSdkAnalyticsEnvironment;
17
21
  stepId: string;
18
22
  stepName: string;
19
23
  stepType?: string;
@@ -21,6 +25,8 @@ export type PublicSdkStepStartInput = {
21
25
  featureFlags?: Record<string, string | null | undefined>;
22
26
  };
23
27
  export type PublicSdkStepEndInput = {
28
+ userId?: string;
29
+ environment?: PublicSdkAnalyticsEnvironment;
24
30
  stepId: string;
25
31
  stepName: string;
26
32
  stepType?: string;
@@ -30,6 +36,8 @@ export type PublicSdkStepEndInput = {
30
36
  featureFlags?: Record<string, string | null | undefined>;
31
37
  };
32
38
  export type PublicSdkStepEngagedInput = {
39
+ userId?: string;
40
+ environment?: PublicSdkAnalyticsEnvironment;
33
41
  stepId: string;
34
42
  stepName: string;
35
43
  stepType?: string;
@@ -40,6 +48,8 @@ export type PublicSdkStepEngagedInput = {
40
48
  featureFlags?: Record<string, string | null | undefined>;
41
49
  };
42
50
  export type PublicSdkConversionCallbackInput = {
51
+ userId?: string;
52
+ environment?: PublicSdkAnalyticsEnvironment;
43
53
  eventId?: string;
44
54
  occurredAt?: string;
45
55
  stepId?: string;
@@ -58,6 +68,7 @@ export type PublicAnalyticsSdkOptions = {
58
68
  batchSize: number;
59
69
  flushIntervalMs: number;
60
70
  };
71
+ export type PublicSdkAnalyticsEnvironment = 'test' | 'live';
61
72
  export declare class PublicAnalyticsSdkService {
62
73
  private readonly options;
63
74
  private readonly eventQueue;
@@ -1,4 +1,4 @@
1
- import { buildMainApiUrl, buildSdkHeaders, FUNNEL_ID, FUNNEL_VERSION_ID, getFunnelSdkPublishableKey, hasPostHogRuntimeConfig, POSTHOG_API_HOST, POSTHOG_PROJECT_API_KEY, PROJECT_ID, STRIPE_ACTIVE_MODE, } from './runtime-api.config.js';
1
+ 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
2
  import { canUseDom, getOrCreateUserId, persistUserId, readStoredUserId, } from './sdk-user-id.storage.js';
3
3
  import { mapAnalyticsEventToMetaPixelEvent, metaPixelService, } from './meta-pixel.service.js';
4
4
  import { googleTagService, mapAnalyticsEventToGoogleTagEvent, } from './google-tag.service.js';
@@ -129,7 +129,6 @@ const normalizeFeatureFlags = (featureFlags) => {
129
129
  const buildFeatureFlagProperties = (featureFlags) => {
130
130
  return Object.fromEntries(Object.entries(featureFlags).map(([key, value]) => [`$feature/${key}`, value]));
131
131
  };
132
- const isRuntimeTestMode = () => STRIPE_ACTIVE_MODE === 'test';
133
132
  const buildEventMetadata = (eventType, metadata) => {
134
133
  if (eventType !== 'checkout_completed') {
135
134
  return metadata;
@@ -251,6 +250,8 @@ export class PublicAnalyticsSdkService {
251
250
  }
252
251
  const envelope = {
253
252
  eventType,
253
+ userId: asString(event.userId),
254
+ environment: normalizeEnvironment(event.environment),
254
255
  eventId,
255
256
  occurredAt: toIsoOrNow(event.occurredAt),
256
257
  stepId: asString(event.stepId),
@@ -277,6 +278,8 @@ export class PublicAnalyticsSdkService {
277
278
  trackStepStarted(input) {
278
279
  return this.track({
279
280
  eventType: 'step_start',
281
+ userId: input.userId,
282
+ environment: input.environment,
280
283
  stepId: input.stepId,
281
284
  stepName: input.stepName,
282
285
  stepType: input.stepType,
@@ -291,6 +294,8 @@ export class PublicAnalyticsSdkService {
291
294
  const endedAt = asString(input.endedAt);
292
295
  return this.track({
293
296
  eventType: 'step_end',
297
+ userId: input.userId,
298
+ environment: input.environment,
294
299
  stepId: input.stepId,
295
300
  stepName: input.stepName,
296
301
  stepType: input.stepType,
@@ -307,6 +312,8 @@ export class PublicAnalyticsSdkService {
307
312
  trackStepEngaged(input) {
308
313
  return this.track({
309
314
  eventType: 'step_engaged',
315
+ userId: input.userId,
316
+ environment: input.environment,
310
317
  stepId: input.stepId,
311
318
  stepName: input.stepName,
312
319
  stepType: input.stepType,
@@ -323,6 +330,8 @@ export class PublicAnalyticsSdkService {
323
330
  trackFunnelStarted(input) {
324
331
  return this.track({
325
332
  eventType: 'funnel_start',
333
+ userId: input.userId,
334
+ environment: input.environment,
326
335
  eventId: input.eventId,
327
336
  occurredAt: input.occurredAt,
328
337
  stepId: input.stepId,
@@ -335,6 +344,8 @@ export class PublicAnalyticsSdkService {
335
344
  trackFirstStepClicked(input) {
336
345
  return this.track({
337
346
  eventType: 'first_step_clicked',
347
+ userId: input.userId,
348
+ environment: input.environment,
338
349
  eventId: input.eventId,
339
350
  occurredAt: input.occurredAt,
340
351
  stepId: input.stepId,
@@ -347,6 +358,8 @@ export class PublicAnalyticsSdkService {
347
358
  trackFirstStepViewed(input) {
348
359
  return this.track({
349
360
  eventType: 'first_step_viewed',
361
+ userId: input.userId,
362
+ environment: input.environment,
350
363
  eventId: input.eventId,
351
364
  occurredAt: input.occurredAt,
352
365
  stepId: input.stepId,
@@ -359,6 +372,8 @@ export class PublicAnalyticsSdkService {
359
372
  trackLead(input) {
360
373
  return this.track({
361
374
  eventType: 'email_captured',
375
+ userId: input.userId,
376
+ environment: input.environment,
362
377
  eventId: input.eventId,
363
378
  occurredAt: input.occurredAt,
364
379
  stepId: input.stepId,
@@ -371,6 +386,8 @@ export class PublicAnalyticsSdkService {
371
386
  trackFunnelCompleted(input) {
372
387
  return this.track({
373
388
  eventType: 'funnel_completed',
389
+ userId: input.userId,
390
+ environment: input.environment,
374
391
  eventId: input.eventId,
375
392
  occurredAt: input.occurredAt,
376
393
  stepId: input.stepId,
@@ -383,6 +400,8 @@ export class PublicAnalyticsSdkService {
383
400
  trackCheckoutStarted(input) {
384
401
  return this.track({
385
402
  eventType: 'checkout_started',
403
+ userId: input.userId,
404
+ environment: input.environment,
386
405
  eventId: input.eventId,
387
406
  occurredAt: input.occurredAt,
388
407
  stepId: input.stepId,
@@ -395,6 +414,8 @@ export class PublicAnalyticsSdkService {
395
414
  trackPaymentInfoSubmitted(input) {
396
415
  return this.track({
397
416
  eventType: 'add_payment_info',
417
+ userId: input.userId,
418
+ environment: input.environment,
398
419
  eventId: input.eventId,
399
420
  occurredAt: input.occurredAt,
400
421
  stepId: input.stepId,
@@ -407,6 +428,8 @@ export class PublicAnalyticsSdkService {
407
428
  trackCheckoutCompleted(input) {
408
429
  return this.track({
409
430
  eventType: 'checkout_completed',
431
+ userId: input.userId,
432
+ environment: input.environment,
410
433
  eventId: input.eventId,
411
434
  occurredAt: input.occurredAt,
412
435
  stepId: input.stepId,
@@ -419,6 +442,8 @@ export class PublicAnalyticsSdkService {
419
442
  trackPaymentCheckoutSucceeded(input) {
420
443
  return this.track({
421
444
  eventType: 'payment_checkout_succeeded',
445
+ userId: input.userId,
446
+ environment: input.environment,
422
447
  eventId: input.eventId,
423
448
  occurredAt: input.occurredAt,
424
449
  stepId: input.stepId,
@@ -431,6 +456,8 @@ export class PublicAnalyticsSdkService {
431
456
  trackPaymentCheckoutReturned(input) {
432
457
  return this.track({
433
458
  eventType: 'payment_checkout_returned',
459
+ userId: input.userId,
460
+ environment: input.environment,
434
461
  eventId: input.eventId,
435
462
  occurredAt: input.occurredAt,
436
463
  stepId: input.stepId,
@@ -516,15 +543,17 @@ export class PublicAnalyticsSdkService {
516
543
  if (!getFunnelSdkPublishableKey() || !hasPostHogRuntimeConfig()) {
517
544
  return true;
518
545
  }
546
+ const analyticsEnvironment = resolveAnalyticsEnvironment();
519
547
  const batch = events.map((event) => {
520
548
  var _a, _b;
521
- return ({
549
+ const eventEnvironment = event.environment || analyticsEnvironment;
550
+ return {
522
551
  event: toPostHogEventName(event.eventType),
523
- properties: buildDefinedRecord(Object.assign(Object.assign({ distinct_id: this.ensureUserId(), $groups: {
552
+ properties: buildDefinedRecord(Object.assign(Object.assign({ distinct_id: event.userId || this.ensureUserId(), $groups: {
524
553
  project: PROJECT_ID,
525
- }, $insert_id: event.eventId, project_id: PROJECT_ID, funnel_id: FUNNEL_ID || undefined, funnel_version_id: FUNNEL_VERSION_ID || undefined, environment: resolveAnalyticsEnvironment(), test: isRuntimeTestMode() || 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, engagement_threshold_ms: (_b = event.engagementThresholdMs) !== null && _b !== void 0 ? _b : undefined, 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 })),
554
+ }, $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, engagement_threshold_ms: (_b = event.engagementThresholdMs) !== null && _b !== void 0 ? _b : undefined, 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 })),
526
555
  timestamp: event.occurredAt,
527
- });
556
+ };
528
557
  });
529
558
  const payload = JSON.stringify({
530
559
  api_key: POSTHOG_PROJECT_API_KEY,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.19",
3
+ "version": "0.1.21",
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.16"
34
+ "@funnelsgrove/runtime": "0.1.41"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "^20",