@funnelsgrove/analytics 0.1.11 → 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;
@@ -13,13 +13,6 @@ export type AnalyticsEventForGoogleTag = {
13
13
  eventType: string;
14
14
  metadata?: Record<string, unknown>;
15
15
  };
16
- type GoogleTagMetaPixelQueue = ((...args: unknown[]) => void) & {
17
- callMethod?: (...args: unknown[]) => void;
18
- loaded?: boolean;
19
- push?: GoogleTagMetaPixelQueue;
20
- queue?: unknown[][];
21
- version?: string;
22
- };
23
16
  declare global {
24
17
  interface Window {
25
18
  __funnelsgroveGoogleTagHeadReady?: boolean;
@@ -27,9 +20,7 @@ declare global {
27
20
  __funnelsgroveGoogleTagServiceReady?: boolean;
28
21
  __funnelsgroveSanitizeGoogleTagUrl?: (value: string) => string;
29
22
  dataLayer?: unknown[];
30
- fbq?: GoogleTagMetaPixelQueue;
31
23
  gtag?: (...args: unknown[]) => void;
32
- _fbq?: GoogleTagMetaPixelQueue;
33
24
  }
34
25
  }
35
26
  export declare const buildGoogleTagHeadScripts: () => GoogleTagHeadScript[];
@@ -41,7 +32,6 @@ export declare class GoogleTagService {
41
32
  private isConfigured;
42
33
  private ensureInitialized;
43
34
  private ensureDataLayer;
44
- private ensureMetaPixelQueue;
45
35
  private installScript;
46
36
  }
47
37
  export declare const googleTagService: GoogleTagService;
@@ -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) => {
@@ -93,7 +82,6 @@ export const buildGoogleTagHeadScripts = () => {
93
82
  : []),
94
83
  ...(GOOGLE_TAG_MANAGER_ID
95
84
  ? [
96
- 'if (!window.fbq) { window.fbq = function(){ window.fbq.callMethod ? window.fbq.callMethod.apply(window.fbq, arguments) : window.fbq.queue.push(arguments); }; window._fbq = window._fbq || window.fbq; window.fbq.push = window.fbq; window.fbq.loaded = true; window.fbq.version = "2.0"; window.fbq.queue = []; }',
97
85
  'window.dataLayer.push({"gtm.start": Date.now(), event: "gtm.js"});',
98
86
  ]
99
87
  : []),
@@ -121,35 +109,14 @@ export const buildGoogleTagHeadScripts = () => {
121
109
  : []),
122
110
  ];
123
111
  };
124
- const toGoogleTagString = (value) => {
125
- return typeof value === 'string' && value.trim() ? value.trim() : undefined;
126
- };
127
- const toGoogleTagNumber = (value) => {
128
- return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
129
- };
130
- const buildGoogleTagItem = (parameters) => {
131
- const itemId = toGoogleTagString(parameters.provider_plan_id) || toGoogleTagString(parameters.plan_id);
132
- if (!itemId) {
133
- return undefined;
134
- }
135
- return {
136
- item_id: itemId,
137
- item_name: toGoogleTagString(parameters.plan_id) || itemId,
138
- price: toGoogleTagNumber(parameters.value),
139
- quantity: 1,
140
- };
141
- };
142
112
  export const mapAnalyticsEventToGoogleTagEvent = (event) => {
143
113
  var _a;
144
114
  switch (event.eventType) {
145
- case 'checkout_started': {
146
- const _b = buildBrowserPurchaseAnalyticsAttributes((_a = event.metadata) !== null && _a !== void 0 ? _a : {}), { transaction_id: _transactionId } = _b, baseParameters = __rest(_b, ["transaction_id"]);
147
- const item = buildGoogleTagItem(baseParameters);
115
+ case 'checkout_completed':
148
116
  return {
149
- eventName: 'begin_checkout',
150
- 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 }))),
151
119
  };
152
- }
153
120
  default:
154
121
  return null;
155
122
  }
@@ -203,7 +170,6 @@ export class GoogleTagService {
203
170
  });
204
171
  }
205
172
  if (GOOGLE_TAG_MANAGER_ID) {
206
- this.ensureMetaPixelQueue();
207
173
  (_d = window.dataLayer) === null || _d === void 0 ? void 0 : _d.push({
208
174
  'gtm.start': Date.now(),
209
175
  event: 'gtm.js',
@@ -218,25 +184,6 @@ export class GoogleTagService {
218
184
  (_a = window.dataLayer) === null || _a === void 0 ? void 0 : _a.push(args);
219
185
  });
220
186
  }
221
- ensureMetaPixelQueue() {
222
- if (window.fbq) {
223
- return;
224
- }
225
- const fbq = ((...args) => {
226
- var _a;
227
- if (fbq.callMethod) {
228
- fbq.callMethod(...args);
229
- return;
230
- }
231
- (_a = fbq.queue) === null || _a === void 0 ? void 0 : _a.push(args);
232
- });
233
- window._fbq = window._fbq || fbq;
234
- fbq.push = fbq;
235
- fbq.loaded = true;
236
- fbq.version = '2.0';
237
- fbq.queue = [];
238
- window.fbq = fbq;
239
- }
240
187
  installScript(src) {
241
188
  var _a;
242
189
  if (typeof document === 'undefined') {
@@ -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.11",
3
+ "version": "0.1.12",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",