@funnelsgrove/analytics 0.1.27 → 0.1.29

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';
@@ -1,4 +1,4 @@
1
- type GoogleTagEventName = 'add_payment_info' | 'begin_checkout' | 'generate_lead' | 'page_view' | 'purchase' | 'tutorial_begin' | 'tutorial_complete';
1
+ type GoogleTagEventName = 'add_payment_info' | 'begin_checkout' | 'lead' | 'page_view' | 'purchase' | 'tutorial_begin' | 'tutorial_complete';
2
2
  export type GoogleTagHeadScript = {
3
3
  id: string;
4
4
  src?: string;
@@ -180,7 +180,7 @@ export const mapAnalyticsEventToGoogleTagEvent = (event) => {
180
180
  case 'email_captured':
181
181
  case 'lead':
182
182
  return {
183
- eventName: 'generate_lead',
183
+ eventName: 'lead',
184
184
  parameters: sanitizeGoogleTagParameters(buildStepParameters(event, {
185
185
  includeEventId: true,
186
186
  })),
@@ -136,6 +136,13 @@ const buildEventMetadata = (eventType, metadata) => {
136
136
  const checkoutUrl = sanitizePurchaseUrl(metadata.url) || currentBrowserPurchaseUrl();
137
137
  return checkoutUrl ? Object.assign(Object.assign({}, metadata), { url: checkoutUrl }) : metadata;
138
138
  };
139
+ const currentBrowserUserAgent = () => {
140
+ if (!canUseDom() || typeof navigator === 'undefined') {
141
+ return undefined;
142
+ }
143
+ const userAgent = typeof navigator.userAgent === 'string' ? navigator.userAgent.trim() : '';
144
+ return userAgent || undefined;
145
+ };
139
146
  export class PublicAnalyticsSdkService {
140
147
  constructor(options = {}) {
141
148
  this.options = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options);
@@ -551,7 +558,7 @@ export class PublicAnalyticsSdkService {
551
558
  event: toPostHogEventName(event.eventType),
552
559
  properties: buildDefinedRecord(Object.assign(Object.assign({ distinct_id: event.userId || this.ensureUserId(), $groups: {
553
560
  project: PROJECT_ID,
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 })),
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, engagement_threshold_ms: (_b = event.engagementThresholdMs) !== null && _b !== void 0 ? _b : 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 })),
555
562
  timestamp: event.occurredAt,
556
563
  };
557
564
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.27",
3
+ "version": "0.1.29",
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",