@funnelsgrove/analytics 0.1.10 → 0.1.12

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 = 'begin_checkout' | 'purchase';
1
+ type GoogleTagEventName = 'purchase';
2
2
  export type GoogleTagHeadScript = {
3
3
  id: string;
4
4
  src?: string;
@@ -1,14 +1,3 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
1
  import { GOOGLE_ANALYTICS_ID, GOOGLE_TAG_ENABLED, GOOGLE_TAG_EVENT_TARGET, GOOGLE_TAG_MANAGER_ID, } from './runtime-api.config.js';
13
2
  import { buildBrowserPurchaseAnalyticsAttributes } from './purchase-attributes.service.js';
14
3
  const normalizeEventTarget = (value) => {
@@ -120,35 +109,14 @@ export const buildGoogleTagHeadScripts = () => {
120
109
  : []),
121
110
  ];
122
111
  };
123
- const toGoogleTagString = (value) => {
124
- return typeof value === 'string' && value.trim() ? value.trim() : undefined;
125
- };
126
- const toGoogleTagNumber = (value) => {
127
- return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
128
- };
129
- const buildGoogleTagItem = (parameters) => {
130
- const itemId = toGoogleTagString(parameters.provider_plan_id) || toGoogleTagString(parameters.plan_id);
131
- if (!itemId) {
132
- return undefined;
133
- }
134
- return {
135
- item_id: itemId,
136
- item_name: toGoogleTagString(parameters.plan_id) || itemId,
137
- price: toGoogleTagNumber(parameters.value),
138
- quantity: 1,
139
- };
140
- };
141
112
  export const mapAnalyticsEventToGoogleTagEvent = (event) => {
142
113
  var _a;
143
114
  switch (event.eventType) {
144
- case 'checkout_started': {
145
- const _b = buildBrowserPurchaseAnalyticsAttributes((_a = event.metadata) !== null && _a !== void 0 ? _a : {}), { transaction_id: _transactionId } = _b, baseParameters = __rest(_b, ["transaction_id"]);
146
- const item = buildGoogleTagItem(baseParameters);
115
+ case 'checkout_completed':
147
116
  return {
148
- eventName: 'begin_checkout',
149
- parameters: sanitizeGoogleTagParameters(Object.assign(Object.assign({}, baseParameters), (item ? { items: [item] } : {}))),
117
+ eventName: 'purchase',
118
+ parameters: sanitizeGoogleTagParameters(buildBrowserPurchaseAnalyticsAttributes(Object.assign(Object.assign({}, ((_a = event.metadata) !== null && _a !== void 0 ? _a : {})), { transactionId: event.eventId }))),
150
119
  };
151
- }
152
120
  default:
153
121
  return null;
154
122
  }
@@ -11,24 +11,15 @@ export type AnalyticsEventForMetaPixel = {
11
11
  };
12
12
  declare global {
13
13
  interface Window {
14
- fbq?: MetaPixelQueue;
15
- _fbq?: MetaPixelQueue;
14
+ fbq?: (...args: unknown[]) => void;
16
15
  }
17
16
  }
18
- type MetaPixelQueue = ((...args: unknown[]) => void) & {
19
- callMethod?: (...args: unknown[]) => void;
20
- loaded?: boolean;
21
- push?: MetaPixelQueue;
22
- queue?: unknown[][];
23
- version?: string;
24
- };
25
17
  export declare const mapAnalyticsEventToMetaPixelEvent: (event: AnalyticsEventForMetaPixel) => MetaPixelTrackInput | null;
26
18
  export declare class MetaPixelService {
27
19
  private initialized;
28
20
  track(input: MetaPixelTrackInput): void;
29
21
  private isConfigured;
30
22
  private ensureInitialized;
31
- private installQueue;
32
23
  private installScript;
33
24
  }
34
25
  export declare const metaPixelService: MetaPixelService;
@@ -73,43 +73,19 @@ export class MetaPixelService {
73
73
  return Boolean(META_PIXEL_ENABLED && META_PIXEL_ID && typeof window !== 'undefined');
74
74
  }
75
75
  ensureInitialized() {
76
+ var _a, _b;
76
77
  if (this.initialized) {
77
78
  return;
78
79
  }
79
80
  this.initialized = true;
80
- this.installQueue();
81
81
  this.installScript();
82
- const fbq = window.fbq;
83
- fbq === null || fbq === void 0 ? void 0 : fbq('init', META_PIXEL_ID);
84
- fbq === null || fbq === void 0 ? void 0 : fbq('track', 'PageView');
85
- }
86
- installQueue() {
87
- if (window.fbq) {
88
- return;
89
- }
90
- const fbq = ((...args) => {
91
- var _a;
92
- if (fbq.callMethod) {
93
- fbq.callMethod(...args);
94
- return;
95
- }
96
- (_a = fbq.queue) === null || _a === void 0 ? void 0 : _a.push(args);
97
- });
98
- window._fbq = window._fbq || fbq;
99
- fbq.push = fbq;
100
- fbq.loaded = true;
101
- fbq.version = '2.0';
102
- fbq.queue = [];
103
- window.fbq = fbq;
82
+ (_a = window.fbq) === null || _a === void 0 ? void 0 : _a.call(window, 'init', META_PIXEL_ID);
83
+ (_b = window.fbq) === null || _b === void 0 ? void 0 : _b.call(window, 'track', 'PageView');
104
84
  }
105
85
  installScript() {
106
- var _a;
107
86
  if (typeof document === 'undefined') {
108
87
  return;
109
88
  }
110
- if ((_a = document.querySelector) === null || _a === void 0 ? void 0 : _a.call(document, 'script[src="https://connect.facebook.net/en_US/fbevents.js"]')) {
111
- return;
112
- }
113
89
  const firstScript = document.getElementsByTagName('script')[0];
114
90
  const parent = firstScript === null || firstScript === void 0 ? void 0 : firstScript.parentNode;
115
91
  if (!parent) {
@@ -1,26 +1,16 @@
1
1
  export type PurchaseAnalyticsAttributesInput = {
2
2
  amountCents?: unknown;
3
3
  attribution?: unknown;
4
- checkoutSessionId?: unknown;
5
- checkout_session_id?: unknown;
6
4
  couponId?: unknown;
7
5
  currency?: unknown;
8
6
  environment?: unknown;
9
- fallbackTransactionId?: unknown;
10
- fallback_transaction_id?: unknown;
11
7
  funnelId?: unknown;
12
8
  funnelUserDocument?: unknown;
13
9
  funnelVersion?: unknown;
14
10
  funnelVersionId?: unknown;
15
- gaClientId?: unknown;
16
- ga_client_id?: unknown;
17
11
  geoCity?: unknown;
18
12
  geoCountry?: unknown;
19
- googleAnalyticsClientId?: unknown;
20
- google_analytics_client_id?: unknown;
21
13
  ipAddress?: unknown;
22
- paymentIntentId?: unknown;
23
- payment_intent_id?: unknown;
24
14
  planId?: unknown;
25
15
  projectId?: unknown;
26
16
  providerPlanId?: unknown;
@@ -28,12 +18,13 @@ export type PurchaseAnalyticsAttributesInput = {
28
18
  timezone?: unknown;
29
19
  url?: unknown;
30
20
  userAgent?: unknown;
21
+ cwClientUserId?: unknown;
22
+ cw_client_user_id?: unknown;
31
23
  userId?: unknown;
32
24
  value?: unknown;
33
25
  };
34
26
  export declare const sanitizePurchaseUrl: (value: unknown) => string | undefined;
35
27
  export declare const currentBrowserPurchaseUrl: () => string | undefined;
36
- export declare const currentGoogleAnalyticsClientId: () => string | undefined;
37
28
  export declare const buildPurchaseAnalyticsAttributes: (input?: PurchaseAnalyticsAttributesInput & Record<string, unknown>, options?: {
38
29
  includeUrl?: boolean;
39
30
  }) => Record<string, unknown>;
@@ -125,36 +125,6 @@ export const currentBrowserPurchaseUrl = () => {
125
125
  }
126
126
  return sanitizePurchaseUrl((_a = window.location) === null || _a === void 0 ? void 0 : _a.href);
127
127
  };
128
- const readBrowserCookie = (name) => {
129
- if (typeof document === 'undefined') {
130
- return undefined;
131
- }
132
- const prefix = `${name}=`;
133
- const cookie = document.cookie
134
- .split(';')
135
- .map((value) => value.trim())
136
- .find((value) => value.startsWith(prefix));
137
- if (!cookie) {
138
- return undefined;
139
- }
140
- try {
141
- return decodeURIComponent(cookie.slice(prefix.length));
142
- }
143
- catch (_a) {
144
- return cookie.slice(prefix.length);
145
- }
146
- };
147
- export const currentGoogleAnalyticsClientId = () => {
148
- const gaCookie = readBrowserCookie('_ga');
149
- if (!gaCookie) {
150
- return undefined;
151
- }
152
- const parts = gaCookie.split('.');
153
- if (parts.length >= 4 && /^GA\d+$/.test(parts[0])) {
154
- return parts.slice(-2).join('.');
155
- }
156
- return gaCookie.trim() || undefined;
157
- };
158
128
  export const buildPurchaseAnalyticsAttributes = (input = {}, options = {}) => {
159
129
  var _a, _b;
160
130
  const document = asRecord(input.funnelUserDocument);
@@ -164,7 +134,8 @@ export const buildPurchaseAnalyticsAttributes = (input = {}, options = {}) => {
164
134
  const transactionInfo = asRecord(document.transactionInfo);
165
135
  const amountCents = toNumber(input.amountCents);
166
136
  const value = (_b = toNumber(input.value)) !== null && _b !== void 0 ? _b : (amountCents === undefined ? undefined : amountCents / 100);
167
- 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, input.checkoutSessionId, input.checkout_session_id, input.paymentIntentId, input.payment_intent_id, input.fallbackTransactionId, input.fallback_transaction_id), url: options.includeUrl
137
+ 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), 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
168
139
  ? sanitizePurchaseUrl(firstString(input.url, currentBrowserPurchaseUrl()))
169
140
  : undefined }));
170
141
  };
@@ -189,5 +160,5 @@ export const toStripeMetadata = (attributes) => {
189
160
  }));
190
161
  };
191
162
  export const buildStripePurchaseMetadata = (input = {}) => {
192
- return toStripeMetadata(Object.assign(Object.assign({}, buildPurchaseAnalyticsAttributes(input, { includeUrl: false })), { ga_client_id: firstString(input.gaClientId, input.ga_client_id, input.googleAnalyticsClientId, input.google_analytics_client_id, currentGoogleAnalyticsClientId()) }));
163
+ return toStripeMetadata(buildPurchaseAnalyticsAttributes(input, { includeUrl: false }));
193
164
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",