@funnelsgrove/analytics 0.1.8 → 0.1.9

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.
@@ -2,6 +2,7 @@ import { buildMainApiUrl, buildSdkHeaders, FUNNEL_ID, FUNNEL_VERSION_ID, getFunn
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';
5
+ import { currentBrowserPurchaseUrl, sanitizePurchaseUrl, } from './purchase-attributes.service.js';
5
6
  const DEFAULT_OPTIONS = {
6
7
  batchSize: 20,
7
8
  flushIntervalMs: 1500,
@@ -79,6 +80,13 @@ const normalizeFeatureFlags = (featureFlags) => {
79
80
  const buildFeatureFlagProperties = (featureFlags) => {
80
81
  return Object.fromEntries(Object.entries(featureFlags).map(([key, value]) => [`$feature/${key}`, value]));
81
82
  };
83
+ const buildEventMetadata = (eventType, metadata) => {
84
+ if (eventType !== 'checkout_completed') {
85
+ return metadata;
86
+ }
87
+ const checkoutUrl = sanitizePurchaseUrl(metadata.url) || currentBrowserPurchaseUrl();
88
+ return checkoutUrl ? Object.assign(Object.assign({}, metadata), { url: checkoutUrl }) : metadata;
89
+ };
82
90
  export class PublicAnalyticsSdkService {
83
91
  constructor(options = {}) {
84
92
  this.options = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options);
@@ -170,10 +178,11 @@ export class PublicAnalyticsSdkService {
170
178
  return null;
171
179
  }
172
180
  const eventId = asString(event.eventId) || buildEventId();
181
+ const metadata = buildEventMetadata(eventType, asRecord(event.metadata));
173
182
  const metaPixelEvent = mapAnalyticsEventToMetaPixelEvent({
174
183
  eventType,
175
184
  eventId,
176
- metadata: asRecord(event.metadata),
185
+ metadata,
177
186
  });
178
187
  if (metaPixelEvent) {
179
188
  metaPixelService.track(metaPixelEvent);
@@ -181,7 +190,7 @@ export class PublicAnalyticsSdkService {
181
190
  const googleTagEvent = mapAnalyticsEventToGoogleTagEvent({
182
191
  eventType,
183
192
  eventId,
184
- metadata: asRecord(event.metadata),
193
+ metadata,
185
194
  });
186
195
  if (googleTagEvent) {
187
196
  googleTagService.track(googleTagEvent);
@@ -202,7 +211,7 @@ export class PublicAnalyticsSdkService {
202
211
  selected: asRecord(event.selected),
203
212
  payload: asRecord(event.payload),
204
213
  context: asRecord(event.context),
205
- metadata: asRecord(event.metadata),
214
+ metadata,
206
215
  featureFlags: normalizeFeatureFlags(event.featureFlags),
207
216
  };
208
217
  this.eventQueue.push(envelope);
@@ -21,6 +21,8 @@ export type PurchaseAnalyticsAttributesInput = {
21
21
  userId?: unknown;
22
22
  value?: unknown;
23
23
  };
24
+ export declare const sanitizePurchaseUrl: (value: unknown) => string | undefined;
25
+ export declare const currentBrowserPurchaseUrl: () => string | undefined;
24
26
  export declare const buildPurchaseAnalyticsAttributes: (input?: PurchaseAnalyticsAttributesInput & Record<string, unknown>, options?: {
25
27
  includeUrl?: boolean;
26
28
  }) => Record<string, unknown>;
@@ -1,5 +1,32 @@
1
1
  const STRIPE_METADATA_KEY_MAX_LENGTH = 40;
2
2
  const STRIPE_METADATA_VALUE_MAX_LENGTH = 500;
3
+ const SENSITIVE_PURCHASE_URL_QUERY_KEYS = new Set([
4
+ 'customeremail',
5
+ 'customer_email',
6
+ 'email',
7
+ 'fullname',
8
+ 'full_name',
9
+ 'name',
10
+ 'phone',
11
+ 'phonenumber',
12
+ 'phone_number',
13
+ ]);
14
+ const FLAT_ATTRIBUTION_KEYS = new Set([
15
+ '_fbc',
16
+ '_fbp',
17
+ 'epik',
18
+ 'fbc',
19
+ 'fbclid',
20
+ 'fbp',
21
+ 'gbraid',
22
+ 'gclid',
23
+ 'li_fat_id',
24
+ 'msclkid',
25
+ 'scclid',
26
+ 'ttclid',
27
+ 'twclid',
28
+ 'wbraid',
29
+ ]);
3
30
  const isRecord = (value) => {
4
31
  return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
5
32
  };
@@ -59,12 +86,44 @@ const normalizeAttributionTouch = (value) => {
59
86
  return [[normalizedKey, normalizedValue]];
60
87
  }));
61
88
  };
62
- const currentBrowserUrl = () => {
89
+ const isFlatAttributionKey = (key) => {
90
+ const normalizedKey = key.trim().toLowerCase();
91
+ return normalizedKey.startsWith('utm_') || FLAT_ATTRIBUTION_KEYS.has(normalizedKey);
92
+ };
93
+ const normalizeFlatAttributionTouch = (value) => {
94
+ return Object.fromEntries(Object.entries(value).flatMap(([key, candidate]) => {
95
+ const normalizedKey = key.trim();
96
+ if (!normalizedKey || !isFlatAttributionKey(normalizedKey)) {
97
+ return [];
98
+ }
99
+ const normalizedValue = toScalar(candidate);
100
+ return normalizedValue === undefined ? [] : [[normalizedKey, normalizedValue]];
101
+ }));
102
+ };
103
+ export const sanitizePurchaseUrl = (value) => {
104
+ const normalized = toString(value);
105
+ if (!normalized) {
106
+ return undefined;
107
+ }
108
+ try {
109
+ const parsed = new URL(normalized, typeof window !== 'undefined' ? window.location.origin : 'https://example.invalid');
110
+ for (const key of Array.from(parsed.searchParams.keys())) {
111
+ if (SENSITIVE_PURCHASE_URL_QUERY_KEYS.has(key.trim().toLowerCase())) {
112
+ parsed.searchParams.delete(key);
113
+ }
114
+ }
115
+ return parsed.toString();
116
+ }
117
+ catch (_a) {
118
+ return normalized;
119
+ }
120
+ };
121
+ export const currentBrowserPurchaseUrl = () => {
63
122
  var _a;
64
123
  if (typeof window === 'undefined') {
65
124
  return undefined;
66
125
  }
67
- return toString((_a = window.location) === null || _a === void 0 ? void 0 : _a.href);
126
+ return sanitizePurchaseUrl((_a = window.location) === null || _a === void 0 ? void 0 : _a.href);
68
127
  };
69
128
  export const buildPurchaseAnalyticsAttributes = (input = {}, options = {}) => {
70
129
  var _a, _b;
@@ -75,7 +134,9 @@ export const buildPurchaseAnalyticsAttributes = (input = {}, options = {}) => {
75
134
  const transactionInfo = asRecord(document.transactionInfo);
76
135
  const amountCents = toNumber(input.amountCents);
77
136
  const value = (_b = toNumber(input.value)) !== null && _b !== void 0 ? _b : (amountCents === undefined ? undefined : amountCents / 100);
78
- return buildDefinedRecord(Object.assign(Object.assign({}, normalizeAttributionTouch(attribution.firstTouch)), { 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), user_id: firstString(input.userId, input.user_id), 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 ? firstString(input.url, currentBrowserUrl()) : undefined }));
137
+ return buildDefinedRecord(Object.assign(Object.assign(Object.assign({}, normalizeFlatAttributionTouch(input)), normalizeAttributionTouch(attribution.firstTouch)), { 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), user_id: firstString(input.userId, input.user_id), 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
+ ? sanitizePurchaseUrl(firstString(input.url, currentBrowserPurchaseUrl()))
139
+ : undefined }));
79
140
  };
80
141
  export const buildBrowserPurchaseAnalyticsAttributes = (input = {}) => {
81
142
  return buildPurchaseAnalyticsAttributes(input, { includeUrl: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",