@funnelsgrove/analytics 0.1.60 → 0.1.62

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.
@@ -27,6 +27,7 @@ const FLAT_ATTRIBUTION_KEYS = new Set([
27
27
  'twclid',
28
28
  'wbraid',
29
29
  ]);
30
+ const COHORT_CLICK_ID_KEYS = new Set(['fbc', 'fbclid', 'gbraid', 'gclid', 'wbraid']);
30
31
  const isRecord = (value) => {
31
32
  return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
32
33
  };
@@ -100,6 +101,24 @@ const normalizeFlatAttributionTouch = (value) => {
100
101
  return normalizedValue === undefined ? [] : [[normalizedKey, normalizedValue]];
101
102
  }));
102
103
  };
104
+ const hasCohortClickId = (value) => (Object.entries(asRecord(value)).some(([key, candidate]) => (COHORT_CLICK_ID_KEYS.has(key.trim().toLowerCase()) && Boolean(toString(candidate)))));
105
+ const resolveLastNonOrganicTouch = (input, attribution) => {
106
+ const currentFlatAttribution = normalizeFlatAttributionTouch(input);
107
+ const candidates = [
108
+ currentFlatAttribution,
109
+ attribution.lastNonOrganicTouch,
110
+ attribution.lastTouch,
111
+ attribution.firstTouch,
112
+ ];
113
+ for (const candidate of candidates) {
114
+ if (hasCohortClickId(candidate)) {
115
+ return asRecord(candidate);
116
+ }
117
+ }
118
+ return [attribution.lastTouch, attribution.firstTouch, currentFlatAttribution]
119
+ .map(asRecord)
120
+ .find((candidate) => Object.keys(candidate).length > 0) || {};
121
+ };
103
122
  export const sanitizePurchaseUrl = (value) => {
104
123
  const normalized = toString(value);
105
124
  if (!normalized) {
@@ -135,7 +154,8 @@ export const buildPurchaseAnalyticsAttributes = (input = {}, options = {}) => {
135
154
  const amountCents = toNumber(input.amountCents);
136
155
  const value = (_b = toNumber(input.value)) !== null && _b !== void 0 ? _b : (amountCents === undefined ? undefined : amountCents / 100);
137
156
  const funnelUserId = firstString(input.userId, input.user_id, input.cwClientUserId, input.cw_client_user_id);
138
- 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), 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
157
+ const lastNonOrganicTouch = resolveLastNonOrganicTouch(input, attribution);
158
+ 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
139
159
  ? sanitizePurchaseUrl(firstString(input.url, currentBrowserPurchaseUrl()))
140
160
  : undefined }));
141
161
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.60",
3
+ "version": "0.1.62",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/The-Solid-Grove/funnelsgrove.git",
@@ -36,7 +36,7 @@
36
36
  "test:run": "vitest run --passWithNoTests"
37
37
  },
38
38
  "dependencies": {
39
- "@funnelsgrove/runtime": "0.7.11"
39
+ "@funnelsgrove/runtime": "0.7.13"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/node": "^20",