@funnelsgrove/analytics 0.1.26 → 0.1.28

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.
package/README.md CHANGED
@@ -1,38 +1,55 @@
1
1
  # @funnelsgrove/analytics
2
2
 
3
- Shared public analytics SDK helpers for funnels.
3
+ Shared analytics package for public funnels.
4
4
 
5
- ## Build And Publish
5
+ Read this file before editing analytics code. Deeper runtime SDK docs live in
6
+ [`docs/funnel-sdk/analytics-sdk.md`](../../docs/funnel-sdk/analytics-sdk.md).
6
7
 
7
- - Build distributable output with `npm run build --workspace @funnelsgrove/analytics`.
8
- - Publish from the repo root with `npm publish --workspace @funnelsgrove/analytics --access public`.
9
- - Local repo installs still resolve this package through npm workspaces when another workspace depends on version `0.1.1`.
10
- - The build normalizes generated relative ESM imports to explicit `.js` files for published package consumers.
8
+ ## Owns event identity and delivery
9
+
10
+ `@funnelsgrove/analytics` owns the browser identity, event-shaping, and delivery contract for public funnel analytics. Funnel code may decide when a business event happened, but this package decides how that event becomes a normalized SDK/PostHog/partner payload.
11
11
 
12
12
  ## Use This Package For
13
13
 
14
14
  - browser user-id persistence
15
+ - SDK user bootstrap for public funnel identity
15
16
  - public analytics event batching and transport
16
- - identify/track helpers for the public SDK
17
- - identify bootstrap helper for funnel identity
18
- - local-only fallback when only the seed publishable key is present
17
+ - identify, track, step lifecycle, and checkout event helpers
18
+ - Stripe purchase metadata shaping before payment metadata reaches the API
19
+ - Google Tag and Meta Pixel event adapters
20
+ - local-only no-op behavior when a real publishable key is not configured
19
21
 
20
- ## Responsibilities
22
+ ## Do Not Use This Package For
21
23
 
22
- - keep client identity stable across funnels
23
- - own the browser storage contract for the external user id
24
- - send analytics events through the shared API contract
25
- - avoid noisy network calls when a real publishable key is not configured
24
+ - step routing
25
+ - manifest logic
26
+ - checkout orchestration
27
+ - billing plan resolution
28
+ - Stripe checkout UI
29
+ - funnel-specific step components or copy
26
30
 
27
- ## What Belongs Here
31
+ ## Public Surfaces
28
32
 
29
- - cookie/localStorage identity helpers
30
- - public SDK wrappers
31
- - event normalization and transport
33
+ - `publicAnalyticsSdk`: the canonical browser SDK for identify, track, lifecycle, checkout, and flush calls.
34
+ - `bootstrapPublicAnalyticsUser`: public SDK identity bootstrap helper.
35
+ - `sdk-user-id.storage`: shared browser user-id storage contract.
36
+ - `buildStripePurchaseMetadata` and purchase attribute helpers: safe metadata shaping for payment handoff.
37
+ - `google-tag.service` and `meta-pixel.service`: partner event adapters.
38
+ - `experiment-result-metrics.service`: shared experiment result metric helpers.
32
39
 
33
- ## What Does Not Belong Here
40
+ ## Engineering Rules
34
41
 
35
- - step routing
36
- - manifest logic
37
- - Stripe checkout UI
38
- - funnel-specific step components
42
+ - Keep transport details here; do not duplicate event batching in funnels.
43
+ - Keep provider adapters dumb: they map canonical events, not funnel business flow.
44
+ - Do not import payment UI or runtime routing from this package.
45
+ - Sensitive values must be filtered before metadata reaches Stripe or partner tools.
46
+
47
+ ## Build, Test, Publish
48
+
49
+ ```bash
50
+ npm run test:run --workspace @funnelsgrove/analytics
51
+ npm run build --workspace @funnelsgrove/analytics
52
+ npm publish --workspace @funnelsgrove/analytics --access public
53
+ ```
54
+
55
+ The build normalizes generated relative ESM imports to explicit `.js` files for published package consumers.
package/dist/index.js CHANGED
@@ -1,8 +1,11 @@
1
+ // Runtime API and identity.
1
2
  export * from './services/runtime-api.config.js';
2
3
  export * from './services/sdk-user-id.storage.js';
3
4
  export * from './services/bootstrap-public-analytics-user.js';
5
+ // Partner adapters.
4
6
  export * from './services/google-tag.service.js';
5
7
  export * from './services/meta-pixel.service.js';
8
+ // Purchase and event transport.
6
9
  export * from './services/purchase-attributes.service.js';
7
10
  export * from './services/public-analytics-sdk.service.js';
8
11
  export * from './services/experiment-result-metrics.service.js';
@@ -7,7 +7,7 @@ const DEFAULT_OPTIONS = {
7
7
  batchSize: 20,
8
8
  flushIntervalMs: 1500,
9
9
  };
10
- const POSTHOG_EVENT_ENDPOINT = '/i/v0/e/';
10
+ const POSTHOG_BATCH_ENDPOINT = '/batch/';
11
11
  const DEFAULT_FUNNEL_DOMAIN_SIGNAL = 'funnelsgrove';
12
12
  const normalizeEnvironment = (value) => {
13
13
  if (typeof value !== 'string') {
@@ -96,13 +96,6 @@ const toDurationMs = (startedAt, endedAt) => {
96
96
  }
97
97
  return endedMs - startedMs;
98
98
  };
99
- const toPostHogOffset = (occurredAt) => {
100
- const occurredMs = new Date(occurredAt).getTime();
101
- if (!Number.isFinite(occurredMs)) {
102
- return 0;
103
- }
104
- return Math.max(0, Date.now() - occurredMs);
105
- };
106
99
  const buildEventId = () => {
107
100
  var _a;
108
101
  if (canUseDom() && typeof ((_a = window.crypto) === null || _a === void 0 ? void 0 : _a.randomUUID) === 'function') {
@@ -556,14 +549,17 @@ export class PublicAnalyticsSdkService {
556
549
  const eventEnvironment = event.environment || analyticsEnvironment;
557
550
  return {
558
551
  event: toPostHogEventName(event.eventType),
559
- properties: buildDefinedRecord(Object.assign(Object.assign({ token: POSTHOG_PROJECT_API_KEY, distinct_id: event.userId || this.ensureUserId(), $groups: {
552
+ properties: buildDefinedRecord(Object.assign(Object.assign({ distinct_id: event.userId || this.ensureUserId(), $groups: {
560
553
  project: PROJECT_ID,
561
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 })),
562
- offset: toPostHogOffset(event.occurredAt),
555
+ timestamp: event.occurredAt,
563
556
  };
564
557
  });
565
- const payload = JSON.stringify(batch);
566
- const endpoint = `${POSTHOG_API_HOST}${POSTHOG_EVENT_ENDPOINT}`;
558
+ const payload = JSON.stringify({
559
+ api_key: POSTHOG_PROJECT_API_KEY,
560
+ batch,
561
+ });
562
+ const endpoint = `${POSTHOG_API_HOST}${POSTHOG_BATCH_ENDPOINT}`;
567
563
  if (canUseDom() && typeof navigator.sendBeacon === 'function') {
568
564
  const accepted = navigator.sendBeacon(endpoint, new Blob([payload], { type: 'application/json' }));
569
565
  if (accepted) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.26",
3
+ "version": "0.1.28",
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.44"
34
+ "@funnelsgrove/runtime": "0.1.45"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "^20",