@funnelsgrove/analytics 0.1.14 → 0.1.16

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,4 +1,4 @@
1
- type GoogleTagEventName = 'purchase';
1
+ type GoogleTagEventName = 'add_payment_info' | 'begin_checkout' | 'generate_lead' | 'page_view' | 'purchase' | 'tutorial_begin' | 'tutorial_complete';
2
2
  export type GoogleTagHeadScript = {
3
3
  id: string;
4
4
  src?: string;
@@ -1,5 +1,5 @@
1
1
  import { GOOGLE_ANALYTICS_ID, GOOGLE_TAG_ENABLED, GOOGLE_TAG_EVENT_TARGET, GOOGLE_TAG_MANAGER_ID, } from './runtime-api.config.js';
2
- import { buildBrowserPurchaseAnalyticsAttributes } from './purchase-attributes.service.js';
2
+ import { buildBrowserPurchaseAnalyticsAttributes, buildPurchaseAnalyticsAttributes, currentBrowserPurchaseUrl, } from './purchase-attributes.service.js';
3
3
  const normalizeEventTarget = (value) => {
4
4
  return value === 'dataLayer' || value === 'gtag' || value === 'both' ? value : 'both';
5
5
  };
@@ -52,6 +52,25 @@ const sanitizeUrlForGoogleTag = (value) => {
52
52
  return trimmed;
53
53
  }
54
54
  };
55
+ const asString = (value) => {
56
+ if (typeof value !== 'string') {
57
+ return undefined;
58
+ }
59
+ const trimmed = value.trim();
60
+ return trimmed || undefined;
61
+ };
62
+ const firstString = (...values) => {
63
+ for (const value of values) {
64
+ const normalized = asString(value);
65
+ if (normalized) {
66
+ return normalized;
67
+ }
68
+ }
69
+ return undefined;
70
+ };
71
+ const buildDefinedRecord = (value) => {
72
+ return Object.fromEntries(Object.entries(value).filter(([, candidate]) => candidate !== undefined && candidate !== null && candidate !== ''));
73
+ };
55
74
  const sanitizeGoogleTagParameters = (parameters) => {
56
75
  return Object.fromEntries(Object.entries(parameters).flatMap(([key, value]) => {
57
76
  if (SENSITIVE_GOOGLE_TAG_PARAMETER_KEYS.has(key)) {
@@ -61,9 +80,35 @@ const sanitizeGoogleTagParameters = (parameters) => {
61
80
  const sanitizedUrl = sanitizeUrlForGoogleTag(value);
62
81
  return sanitizedUrl ? [[key, sanitizedUrl]] : [];
63
82
  }
83
+ if (key === 'page_location') {
84
+ const sanitizedUrl = sanitizeUrlForGoogleTag(value);
85
+ return sanitizedUrl ? [[key, sanitizedUrl]] : [];
86
+ }
64
87
  return [[key, value]];
65
88
  }));
66
89
  };
90
+ const buildStepParameters = (event, options = {}) => {
91
+ var _a;
92
+ const metadata = (_a = event.metadata) !== null && _a !== void 0 ? _a : {};
93
+ return buildDefinedRecord({
94
+ event_id: options.includeEventId ? asString(event.eventId) : undefined,
95
+ step_id: firstString(metadata.stepId, metadata.step_id),
96
+ step_name: firstString(metadata.stepName, metadata.step_name),
97
+ });
98
+ };
99
+ const buildPageViewParameters = (event) => {
100
+ var _a;
101
+ const metadata = (_a = event.metadata) !== null && _a !== void 0 ? _a : {};
102
+ return sanitizeGoogleTagParameters(buildDefinedRecord(Object.assign({ page_location: firstString(metadata.page_location, metadata.url, currentBrowserPurchaseUrl()), page_title: firstString(metadata.page_title, metadata.stepName, metadata.step_name) }, buildStepParameters(event))));
103
+ };
104
+ const buildCommerceParameters = (event, options = {}) => {
105
+ var _a;
106
+ const metadata = (_a = event.metadata) !== null && _a !== void 0 ? _a : {};
107
+ const purchaseAttributes = options.includeTransactionId
108
+ ? buildBrowserPurchaseAnalyticsAttributes(Object.assign(Object.assign({}, metadata), { transactionId: event.eventId }))
109
+ : buildPurchaseAnalyticsAttributes(metadata);
110
+ return sanitizeGoogleTagParameters(buildDefinedRecord(Object.assign(Object.assign({}, purchaseAttributes), buildStepParameters(event))));
111
+ };
67
112
  export const buildGoogleTagHeadScripts = () => {
68
113
  if (!GOOGLE_TAG_ENABLED || (!GOOGLE_ANALYTICS_ID && !GOOGLE_TAG_MANAGER_ID)) {
69
114
  return [];
@@ -77,7 +122,7 @@ export const buildGoogleTagHeadScripts = () => {
77
122
  ...(GOOGLE_ANALYTICS_ID
78
123
  ? [
79
124
  'window.gtag("js", new Date());',
80
- `window.gtag("config", ${JSON.stringify(GOOGLE_ANALYTICS_ID)}, { page_location: window.__funnelsgroveGoogleTagPageLocation });`,
125
+ `window.gtag("config", ${JSON.stringify(GOOGLE_ANALYTICS_ID)}, { page_location: window.__funnelsgroveGoogleTagPageLocation, send_page_view: false });`,
81
126
  ]
82
127
  : []),
83
128
  ...(GOOGLE_TAG_MANAGER_ID
@@ -111,12 +156,52 @@ export const buildGoogleTagHeadScripts = () => {
111
156
  ];
112
157
  };
113
158
  export const mapAnalyticsEventToGoogleTagEvent = (event) => {
114
- var _a;
115
159
  switch (event.eventType) {
160
+ case 'step_start':
161
+ return {
162
+ eventName: 'page_view',
163
+ parameters: buildPageViewParameters(event),
164
+ };
165
+ case 'funnel_start':
166
+ case 'first_step_clicked':
167
+ return {
168
+ eventName: 'tutorial_begin',
169
+ parameters: sanitizeGoogleTagParameters(buildStepParameters(event, {
170
+ includeEventId: true,
171
+ })),
172
+ };
173
+ case 'funnel_completed':
174
+ return {
175
+ eventName: 'tutorial_complete',
176
+ parameters: sanitizeGoogleTagParameters(buildStepParameters(event, {
177
+ includeEventId: true,
178
+ })),
179
+ };
180
+ case 'email_captured':
181
+ case 'lead':
182
+ return {
183
+ eventName: 'generate_lead',
184
+ parameters: sanitizeGoogleTagParameters(buildStepParameters(event, {
185
+ includeEventId: true,
186
+ })),
187
+ };
188
+ case 'checkout_started':
189
+ return {
190
+ eventName: 'begin_checkout',
191
+ parameters: buildCommerceParameters(event),
192
+ };
193
+ case 'add_payment_info':
194
+ case 'payment_checkout_succeeded':
195
+ return {
196
+ eventName: 'add_payment_info',
197
+ parameters: buildCommerceParameters(event),
198
+ };
116
199
  case 'checkout_completed':
117
200
  return {
118
201
  eventName: 'purchase',
119
- parameters: sanitizeGoogleTagParameters(buildBrowserPurchaseAnalyticsAttributes(Object.assign(Object.assign({}, ((_a = event.metadata) !== null && _a !== void 0 ? _a : {})), { transactionId: event.eventId }))),
202
+ parameters: buildCommerceParameters(event, {
203
+ includeTransactionId: true,
204
+ }),
120
205
  };
121
206
  default:
122
207
  return null;
@@ -168,6 +253,7 @@ export class GoogleTagService {
168
253
  (_a = window.gtag) === null || _a === void 0 ? void 0 : _a.call(window, 'js', new Date());
169
254
  (_b = window.gtag) === null || _b === void 0 ? void 0 : _b.call(window, 'config', GOOGLE_ANALYTICS_ID, {
170
255
  page_location: sanitizeUrlForGoogleTag((_c = window.location) === null || _c === void 0 ? void 0 : _c.href),
256
+ send_page_view: false,
171
257
  });
172
258
  }
173
259
  if (GOOGLE_TAG_MANAGER_ID) {
@@ -74,10 +74,13 @@ export declare class PublicAnalyticsSdkService {
74
74
  trackStepStarted(input: PublicSdkStepStartInput): string | null;
75
75
  trackStepCompleted(input: PublicSdkStepEndInput): string | null;
76
76
  trackStepEngaged(input: PublicSdkStepEngagedInput): string | null;
77
+ trackFunnelStarted(input: PublicSdkConversionCallbackInput): string | null;
77
78
  trackFirstStepClicked(input: PublicSdkConversionCallbackInput): string | null;
78
79
  trackFirstStepViewed(input: PublicSdkConversionCallbackInput): string | null;
79
80
  trackLead(input: PublicSdkConversionCallbackInput): string | null;
81
+ trackFunnelCompleted(input: PublicSdkConversionCallbackInput): string | null;
80
82
  trackCheckoutStarted(input: PublicSdkConversionCallbackInput): string | null;
83
+ trackPaymentInfoSubmitted(input: PublicSdkConversionCallbackInput): string | null;
81
84
  trackCheckoutCompleted(input: PublicSdkConversionCallbackInput): string | null;
82
85
  flush(): Promise<number>;
83
86
  private ensureUserId;
@@ -1,4 +1,4 @@
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';
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';
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,6 +129,7 @@ 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';
132
133
  const buildEventMetadata = (eventType, metadata) => {
133
134
  if (eventType !== 'checkout_completed') {
134
135
  return metadata;
@@ -228,10 +229,11 @@ export class PublicAnalyticsSdkService {
228
229
  }
229
230
  const eventId = asString(event.eventId) || buildEventId();
230
231
  const metadata = buildEventMetadata(eventType, asRecord(event.metadata));
232
+ const providerMetadata = buildDefinedRecord(Object.assign(Object.assign({}, metadata), { stepId: asString(event.stepId) || metadata.stepId, stepName: asString(event.stepName) || metadata.stepName }));
231
233
  const metaPixelEvent = mapAnalyticsEventToMetaPixelEvent({
232
234
  eventType,
233
235
  eventId,
234
- metadata,
236
+ metadata: providerMetadata,
235
237
  });
236
238
  if (metaPixelEvent) {
237
239
  metaPixelService.track(metaPixelEvent);
@@ -239,7 +241,7 @@ export class PublicAnalyticsSdkService {
239
241
  const googleTagEvent = mapAnalyticsEventToGoogleTagEvent({
240
242
  eventType,
241
243
  eventId,
242
- metadata,
244
+ metadata: providerMetadata,
243
245
  });
244
246
  if (googleTagEvent) {
245
247
  googleTagService.track(googleTagEvent);
@@ -314,6 +316,17 @@ export class PublicAnalyticsSdkService {
314
316
  featureFlags: input.featureFlags,
315
317
  });
316
318
  }
319
+ trackFunnelStarted(input) {
320
+ return this.track({
321
+ eventType: 'funnel_start',
322
+ eventId: input.eventId,
323
+ occurredAt: input.occurredAt,
324
+ stepId: input.stepId,
325
+ stepName: input.stepName,
326
+ metadata: input.metadata,
327
+ featureFlags: input.featureFlags,
328
+ });
329
+ }
317
330
  trackFirstStepClicked(input) {
318
331
  return this.track({
319
332
  eventType: 'first_step_clicked',
@@ -347,6 +360,17 @@ export class PublicAnalyticsSdkService {
347
360
  featureFlags: input.featureFlags,
348
361
  });
349
362
  }
363
+ trackFunnelCompleted(input) {
364
+ return this.track({
365
+ eventType: 'funnel_completed',
366
+ eventId: input.eventId,
367
+ occurredAt: input.occurredAt,
368
+ stepId: input.stepId,
369
+ stepName: input.stepName,
370
+ metadata: input.metadata,
371
+ featureFlags: input.featureFlags,
372
+ });
373
+ }
350
374
  trackCheckoutStarted(input) {
351
375
  return this.track({
352
376
  eventType: 'checkout_started',
@@ -358,6 +382,17 @@ export class PublicAnalyticsSdkService {
358
382
  featureFlags: input.featureFlags,
359
383
  });
360
384
  }
385
+ trackPaymentInfoSubmitted(input) {
386
+ return this.track({
387
+ eventType: 'add_payment_info',
388
+ eventId: input.eventId,
389
+ occurredAt: input.occurredAt,
390
+ stepId: input.stepId,
391
+ stepName: input.stepName,
392
+ metadata: input.metadata,
393
+ featureFlags: input.featureFlags,
394
+ });
395
+ }
361
396
  trackCheckoutCompleted(input) {
362
397
  return this.track({
363
398
  eventType: 'checkout_completed',
@@ -451,7 +486,7 @@ export class PublicAnalyticsSdkService {
451
486
  event: toPostHogEventName(event.eventType),
452
487
  properties: buildDefinedRecord(Object.assign(Object.assign({ distinct_id: this.ensureUserId(), $groups: {
453
488
  project: PROJECT_ID,
454
- }, $insert_id: event.eventId, project_id: PROJECT_ID, funnel_id: FUNNEL_ID || undefined, funnel_version_id: FUNNEL_VERSION_ID || undefined, environment: resolveAnalyticsEnvironment(), step_id: event.stepId || undefined, step_name: event.stepName || 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 })),
489
+ }, $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, 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 })),
455
490
  timestamp: event.occurredAt,
456
491
  });
457
492
  });
@@ -11,6 +11,7 @@ export declare const GOOGLE_TAG_ENABLED: boolean;
11
11
  export declare const GOOGLE_ANALYTICS_ID: string;
12
12
  export declare const GOOGLE_TAG_MANAGER_ID: string;
13
13
  export declare const GOOGLE_TAG_EVENT_TARGET: string;
14
+ export declare const STRIPE_ACTIVE_MODE: string;
14
15
  export declare const hasRealFunnelSdkPublishableKey: (value?: string) => boolean;
15
16
  export declare const getFunnelSdkPublishableKey: () => string | null;
16
17
  export declare const hasPostHogRuntimeConfig: () => boolean;
@@ -1,4 +1,4 @@
1
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
1
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
2
2
  import { resolveRuntimeSdkApiBaseUrl, runtimeEnvConfig, } from '@funnelsgrove/runtime/config/env.config';
3
3
  const DEFAULT_API_BASE_URL = 'https://sdk-api.funnelsgrove.com';
4
4
  const DEFAULT_FUNNEL_SDK_PUBLISHABLE_KEY = 'pk_test_app_deals_seed_public';
@@ -32,6 +32,7 @@ export const GOOGLE_TAG_ENABLED = ((_j = runtimeEnvConfig.googleTagEnabled) !==
32
32
  export const GOOGLE_ANALYTICS_ID = ((_k = runtimeEnvConfig.googleAnalyticsId) !== null && _k !== void 0 ? _k : '').trim();
33
33
  export const GOOGLE_TAG_MANAGER_ID = ((_l = runtimeEnvConfig.googleTagManagerId) !== null && _l !== void 0 ? _l : '').trim();
34
34
  export const GOOGLE_TAG_EVENT_TARGET = ((_m = runtimeEnvConfig.googleTagEventTarget) !== null && _m !== void 0 ? _m : '').trim();
35
+ export const STRIPE_ACTIVE_MODE = ((_o = runtimeEnvConfig.stripeActiveMode) !== null && _o !== void 0 ? _o : '').trim().toLowerCase();
35
36
  export const hasRealFunnelSdkPublishableKey = (value = FUNNEL_SDK_PUBLISHABLE_KEY) => {
36
37
  const normalized = value.trim();
37
38
  return Boolean(normalized) && normalized !== DEFAULT_FUNNEL_SDK_PUBLISHABLE_KEY;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",