@funnelsgrove/analytics 0.13.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;
@@ -54,3 +54,9 @@ export const toRudderstackTrackEvent = (input) => {
54
54
  delete properties.$groups;
55
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
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 });
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;
@@ -1,4 +1,5 @@
1
1
  import { RudderstackDelivery } from './rudderstack-delivery.js';
2
+ import { AmplitudeDelivery } from './amplitude-delivery.js';
2
3
  import { PostHogDelivery } from './posthog-delivery.js';
3
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';
@@ -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) {
@@ -541,9 +544,10 @@ export class PublicAnalyticsSdkService {
541
544
  });
542
545
  }
543
546
  async flush() {
544
- if (!this.rudderstack)
547
+ var _a, _b;
548
+ if (!this.rudderstack && !this.amplitude)
545
549
  return this.delivery.flush();
546
- 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()]);
547
551
  return accepted;
548
552
  }
549
553
  ensureUserId() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.13.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.31.0"
48
+ "@funnelsgrove/runtime": "0.32.0"
49
49
  },
50
50
  "peerDependencies": {
51
- "@funnelsgrove/runtime": "^0.31.0"
51
+ "@funnelsgrove/runtime": "^0.32.0"
52
52
  }
53
53
  }