@funnelsgrove/analytics 0.1.29 → 0.1.31

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.
package/README.md CHANGED
@@ -44,6 +44,22 @@ Read this file before editing analytics code. Deeper runtime SDK docs live in
44
44
  - Do not import payment UI or runtime routing from this package.
45
45
  - Sensitive values must be filtered before metadata reaches Stripe or partner tools.
46
46
 
47
+ ## Google Tag Configuration
48
+
49
+ `NEXT_PUBLIC_GOOGLE_ANALYTICS_ID` and `NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID`
50
+ configure which Google tags load. Google Ads conversion actions also need the
51
+ full `send_to` destination, such as `AW-123456789/ConversionLabel`.
52
+
53
+ - `NEXT_PUBLIC_GOOGLE_TAG_LEAD_SEND_TO` sends an Ads `conversion` event when the
54
+ SDK records a lead/email capture.
55
+ - `NEXT_PUBLIC_GOOGLE_TAG_PURCHASE_SEND_TO` sends an Ads `conversion` event when
56
+ the SDK records a paid checkout completion.
57
+
58
+ When either `send_to` value is present, Google Tag is treated as enabled even if
59
+ `NEXT_PUBLIC_GOOGLE_TAG_ENABLED` is not set. The `AW-...` prefix is used to load
60
+ and configure `gtag.js`; the full `AW-.../...` value is sent on the conversion
61
+ event.
62
+
47
63
  ## Build, Test, Publish
48
64
 
49
65
  ```bash
@@ -1,4 +1,4 @@
1
- type GoogleTagEventName = 'add_payment_info' | 'begin_checkout' | 'lead' | 'page_view' | 'purchase' | 'tutorial_begin' | 'tutorial_complete';
1
+ type GoogleTagEventName = 'add_payment_info' | 'begin_checkout' | 'conversion' | 'lead' | 'page_view' | 'purchase' | 'tutorial_begin' | 'tutorial_complete';
2
2
  export type GoogleTagHeadScript = {
3
3
  id: string;
4
4
  src?: string;
@@ -25,6 +25,8 @@ declare global {
25
25
  }
26
26
  export declare const buildGoogleTagHeadScripts: () => GoogleTagHeadScript[];
27
27
  export declare const mapAnalyticsEventToGoogleTagEvent: (event: AnalyticsEventForGoogleTag) => GoogleTagTrackInput | null;
28
+ export declare const mapAnalyticsEventToGoogleAdsConversionEvent: (event: AnalyticsEventForGoogleTag) => GoogleTagTrackInput | null;
29
+ export declare const mapAnalyticsEventToGoogleTagEvents: (event: AnalyticsEventForGoogleTag) => GoogleTagTrackInput[];
28
30
  export declare class GoogleTagService {
29
31
  private initialized;
30
32
  initialize(): void;
@@ -1,4 +1,4 @@
1
- import { GOOGLE_ANALYTICS_ID, GOOGLE_TAG_ENABLED, GOOGLE_TAG_EVENT_TARGET, GOOGLE_TAG_MANAGER_ID, } from './runtime-api.config.js';
1
+ import { GOOGLE_TAG_CONFIG_IDS, GOOGLE_TAG_ENABLED, GOOGLE_TAG_EVENT_TARGET, GOOGLE_TAG_LEAD_SEND_TO, GOOGLE_TAG_MANAGER_ID, GOOGLE_TAG_PURCHASE_SEND_TO, GOOGLE_TAG_SCRIPT_ID, } from './runtime-api.config.js';
2
2
  import { buildBrowserPurchaseAnalyticsAttributes, buildPurchaseAnalyticsAttributes, currentBrowserPurchaseUrl, } from './purchase-attributes.service.js';
3
3
  const normalizeEventTarget = (value) => {
4
4
  return value === 'dataLayer' || value === 'gtag' || value === 'both' ? value : 'both';
@@ -109,8 +109,11 @@ const buildCommerceParameters = (event, options = {}) => {
109
109
  : buildPurchaseAnalyticsAttributes(metadata);
110
110
  return sanitizeGoogleTagParameters(buildDefinedRecord(Object.assign(Object.assign({}, purchaseAttributes), buildStepParameters(event))));
111
111
  };
112
+ const buildGoogleAdsConversionParameters = (parameters, sendTo) => {
113
+ return sanitizeGoogleTagParameters(buildDefinedRecord(Object.assign(Object.assign({}, parameters), { send_to: sendTo })));
114
+ };
112
115
  export const buildGoogleTagHeadScripts = () => {
113
- if (!GOOGLE_TAG_ENABLED || (!GOOGLE_ANALYTICS_ID && !GOOGLE_TAG_MANAGER_ID)) {
116
+ if (!GOOGLE_TAG_ENABLED || (!GOOGLE_TAG_SCRIPT_ID && !GOOGLE_TAG_MANAGER_ID)) {
114
117
  return [];
115
118
  }
116
119
  const bootstrapLines = [
@@ -119,10 +122,10 @@ export const buildGoogleTagHeadScripts = () => {
119
122
  'window.__funnelsgroveSanitizeGoogleTagUrl = window.__funnelsgroveSanitizeGoogleTagUrl || function(value){ try { var url = new URL(value, window.location.origin); ["customeremail","customer_email","email","fullname","full_name","name","phone","phonenumber","phone_number"].forEach(function(key){ url.searchParams.delete(key); }); return url.href; } catch (error) { return value; } };',
120
123
  'window.__funnelsgroveGoogleTagPageLocation = window.__funnelsgroveSanitizeGoogleTagUrl(window.location.href);',
121
124
  'window.__funnelsgroveGoogleTagHeadReady = true;',
122
- ...(GOOGLE_ANALYTICS_ID
125
+ ...(GOOGLE_TAG_CONFIG_IDS.length > 0
123
126
  ? [
124
127
  'window.gtag("js", new Date());',
125
- `window.gtag("config", ${JSON.stringify(GOOGLE_ANALYTICS_ID)}, { page_location: window.__funnelsgroveGoogleTagPageLocation, send_page_view: false });`,
128
+ ...GOOGLE_TAG_CONFIG_IDS.map((googleTagConfigId) => `window.gtag("config", ${JSON.stringify(googleTagConfigId)}, { page_location: window.__funnelsgroveGoogleTagPageLocation, send_page_view: false });`),
126
129
  ]
127
130
  : []),
128
131
  ...(GOOGLE_TAG_MANAGER_ID
@@ -137,11 +140,11 @@ export const buildGoogleTagHeadScripts = () => {
137
140
  id: 'funnelsgrove-google-tag-bootstrap',
138
141
  content: bootstrapLines.join('\n'),
139
142
  },
140
- ...(GOOGLE_ANALYTICS_ID
143
+ ...(GOOGLE_TAG_SCRIPT_ID
141
144
  ? [
142
145
  {
143
146
  id: 'funnelsgrove-google-analytics',
144
- src: buildGoogleAnalyticsScriptSrc(GOOGLE_ANALYTICS_ID),
147
+ src: buildGoogleAnalyticsScriptSrc(GOOGLE_TAG_SCRIPT_ID),
145
148
  },
146
149
  ]
147
150
  : []),
@@ -207,6 +210,37 @@ export const mapAnalyticsEventToGoogleTagEvent = (event) => {
207
210
  return null;
208
211
  }
209
212
  };
213
+ export const mapAnalyticsEventToGoogleAdsConversionEvent = (event) => {
214
+ switch (event.eventType) {
215
+ case 'email_captured':
216
+ case 'lead':
217
+ if (!GOOGLE_TAG_LEAD_SEND_TO) {
218
+ return null;
219
+ }
220
+ return {
221
+ eventName: 'conversion',
222
+ parameters: buildGoogleAdsConversionParameters(Object.assign(Object.assign({}, buildStepParameters(event, { includeEventId: true })), { value: 0, currency: 'USD' }), GOOGLE_TAG_LEAD_SEND_TO),
223
+ };
224
+ case 'checkout_completed':
225
+ if (!GOOGLE_TAG_PURCHASE_SEND_TO) {
226
+ return null;
227
+ }
228
+ return {
229
+ eventName: 'conversion',
230
+ parameters: buildGoogleAdsConversionParameters(buildCommerceParameters(event, {
231
+ includeTransactionId: true,
232
+ }), GOOGLE_TAG_PURCHASE_SEND_TO),
233
+ };
234
+ default:
235
+ return null;
236
+ }
237
+ };
238
+ export const mapAnalyticsEventToGoogleTagEvents = (event) => {
239
+ return [
240
+ mapAnalyticsEventToGoogleTagEvent(event),
241
+ mapAnalyticsEventToGoogleAdsConversionEvent(event),
242
+ ].filter((googleTagEvent) => Boolean(googleTagEvent));
243
+ };
210
244
  export class GoogleTagService {
211
245
  constructor() {
212
246
  this.initialized = false;
@@ -234,7 +268,7 @@ export class GoogleTagService {
234
268
  }
235
269
  isConfigured() {
236
270
  return Boolean(GOOGLE_TAG_ENABLED &&
237
- (GOOGLE_ANALYTICS_ID || GOOGLE_TAG_MANAGER_ID) &&
271
+ (GOOGLE_TAG_SCRIPT_ID || GOOGLE_TAG_MANAGER_ID) &&
238
272
  typeof window !== 'undefined');
239
273
  }
240
274
  ensureInitialized() {
@@ -248,13 +282,15 @@ export class GoogleTagService {
248
282
  if (window.__funnelsgroveGoogleTagHeadReady) {
249
283
  return;
250
284
  }
251
- if (GOOGLE_ANALYTICS_ID) {
252
- this.installScript(buildGoogleAnalyticsScriptSrc(GOOGLE_ANALYTICS_ID));
285
+ if (GOOGLE_TAG_SCRIPT_ID) {
286
+ this.installScript(buildGoogleAnalyticsScriptSrc(GOOGLE_TAG_SCRIPT_ID));
253
287
  (_a = window.gtag) === null || _a === void 0 ? void 0 : _a.call(window, 'js', new Date());
254
- (_b = window.gtag) === null || _b === void 0 ? void 0 : _b.call(window, 'config', GOOGLE_ANALYTICS_ID, {
255
- page_location: sanitizeUrlForGoogleTag((_c = window.location) === null || _c === void 0 ? void 0 : _c.href),
256
- send_page_view: false,
257
- });
288
+ for (const googleTagConfigId of GOOGLE_TAG_CONFIG_IDS) {
289
+ (_b = window.gtag) === null || _b === void 0 ? void 0 : _b.call(window, 'config', googleTagConfigId, {
290
+ page_location: sanitizeUrlForGoogleTag((_c = window.location) === null || _c === void 0 ? void 0 : _c.href),
291
+ send_page_view: false,
292
+ });
293
+ }
258
294
  }
259
295
  if (GOOGLE_TAG_MANAGER_ID) {
260
296
  (_d = window.dataLayer) === null || _d === void 0 ? void 0 : _d.push({
@@ -1,7 +1,7 @@
1
1
  import { buildMainApiUrl, buildSdkHeaders, FUNNEL_ID, FUNNEL_VERSION_ID, getFunnelSdkPublishableKey, hasPostHogRuntimeConfig, POSTHOG_API_HOST, POSTHOG_PROJECT_API_KEY, PROJECT_ID, } from './runtime-api.config.js';
2
2
  import { canUseDom, getOrCreateUserId, persistUserId, readStoredUserId, } from './sdk-user-id.storage.js';
3
3
  import { mapAnalyticsEventToMetaPixelEvent, metaPixelService, } from './meta-pixel.service.js';
4
- import { googleTagService, mapAnalyticsEventToGoogleTagEvent, } from './google-tag.service.js';
4
+ import { googleTagService, mapAnalyticsEventToGoogleTagEvents, } from './google-tag.service.js';
5
5
  import { currentBrowserPurchaseUrl, sanitizePurchaseUrl, } from './purchase-attributes.service.js';
6
6
  const DEFAULT_OPTIONS = {
7
7
  batchSize: 20,
@@ -244,12 +244,12 @@ export class PublicAnalyticsSdkService {
244
244
  if (metaPixelEvent) {
245
245
  metaPixelService.track(metaPixelEvent);
246
246
  }
247
- const googleTagEvent = mapAnalyticsEventToGoogleTagEvent({
247
+ const googleTagEvents = mapAnalyticsEventToGoogleTagEvents({
248
248
  eventType,
249
249
  eventId,
250
250
  metadata: providerMetadata,
251
251
  });
252
- if (googleTagEvent) {
252
+ for (const googleTagEvent of googleTagEvents) {
253
253
  googleTagService.track(googleTagEvent);
254
254
  }
255
255
  if (!getFunnelSdkPublishableKey()) {
@@ -7,10 +7,15 @@ export declare const POSTHOG_API_HOST = "https://us.i.posthog.com";
7
7
  export declare const POSTHOG_PROJECT_API_KEY: string;
8
8
  export declare const META_PIXEL_ENABLED: boolean;
9
9
  export declare const META_PIXEL_ID: string;
10
- export declare const GOOGLE_TAG_ENABLED: boolean;
11
10
  export declare const GOOGLE_ANALYTICS_ID: string;
12
11
  export declare const GOOGLE_TAG_MANAGER_ID: string;
13
12
  export declare const GOOGLE_TAG_EVENT_TARGET: string;
13
+ export declare const GOOGLE_TAG_LEAD_SEND_TO: string;
14
+ export declare const GOOGLE_TAG_PURCHASE_SEND_TO: string;
15
+ export declare const GOOGLE_ADS_CONVERSION_ID: string;
16
+ export declare const GOOGLE_TAG_CONFIG_IDS: string[];
17
+ export declare const GOOGLE_TAG_SCRIPT_ID: string;
18
+ export declare const GOOGLE_TAG_ENABLED: boolean;
14
19
  export declare const STRIPE_ACTIVE_MODE: string;
15
20
  export declare const hasRealFunnelSdkPublishableKey: (value?: string) => boolean;
16
21
  export declare const getFunnelSdkPublishableKey: () => string | null;
@@ -1,10 +1,23 @@
1
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
1
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
2
2
  import { resolveRuntimeSdkApiBaseUrl, runtimeEnvConfig, } from '@funnelsgrove/runtime/config/env.config';
3
3
  const DEFAULT_API_BASE_URL = 'https://sdk-api.funnelsgrove.com';
4
4
  const DEFAULT_FUNNEL_SDK_PUBLISHABLE_KEY = 'pk_test_app_deals_seed_public';
5
5
  const trimTrailingSlash = (value) => {
6
6
  return value.replace(/\/+$/, '');
7
7
  };
8
+ const trimEnv = (value) => {
9
+ return (value !== null && value !== void 0 ? value : '').trim();
10
+ };
11
+ const resolveGoogleAdsConversionId = (...sendToValues) => {
12
+ var _a;
13
+ for (const sendTo of sendToValues) {
14
+ const conversionId = (_a = sendTo.split('/')[0]) === null || _a === void 0 ? void 0 : _a.trim();
15
+ if (conversionId) {
16
+ return conversionId;
17
+ }
18
+ }
19
+ return '';
20
+ };
8
21
  const toNormalizedPath = (path) => {
9
22
  return path.startsWith('/') ? path : `/${path}`;
10
23
  };
@@ -28,11 +41,20 @@ export const POSTHOG_API_HOST = 'https://us.i.posthog.com';
28
41
  export const POSTHOG_PROJECT_API_KEY = ((_f = runtimeEnvConfig.posthogProjectApiKey) !== null && _f !== void 0 ? _f : '').trim();
29
42
  export const META_PIXEL_ENABLED = ((_g = runtimeEnvConfig.metaPixelEnabled) !== null && _g !== void 0 ? _g : '').trim() === 'true';
30
43
  export const META_PIXEL_ID = ((_h = runtimeEnvConfig.metaPixelId) !== null && _h !== void 0 ? _h : '').trim();
31
- export const GOOGLE_TAG_ENABLED = ((_j = runtimeEnvConfig.googleTagEnabled) !== null && _j !== void 0 ? _j : '').trim() === 'true';
32
- export const GOOGLE_ANALYTICS_ID = ((_k = runtimeEnvConfig.googleAnalyticsId) !== null && _k !== void 0 ? _k : '').trim();
33
- export const GOOGLE_TAG_MANAGER_ID = ((_l = runtimeEnvConfig.googleTagManagerId) !== null && _l !== void 0 ? _l : '').trim();
34
- export const GOOGLE_TAG_EVENT_TARGET = ((_m = runtimeEnvConfig.googleTagEventTarget) !== null && _m !== void 0 ? _m : '').trim();
35
- export const STRIPE_ACTIVE_MODE = ((_o = runtimeEnvConfig.stripeActiveMode) !== null && _o !== void 0 ? _o : '').trim().toLowerCase();
44
+ export const GOOGLE_ANALYTICS_ID = trimEnv(runtimeEnvConfig.googleAnalyticsId);
45
+ export const GOOGLE_TAG_MANAGER_ID = trimEnv(runtimeEnvConfig.googleTagManagerId);
46
+ export const GOOGLE_TAG_EVENT_TARGET = trimEnv(runtimeEnvConfig.googleTagEventTarget);
47
+ export const GOOGLE_TAG_LEAD_SEND_TO = trimEnv(runtimeEnvConfig.googleTagLeadSendTo);
48
+ export const GOOGLE_TAG_PURCHASE_SEND_TO = trimEnv(runtimeEnvConfig.googleTagPurchaseSendTo);
49
+ export const GOOGLE_ADS_CONVERSION_ID = resolveGoogleAdsConversionId(GOOGLE_TAG_LEAD_SEND_TO, GOOGLE_TAG_PURCHASE_SEND_TO);
50
+ export const GOOGLE_TAG_CONFIG_IDS = Array.from(new Set([
51
+ GOOGLE_ANALYTICS_ID,
52
+ GOOGLE_ADS_CONVERSION_ID,
53
+ ].filter(Boolean)));
54
+ export const GOOGLE_TAG_SCRIPT_ID = (_j = GOOGLE_TAG_CONFIG_IDS[0]) !== null && _j !== void 0 ? _j : '';
55
+ export const GOOGLE_TAG_ENABLED = (trimEnv(runtimeEnvConfig.googleTagEnabled) === 'true' ||
56
+ Boolean(GOOGLE_TAG_LEAD_SEND_TO || GOOGLE_TAG_PURCHASE_SEND_TO));
57
+ export const STRIPE_ACTIVE_MODE = ((_k = runtimeEnvConfig.stripeActiveMode) !== null && _k !== void 0 ? _k : '').trim().toLowerCase();
36
58
  export const hasRealFunnelSdkPublishableKey = (value = FUNNEL_SDK_PUBLISHABLE_KEY) => {
37
59
  const normalized = value.trim();
38
60
  return Boolean(normalized) && normalized !== DEFAULT_FUNNEL_SDK_PUBLISHABLE_KEY;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.29",
3
+ "version": "0.1.31",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",
@@ -31,7 +31,7 @@
31
31
  "test:run": "vitest run --passWithNoTests"
32
32
  },
33
33
  "dependencies": {
34
- "@funnelsgrove/runtime": "0.1.45"
34
+ "@funnelsgrove/runtime": "0.1.51"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "^20",