@funnelsgrove/analytics 0.30.0 → 0.32.0

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,5 +1,5 @@
1
1
  import { analyticsUrlSanitizerSource, sanitizeAnalyticsExport, sanitizeAnalyticsUrl } from './analytics-export.js';
2
- import { FUNNEL_ID, FUNNEL_VERSION_ID, GOOGLE_TAG_CONFIG_IDS, GOOGLE_TAG_ENABLED, GOOGLE_TAG_LEAD_VALUE, GOOGLE_TAG_LEAD_SEND_TO, GOOGLE_TAG_MANAGER_ID, GOOGLE_TAG_PURCHASE_SEND_TO, GOOGLE_TAG_SCRIPT_ID, PROJECT_ID, STRIPE_ACTIVE_MODE, } from './runtime-api.config.js';
2
+ import { FUNNEL_ID, FUNNEL_VERSION_ID, GOOGLE_ANALYTICS_ID, GOOGLE_TAG_CONFIG_IDS, GOOGLE_TAG_ENABLED, GOOGLE_TAG_LEAD_VALUE, GOOGLE_TAG_LEAD_SEND_TO, GOOGLE_TAG_MANAGER_ID, GOOGLE_TAG_PURCHASE_SEND_TO, GOOGLE_TAG_SCRIPT_ID, PROJECT_ID, STRIPE_ACTIVE_MODE, } from './runtime-api.config.js';
3
3
  import { isNonLiveAnalyticsRuntime, nonLiveBrowserLocationSource } from './non-live-runtime.js';
4
4
  import { readExperimentExposureProperties } from '@funnelsgrove/runtime';
5
5
  import { buildBrowserPurchaseAnalyticsAttributes, buildPurchaseAnalyticsAttributes, currentBrowserPurchaseUrl, } from './purchase-attributes.service.js';
@@ -63,6 +63,27 @@ const buildCommerceParameters = (event, options = {}) => {
63
63
  const buildGoogleAdsConversionParameters = (parameters, sendTo) => {
64
64
  return sanitizeGoogleTagParameters(buildDefinedRecord(Object.assign(Object.assign({}, parameters), { send_to: sendTo })));
65
65
  };
66
+ // GA4's automatic events (first_visit, session_start, scroll) carry only what the tag config holds.
67
+ // Inlined into the head bootstrap, so it must stay self-contained.
68
+ const readGoogleTagFunnelParameters = (funnelId, funnelVersionId) => {
69
+ var _a, _b, _c, _d;
70
+ const parameters = {};
71
+ if (funnelId)
72
+ parameters.funnel_id = funnelId;
73
+ if (funnelVersionId)
74
+ parameters.funnel_version_id = funnelVersionId;
75
+ const marker = typeof document === 'undefined'
76
+ ? null
77
+ : (_a = document.querySelector) === null || _a === void 0 ? void 0 : _a.call(document, 'meta[name="fg-cross-funnel-assignment"]');
78
+ const experimentId = (_b = marker === null || marker === void 0 ? void 0 : marker.getAttribute('data-experiment-id')) === null || _b === void 0 ? void 0 : _b.trim();
79
+ const visitorId = (_c = marker === null || marker === void 0 ? void 0 : marker.getAttribute('data-visitor-id')) === null || _c === void 0 ? void 0 : _c.trim();
80
+ const variantKey = (_d = marker === null || marker === void 0 ? void 0 : marker.getAttribute('data-variant-key')) === null || _d === void 0 ? void 0 : _d.trim();
81
+ if (experimentId && visitorId && variantKey) {
82
+ parameters.cross_funnel_experiment_id = experimentId;
83
+ parameters.cross_funnel_variant_key = variantKey;
84
+ }
85
+ return parameters;
86
+ };
66
87
  export const buildGoogleTagHeadScripts = () => {
67
88
  if (!GOOGLE_TAG_ENABLED || (!GOOGLE_TAG_SCRIPT_ID && !GOOGLE_TAG_MANAGER_ID) || STRIPE_ACTIVE_MODE === 'test') {
68
89
  return [];
@@ -83,15 +104,17 @@ export const buildGoogleTagHeadScripts = () => {
83
104
  `window.__funnelsgroveSanitizeGoogleTagUrl = ${analyticsUrlSanitizerSource};`,
84
105
  'window.__funnelsgroveGoogleTagPageLocation = window.__funnelsgroveSanitizeGoogleTagUrl(window.location.href);',
85
106
  'window.__funnelsgroveGoogleTagHeadReady = true;',
107
+ `const funnelParameters = (${readGoogleTagFunnelParameters.toString()})(${JSON.stringify(FUNNEL_ID)}, ${JSON.stringify(FUNNEL_VERSION_ID)});`,
86
108
  ...(GOOGLE_TAG_CONFIG_IDS.length > 0
87
109
  ? [
88
110
  'window.gtag("js", new Date());',
89
- ...GOOGLE_TAG_CONFIG_IDS.map((googleTagConfigId) => `window.gtag("config", ${JSON.stringify(googleTagConfigId)}, { page_location: window.__funnelsgroveGoogleTagPageLocation, send_page_view: false });`),
111
+ ...GOOGLE_TAG_CONFIG_IDS.map((googleTagConfigId) => `window.gtag("config", ${JSON.stringify(googleTagConfigId)}, Object.assign({ page_location: window.__funnelsgroveGoogleTagPageLocation, send_page_view: false }${googleTagConfigId === GOOGLE_ANALYTICS_ID ? ', funnelParameters' : ''}));`),
90
112
  ]
91
113
  : []),
92
114
  ...(GOOGLE_TAG_MANAGER_ID
93
115
  ? [
94
116
  '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 = []; }',
117
+ 'window.dataLayer.push(funnelParameters);',
95
118
  'window.dataLayer.push({"gtm.start": Date.now(), event: "gtm.js"});',
96
119
  ]
97
120
  : []),
@@ -229,7 +252,7 @@ export class GoogleTagService {
229
252
  typeof window !== 'undefined');
230
253
  }
231
254
  ensureInitialized() {
232
- var _a, _b, _c, _d;
255
+ var _a, _b, _c, _d, _e;
233
256
  if (this.initialized) {
234
257
  return;
235
258
  }
@@ -239,18 +262,17 @@ export class GoogleTagService {
239
262
  if (window.__funnelsgroveGoogleTagHeadReady) {
240
263
  return;
241
264
  }
265
+ const funnelParameters = readGoogleTagFunnelParameters(FUNNEL_ID, FUNNEL_VERSION_ID);
242
266
  if (GOOGLE_TAG_SCRIPT_ID) {
243
267
  this.installScript(buildGoogleAnalyticsScriptSrc(GOOGLE_TAG_SCRIPT_ID));
244
268
  (_a = window.gtag) === null || _a === void 0 ? void 0 : _a.call(window, 'js', new Date());
245
269
  for (const googleTagConfigId of GOOGLE_TAG_CONFIG_IDS) {
246
- (_b = window.gtag) === null || _b === void 0 ? void 0 : _b.call(window, 'config', googleTagConfigId, {
247
- page_location: sanitizeAnalyticsUrl((_c = window.location) === null || _c === void 0 ? void 0 : _c.href),
248
- send_page_view: false,
249
- });
270
+ (_b = window.gtag) === null || _b === void 0 ? void 0 : _b.call(window, 'config', googleTagConfigId, Object.assign({ page_location: sanitizeAnalyticsUrl((_c = window.location) === null || _c === void 0 ? void 0 : _c.href), send_page_view: false }, (googleTagConfigId === GOOGLE_ANALYTICS_ID ? funnelParameters : {})));
250
271
  }
251
272
  }
252
273
  if (GOOGLE_TAG_MANAGER_ID) {
253
- (_d = window.dataLayer) === null || _d === void 0 ? void 0 : _d.push({
274
+ (_d = window.dataLayer) === null || _d === void 0 ? void 0 : _d.push(funnelParameters);
275
+ (_e = window.dataLayer) === null || _e === void 0 ? void 0 : _e.push({
254
276
  'gtm.start': Date.now(),
255
277
  event: 'gtm.js',
256
278
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.30.0",
3
+ "version": "0.32.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/The-Solid-Grove/funnelsgrove.git",
@@ -45,9 +45,9 @@
45
45
  "@types/node": "^20",
46
46
  "typescript": "^5",
47
47
  "vitest": "^3.2.4",
48
- "@funnelsgrove/runtime": "0.47.0"
48
+ "@funnelsgrove/runtime": "0.49.0"
49
49
  },
50
50
  "peerDependencies": {
51
- "@funnelsgrove/runtime": "^0.47.0"
51
+ "@funnelsgrove/runtime": "^0.49.0"
52
52
  }
53
53
  }