@funnelsgrove/analytics 0.12.0 → 0.14.0

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.
@@ -0,0 +1,15 @@
1
+ import { type BrowserDeliveryOptions } from './browser-event-delivery.js';
2
+ type AnalyticsEvent = {
3
+ event: string;
4
+ properties: Record<string, unknown>;
5
+ timestamp?: string;
6
+ };
7
+ /** Maps the same normalized event used by PostHog, preserving its identity. */
8
+ export declare class AmplitudeDelivery {
9
+ private readonly delivery;
10
+ static create(options: Partial<BrowserDeliveryOptions>): AmplitudeDelivery | null;
11
+ private constructor();
12
+ enqueue(event: AnalyticsEvent): boolean;
13
+ flush(): Promise<number>;
14
+ }
15
+ export {};
@@ -0,0 +1,36 @@
1
+ import { toAmplitudeEvent } from './analytics-export.js';
2
+ import { runtimeEnvConfig } from '@funnelsgrove/runtime/config/env.config';
3
+ import { BrowserEventDelivery } from './browser-event-delivery.js';
4
+ const ENDPOINTS = { us: 'https://api2.amplitude.com/2/httpapi', eu: 'https://api.eu.amplitude.com/2/httpapi' };
5
+ /** Maps the same normalized event used by PostHog, preserving its identity. */
6
+ export class AmplitudeDelivery {
7
+ static create(options) {
8
+ var _a;
9
+ if (runtimeEnvConfig.amplitudeEnabled !== 'true')
10
+ return null;
11
+ const configuredKey = ((_a = runtimeEnvConfig.amplitudeApiKey) === null || _a === void 0 ? void 0 : _a.trim()) || '';
12
+ const key = /^[\x21-\x7e]+$/.test(configuredKey) ? configuredKey : '';
13
+ if (!key)
14
+ return null;
15
+ return new AmplitudeDelivery(options, runtimeEnvConfig.amplitudeRegion === 'eu' ? ENDPOINTS.eu : ENDPOINTS.us, key);
16
+ }
17
+ constructor(options, endpoint, key) {
18
+ this.delivery = new BrowserEventDelivery(options, {
19
+ enabled: true, endpoint, batchPrefix: `{"api_key":${JSON.stringify(key)},"events":[`,
20
+ });
21
+ }
22
+ enqueue(event) {
23
+ if (event.properties.environment !== 'live' && runtimeEnvConfig.amplitudeIncludeTestEvents !== 'true')
24
+ return true;
25
+ try {
26
+ return this.delivery.enqueue(toAmplitudeEvent({
27
+ distinctId: String(event.properties.distinct_id), messageId: String(event.properties.$insert_id),
28
+ event: event.event, properties: event.properties, timestamp: event.timestamp,
29
+ }, { remoteIp: true }));
30
+ }
31
+ catch (_a) {
32
+ return false;
33
+ }
34
+ }
35
+ flush() { return this.delivery.flush(); }
36
+ }
@@ -18,3 +18,22 @@ export declare const toRudderstackTrackEvent: (input: {
18
18
  timestamp?: string;
19
19
  properties?: Record<string, unknown>;
20
20
  }) => RudderstackTrackEvent;
21
+ export type AmplitudeEvent = {
22
+ event_type: string;
23
+ device_id: string;
24
+ insert_id: string;
25
+ time?: number;
26
+ platform: 'Web';
27
+ ip?: '$remote';
28
+ event_properties: Record<string, unknown>;
29
+ };
30
+ /** The FunnelsGrove user ID is the Amplitude Device ID so apps can adopt it from a link. */
31
+ export declare const toAmplitudeEvent: (input: {
32
+ distinctId: string;
33
+ event: string;
34
+ messageId: string;
35
+ timestamp?: string;
36
+ properties?: Record<string, unknown>;
37
+ }, options?: {
38
+ remoteIp?: boolean;
39
+ }) => AmplitudeEvent;
@@ -52,5 +52,11 @@ export const toRudderstackTrackEvent = (input) => {
52
52
  delete properties.distinct_id;
53
53
  delete properties.$insert_id;
54
54
  delete properties.$groups;
55
- return sanitizeAnalyticsExport(Object.assign(Object.assign({ type: 'track', event: input.event, userId: input.distinctId, messageId: input.messageId }, (input.timestamp ? { timestamp: input.timestamp } : {})), { properties }));
55
+ return sanitizeAnalyticsExport(Object.assign(Object.assign({ type: 'track', event: input.event, userId: input.distinctId, messageId: input.messageId }, (input.timestamp ? { timestamp: input.timestamp } : {})), { properties: Object.assign(Object.assign({}, properties), { funnel_platform: 'funnelsgrove' }) }));
56
+ };
57
+ /** The FunnelsGrove user ID is the Amplitude Device ID so apps can adopt it from a link. */
58
+ export const toAmplitudeEvent = (input, options = {}) => {
59
+ const { userId, messageId, event, timestamp, properties } = toRudderstackTrackEvent(input);
60
+ const time = timestamp ? Date.parse(timestamp) : NaN;
61
+ return Object.assign(Object.assign(Object.assign(Object.assign({ event_type: event, device_id: userId, insert_id: messageId }, (Number.isFinite(time) ? { time } : {})), { platform: 'Web' }), (options.remoteIp ? { ip: '$remote' } : {})), { event_properties: properties });
56
62
  };
@@ -112,6 +112,7 @@ export declare const createBootstrapIdempotencyKey: (secureCrypto?: SecureBootst
112
112
  export declare class PublicAnalyticsSdkService {
113
113
  private readonly delivery;
114
114
  private readonly rudderstack;
115
+ private readonly amplitude;
115
116
  private userId;
116
117
  constructor(options?: Partial<PublicAnalyticsSdkOptions>);
117
118
  getCurrentUserId(): string;
@@ -139,6 +140,8 @@ export declare class PublicAnalyticsSdkService {
139
140
  /** @deprecated Legacy contract-v1 compatibility only; v2 funnels use submitEmailCapture. */
140
141
  trackLead(input: PublicSdkConversionCallbackInput): string | null;
141
142
  trackFunnelCompleted(input: PublicSdkStepConversionCallbackInput): string | null;
143
+ /** A checkout form or wallet button became visible; each show is a new event. */
144
+ trackCheckoutShown(input: PublicSdkStepConversionCallbackInput): string | null;
142
145
  trackCheckoutStarted(input: PublicSdkStepConversionCallbackInput): string | null;
143
146
  trackPaymentInfoSubmitted(input: PublicSdkStepConversionCallbackInput): string | null;
144
147
  trackCheckoutCompleted(input: PublicSdkStepConversionCallbackInput): string | null;
@@ -1,6 +1,7 @@
1
1
  import { RudderstackDelivery } from './rudderstack-delivery.js';
2
+ import { AmplitudeDelivery } from './amplitude-delivery.js';
2
3
  import { PostHogDelivery } from './posthog-delivery.js';
3
- import { FUNNEL_ANALYTICS_EVENT_CONTRACTS, funnelSdkService, getPaywallUser, readCrossFunnelEventProperties, FUNNEL_STEP_CONTRACTS, } from '@funnelsgrove/runtime';
4
+ import { FUNNEL_ANALYTICS_EVENT_CONTRACTS, funnelSdkService, getPaywallUser, readAdTrackerClickIdProperties, readCrossFunnelEventProperties, FUNNEL_STEP_CONTRACTS, } from '@funnelsgrove/runtime';
4
5
  import { APP_DEALS_API_BASE_URL, FUNNEL_ID, FUNNEL_VERSION_ID, getFunnelSdkPublishableKey, PROJECT_ID, } from './runtime-api.config.js';
5
6
  import { canUseDom, getOrCreateUserId, persistUserId, readStoredUserId, } from './sdk-user-id.storage.js';
6
7
  import { mapAnalyticsEventToMetaPixelEvent, metaPixelService, } from './meta-pixel.service.js';
@@ -184,6 +185,7 @@ export class PublicAnalyticsSdkService {
184
185
  this.userId = null;
185
186
  this.delivery = new PostHogDelivery(options);
186
187
  this.rudderstack = RudderstackDelivery.create(options);
188
+ this.amplitude = AmplitudeDelivery.create(options);
187
189
  googleTagService.initialize();
188
190
  }
189
191
  getCurrentUserId() {
@@ -288,7 +290,7 @@ export class PublicAnalyticsSdkService {
288
290
  }
289
291
  }
290
292
  trackTrusted(event) {
291
- var _a, _b, _c, _d, _e, _f, _g;
293
+ var _a, _b, _c, _d, _e, _f, _g, _h;
292
294
  const eventType = asString(event.eventType);
293
295
  if (!eventType) {
294
296
  return null;
@@ -323,6 +325,7 @@ export class PublicAnalyticsSdkService {
323
325
  };
324
326
  const normalized = this.toPostHogEvent(envelope);
325
327
  (_g = this.rudderstack) === null || _g === void 0 ? void 0 : _g.enqueue(normalized);
328
+ (_h = this.amplitude) === null || _h === void 0 ? void 0 : _h.enqueue(normalized);
326
329
  return this.delivery.enqueue(normalized) ? envelope.eventId : null;
327
330
  }
328
331
  trackContractCallback(eventName, input) {
@@ -485,6 +488,22 @@ export class PublicAnalyticsSdkService {
485
488
  trackFunnelCompleted(input) {
486
489
  return this.trackContractCallback('funnel_completed', input);
487
490
  }
491
+ /** A checkout form or wallet button became visible; each show is a new event. */
492
+ trackCheckoutShown(input) {
493
+ return this.trackTrusted({
494
+ eventType: 'checkout_shown',
495
+ userId: input.userId,
496
+ environment: input.environment,
497
+ eventId: input.eventId,
498
+ occurredAt: input.occurredAt,
499
+ stepId: input.stepId,
500
+ stepName: input.stepName,
501
+ stepType: input.stepType,
502
+ stepContractVersion: input.stepContractVersion,
503
+ metadata: input.metadata,
504
+ featureFlags: input.featureFlags,
505
+ });
506
+ }
488
507
  trackCheckoutStarted(input) {
489
508
  return this.trackContractCallback('checkout_started', input);
490
509
  }
@@ -525,9 +544,10 @@ export class PublicAnalyticsSdkService {
525
544
  });
526
545
  }
527
546
  async flush() {
528
- if (!this.rudderstack)
547
+ var _a, _b;
548
+ if (!this.rudderstack && !this.amplitude)
529
549
  return this.delivery.flush();
530
- const [accepted] = await Promise.all([this.delivery.flush(), this.rudderstack.flush()]);
550
+ const [accepted] = await Promise.all([this.delivery.flush(), (_a = this.rudderstack) === null || _a === void 0 ? void 0 : _a.flush(), (_b = this.amplitude) === null || _b === void 0 ? void 0 : _b.flush()]);
531
551
  return accepted;
532
552
  }
533
553
  ensureUserId() {
@@ -566,9 +586,9 @@ export class PublicAnalyticsSdkService {
566
586
  const eventEnvironment = event.environment || analyticsEnvironment;
567
587
  return {
568
588
  event: toPostHogEventName(event.eventType),
569
- properties: buildDefinedRecord(Object.assign(Object.assign(Object.assign({ distinct_id: ((_a = getPaywallUser()) === null || _a === void 0 ? void 0 : _a.user_id) || event.userId || this.ensureUserId(), $groups: {
589
+ properties: buildDefinedRecord(Object.assign(Object.assign(Object.assign(Object.assign({ distinct_id: ((_a = getPaywallUser()) === null || _a === void 0 ? void 0 : _a.user_id) || event.userId || this.ensureUserId(), $groups: {
570
590
  project: PROJECT_ID,
571
- }, $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: (_b = event.durationMs) !== null && _b !== void 0 ? _b : undefined, exit_reason: event.exitReason || undefined, step_contract_version: (_c = event.stepContractVersion) !== null && _c !== void 0 ? _c : undefined, engagement_threshold_ms: (_d = event.engagementThresholdMs) !== null && _d !== void 0 ? _d : undefined, user_agent: currentBrowserUserAgent(), event_source: 'client' }, readCrossFunnelEventProperties(event.metadata.funnel_first_step_id)), 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 })),
591
+ }, $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: (_b = event.durationMs) !== null && _b !== void 0 ? _b : undefined, exit_reason: event.exitReason || undefined, step_contract_version: (_c = event.stepContractVersion) !== null && _c !== void 0 ? _c : undefined, engagement_threshold_ms: (_d = event.engagementThresholdMs) !== null && _d !== void 0 ? _d : undefined, user_agent: currentBrowserUserAgent(), event_source: 'client' }, readAdTrackerClickIdProperties()), readCrossFunnelEventProperties(event.metadata.funnel_first_step_id)), 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 })),
572
592
  timestamp: event.occurredAt,
573
593
  };
574
594
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/The-Solid-Grove/funnelsgrove.git",
@@ -45,9 +45,9 @@
45
45
  "@types/node": "^20",
46
46
  "typescript": "^5",
47
47
  "vitest": "^3.2.4",
48
- "@funnelsgrove/runtime": "0.30.0"
48
+ "@funnelsgrove/runtime": "0.32.0"
49
49
  },
50
50
  "peerDependencies": {
51
- "@funnelsgrove/runtime": "^0.30.0"
51
+ "@funnelsgrove/runtime": "^0.32.0"
52
52
  }
53
53
  }