@funnelsgrove/analytics 0.3.0 → 0.4.1

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,5 +1,6 @@
1
1
  import { isSystemPreviewHostname } from '@funnelsgrove/runtime';
2
2
  import { META_PIXEL_ENABLED, META_PIXEL_ID, STRIPE_ACTIVE_MODE } from './runtime-api.config.js';
3
+ import { buildPurchaseAnalyticsAttributes } from './purchase-attributes.service.js';
3
4
  const toNumber = (value) => {
4
5
  if (typeof value !== 'number' || !Number.isFinite(value)) {
5
6
  return null;
@@ -10,10 +11,9 @@ const toCurrency = (value) => {
10
11
  return typeof value === 'string' && value.trim() ? value.trim().toUpperCase() : undefined;
11
12
  };
12
13
  const buildCommerceCustomData = (metadata = {}) => {
13
- var _a;
14
- const amountCents = toNumber(metadata.amountCents);
15
- const value = (_a = toNumber(metadata.value)) !== null && _a !== void 0 ? _a : (amountCents === null ? null : amountCents / 100);
16
- const currency = toCurrency(metadata.currency);
14
+ const attributes = buildPurchaseAnalyticsAttributes(metadata);
15
+ const value = toNumber(attributes.value);
16
+ const currency = toCurrency(attributes.currency);
17
17
  const customData = {};
18
18
  if (currency) {
19
19
  customData.currency = currency;
@@ -1,5 +1,6 @@
1
1
  import { type PostHogDeliveryOptions } from './posthog-delivery.js';
2
2
  import { type FunnelStepExitReason, type FunnelStepType } from '@funnelsgrove/runtime';
3
+ export type PublicAnalyticsFeatureFlags = Readonly<Record<string, string | boolean | null | undefined>>;
3
4
  export type PublicSdkTrackEventInput = {
4
5
  eventType: string;
5
6
  userId?: string;
@@ -18,7 +19,7 @@ export type PublicSdkTrackEventInput = {
18
19
  payload?: Record<string, unknown>;
19
20
  context?: Record<string, unknown>;
20
21
  metadata?: Record<string, unknown>;
21
- featureFlags?: Record<string, string | null | undefined>;
22
+ featureFlags?: PublicAnalyticsFeatureFlags;
22
23
  };
23
24
  export type PublicSdkStepStartInput = {
24
25
  userId?: string;
@@ -29,7 +30,7 @@ export type PublicSdkStepStartInput = {
29
30
  stepContractVersion: number;
30
31
  startedAt: string;
31
32
  metadata?: Record<string, unknown>;
32
- featureFlags?: Record<string, string | null | undefined>;
33
+ featureFlags?: PublicAnalyticsFeatureFlags;
33
34
  };
34
35
  export type PublicSdkStepEndInput = {
35
36
  userId?: string;
@@ -42,7 +43,7 @@ export type PublicSdkStepEndInput = {
42
43
  endedAt: string;
43
44
  selected?: Record<string, unknown>;
44
45
  metadata?: Record<string, unknown>;
45
- featureFlags?: Record<string, string | null | undefined>;
46
+ featureFlags?: PublicAnalyticsFeatureFlags;
46
47
  };
47
48
  export type PublicSdkStepExitInput = {
48
49
  userId?: string;
@@ -56,7 +57,7 @@ export type PublicSdkStepExitInput = {
56
57
  exitReason: FunnelStepExitReason;
57
58
  stepContractVersion: number;
58
59
  metadata?: Record<string, unknown>;
59
- featureFlags?: Record<string, string | null | undefined>;
60
+ featureFlags?: PublicAnalyticsFeatureFlags;
60
61
  };
61
62
  export type PublicSdkStepEngagedInput = {
62
63
  userId?: string;
@@ -70,7 +71,7 @@ export type PublicSdkStepEngagedInput = {
70
71
  durationMs: number;
71
72
  engagementThresholdMs: number;
72
73
  metadata?: Record<string, unknown>;
73
- featureFlags?: Record<string, string | null | undefined>;
74
+ featureFlags?: PublicAnalyticsFeatureFlags;
74
75
  };
75
76
  export type PublicSdkConversionCallbackInput = {
76
77
  userId?: string;
@@ -82,7 +83,7 @@ export type PublicSdkConversionCallbackInput = {
82
83
  stepType?: string;
83
84
  stepContractVersion?: number;
84
85
  metadata?: Record<string, unknown>;
85
- featureFlags?: Record<string, string | null | undefined>;
86
+ featureFlags?: PublicAnalyticsFeatureFlags;
86
87
  };
87
88
  export type PublicSdkFunnelConversionCallbackInput = PublicSdkConversionCallbackInput & {
88
89
  stepContractVersion: number;
@@ -132,7 +132,10 @@ const normalizeFeatureFlags = (featureFlags) => {
132
132
  return {};
133
133
  }
134
134
  return Object.fromEntries(Object.entries(featureFlags)
135
- .map(([key, value]) => [key.trim(), (value === null || value === void 0 ? void 0 : value.trim()) || ''])
135
+ .map(([key, value]) => [
136
+ key.trim(),
137
+ typeof value === 'boolean' ? String(value) : (value === null || value === void 0 ? void 0 : value.trim()) || '',
138
+ ])
136
139
  .filter(([key, value]) => Boolean(key && value)));
137
140
  };
138
141
  const buildFeatureFlagProperties = (featureFlags) => {
@@ -1,5 +1,6 @@
1
1
  export type PurchaseAnalyticsAttributesInput = {
2
2
  amountCents?: unknown;
3
+ money?: unknown;
3
4
  attribution?: unknown;
4
5
  couponId?: unknown;
5
6
  currency?: unknown;
@@ -1,4 +1,5 @@
1
1
  import { sanitizeAnalyticsUrl } from './analytics-export.js';
2
+ import { moneyToMajor, normalizeProviderMoney } from '@funnelsgrove/runtime';
2
3
  const STRIPE_METADATA_KEY_MAX_LENGTH = 40;
3
4
  const STRIPE_METADATA_VALUE_MAX_LENGTH = 500;
4
5
  const FLAT_ATTRIBUTION_KEYS = new Set([
@@ -118,17 +119,23 @@ export const currentBrowserPurchaseUrl = () => {
118
119
  return sanitizePurchaseUrl((_a = window.location) === null || _a === void 0 ? void 0 : _a.href);
119
120
  };
120
121
  export const buildPurchaseAnalyticsAttributes = (input = {}, options = {}) => {
121
- var _a, _b;
122
+ var _a, _b, _c, _d;
122
123
  const document = asRecord(input.funnelUserDocument);
123
124
  const attribution = asRecord((_a = input.attribution) !== null && _a !== void 0 ? _a : document.attribution);
124
125
  const attributionContext = asRecord(attribution.context);
125
126
  const progressAttributes = asRecord(asRecord(document.progress).attributes);
126
127
  const transactionInfo = asRecord(document.transactionInfo);
127
128
  const amountCents = toNumber(input.amountCents);
128
- const value = (_b = toNumber(input.value)) !== null && _b !== void 0 ? _b : (amountCents === undefined ? undefined : amountCents / 100);
129
+ const money = (_b = normalizeProviderMoney(input.money)) !== null && _b !== void 0 ? _b : undefined;
130
+ const hasExplicitMoney = input.money !== undefined && input.money !== null;
131
+ const value = (_c = toNumber(input.value)) !== null && _c !== void 0 ? _c : (money
132
+ ? moneyToMajor(money)
133
+ : hasExplicitMoney || amountCents === undefined
134
+ ? undefined
135
+ : amountCents / 100);
129
136
  const funnelUserId = firstString(input.userId, input.user_id, input.cwClientUserId, input.cw_client_user_id);
130
137
  const lastNonOrganicTouch = resolveLastNonOrganicTouch(input, attribution);
131
- return buildDefinedRecord(Object.assign(Object.assign({}, normalizeAttributionTouch(lastNonOrganicTouch)), { currency: toCurrency(input.currency), value, plan_id: firstString(input.planId, input.plan_id), provider_plan_id: firstString(input.providerPlanId, input.provider_plan_id), coupon_id: firstString(input.couponId, input.coupon_id), environment: firstString(input.environment), checkout_start_source: firstString(input.checkoutStartSource, input.checkout_start_source), step_id: firstString(input.stepId, input.step_id), step_name: firstString(input.stepName, input.step_name), step_type: firstString(input.stepType, input.step_type), user_id: funnelUserId, cw_client_user_id: funnelUserId, project_id: firstString(input.projectId, input.project_id), funnel_id: firstString(input.funnelId, input.funnel_id), funnel_version: firstString(input.funnelVersion, input.funnelVersionId, input.funnel_version), ip_address: firstString(input.ipAddress, input.ip_address, transactionInfo.ipAddress, transactionInfo.ip_address), user_agent: firstString(input.userAgent, input.user_agent, attributionContext.userAgent), timezone: firstString(input.timezone, input.time_zone, progressAttributes.timezone, progressAttributes.time_zone, document.timezone, document.time_zone, attributionContext.timezone, attributionContext.time_zone), geo_city: firstString(input.geoCity, input.geo_city, progressAttributes.geo_city, progressAttributes.geoCity, document.geo_city, document.geoCity, asRecord(document.geo).city, attributionContext.geo_city, attributionContext.geoCity, attributionContext.cityName, attributionContext.city), geo_country: firstString(input.geoCountry, input.geo_country, progressAttributes.geo_country, progressAttributes.geoCountry, document.geo_country, document.geoCountry, asRecord(document.geo).country, attributionContext.geo_country, attributionContext.geoCountry, attributionContext.countryName, attributionContext.countryCode, attributionContext.country), transaction_id: firstString(input.transactionId, input.transaction_id), url: options.includeUrl
138
+ return buildDefinedRecord(Object.assign(Object.assign({}, normalizeAttributionTouch(lastNonOrganicTouch)), { currency: (_d = money === null || money === void 0 ? void 0 : money.currency) !== null && _d !== void 0 ? _d : toCurrency(input.currency), value, plan_id: firstString(input.planId, input.plan_id), provider_plan_id: firstString(input.providerPlanId, input.provider_plan_id), coupon_id: firstString(input.couponId, input.coupon_id), environment: firstString(input.environment), checkout_start_source: firstString(input.checkoutStartSource, input.checkout_start_source), step_id: firstString(input.stepId, input.step_id), step_name: firstString(input.stepName, input.step_name), step_type: firstString(input.stepType, input.step_type), user_id: funnelUserId, cw_client_user_id: funnelUserId, project_id: firstString(input.projectId, input.project_id), funnel_id: firstString(input.funnelId, input.funnel_id), funnel_version: firstString(input.funnelVersion, input.funnelVersionId, input.funnel_version), ip_address: firstString(input.ipAddress, input.ip_address, transactionInfo.ipAddress, transactionInfo.ip_address), user_agent: firstString(input.userAgent, input.user_agent, attributionContext.userAgent), timezone: firstString(input.timezone, input.time_zone, progressAttributes.timezone, progressAttributes.time_zone, document.timezone, document.time_zone, attributionContext.timezone, attributionContext.time_zone), geo_city: firstString(input.geoCity, input.geo_city, progressAttributes.geo_city, progressAttributes.geoCity, document.geo_city, document.geoCity, asRecord(document.geo).city, attributionContext.geo_city, attributionContext.geoCity, attributionContext.cityName, attributionContext.city), geo_country: firstString(input.geoCountry, input.geo_country, progressAttributes.geo_country, progressAttributes.geoCountry, document.geo_country, document.geoCountry, asRecord(document.geo).country, attributionContext.geo_country, attributionContext.geoCountry, attributionContext.countryName, attributionContext.countryCode, attributionContext.country), transaction_id: firstString(input.transactionId, input.transaction_id), url: options.includeUrl
132
139
  ? sanitizePurchaseUrl(firstString(input.url, currentBrowserPurchaseUrl()))
133
140
  : undefined }));
134
141
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
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.20.0"
48
+ "@funnelsgrove/runtime": "0.21.1"
49
49
  },
50
50
  "peerDependencies": {
51
- "@funnelsgrove/runtime": "^0.20.0"
51
+ "@funnelsgrove/runtime": "^0.21.1"
52
52
  }
53
53
  }