@aranova/tracking-next 0.6.0 → 0.7.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.
package/dist/index.js CHANGED
@@ -82,7 +82,9 @@ function createTrackingClientContext(surface, input = {}) {
82
82
  package_name: input.packageName ?? null,
83
83
  site_origin: input.siteOrigin ?? (typeof window === "undefined" ? null : window.location.origin),
84
84
  page_title: input.pageTitle ?? (typeof document === "undefined" ? null : document.title || null),
85
- referrer: input.referrer ?? (typeof document === "undefined" ? null : document.referrer || null)
85
+ referrer: input.referrer ?? (typeof document === "undefined" ? null : document.referrer || null),
86
+ environment: input.environment ?? null,
87
+ active_gtag_ids: input.activeGtagIds ?? null
86
88
  };
87
89
  }
88
90
  function createTrackingSessionUpsertPayload(trackingParams, input, context) {
@@ -114,6 +116,10 @@ function createTrackingEventCreatePayload(trackingParams, input, context) {
114
116
  }
115
117
 
116
118
  // ../tracking-core/src/gtag.ts
119
+ var GTAG_ID_PATTERN = /^[A-Z]{1,3}-[A-Za-z0-9_-]+$/;
120
+ function isValidGtagId(id) {
121
+ return GTAG_ID_PATTERN.test(id);
122
+ }
117
123
  function createConsentDefaultScript() {
118
124
  return `
119
125
  window.dataLayer = window.dataLayer || [];
@@ -129,6 +135,7 @@ gtag('consent', 'default', {
129
135
  `.trim();
130
136
  }
131
137
  function createGtagInitScript(gtagId) {
138
+ if (!isValidGtagId(gtagId)) return "";
132
139
  return `
133
140
  window.dataLayer = window.dataLayer || [];
134
141
  function gtag(){dataLayer.push(arguments);}
@@ -150,6 +157,17 @@ if (window.gtag && (consentState === 'granted' || consentState === 'denied')) {
150
157
  }
151
158
  `.trim();
152
159
  }
160
+ function createGtagInitScriptMulti(gtagIds) {
161
+ const safeIds = gtagIds.filter(isValidGtagId);
162
+ const configs = safeIds.map((id) => `gtag('config', '${id}');`).join("\n");
163
+ return `
164
+ window.dataLayer = window.dataLayer || [];
165
+ function gtag(){dataLayer.push(arguments);}
166
+ window.gtag = gtag;
167
+ gtag('js', new Date());
168
+ ${configs}
169
+ `.trim();
170
+ }
153
171
 
154
172
  // ../tracking-core/src/tracking.ts
155
173
  var TRACKING_COOKIE_MAX_AGE_SECONDS = 7776e3;
@@ -384,7 +402,7 @@ function serializeValue(value) {
384
402
  }
385
403
  return value;
386
404
  }
387
- function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers) {
405
+ function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers, gtagIds = null) {
388
406
  const automaticNames = triggers ? Object.keys(triggers.automatic) : [];
389
407
  const manualNames = triggers?.manual ? Object.keys(triggers.manual) : [];
390
408
  let triggerConfig = null;
@@ -417,7 +435,8 @@ function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers) {
417
435
  automatic: automaticNames,
418
436
  manual: manualNames
419
437
  },
420
- trigger_config: triggerConfig
438
+ trigger_config: triggerConfig,
439
+ configured_gtag_ids: gtagIds
421
440
  };
422
441
  }
423
442
 
@@ -426,14 +445,16 @@ var DEFAULT_FLUSH_INTERVAL_MS = 2e3;
426
445
  var DEFAULT_MAX_QUEUE_SIZE = 10;
427
446
  var HARD_MAX_BATCH = 50;
428
447
  var API_KEY_HEADER = "X-Aranova-Api-Key";
429
- function buildContext(surface, sdkVersion, packageName) {
448
+ function buildContext(surface, sdkVersion, packageName, environment, activeGtagIds) {
430
449
  return {
431
450
  surface,
432
451
  sdk_version: sdkVersion,
433
452
  package_name: packageName,
434
453
  site_origin: typeof window === "undefined" ? null : window.location.origin,
435
454
  page_title: typeof document === "undefined" ? null : document.title || null,
436
- referrer: typeof document === "undefined" ? null : document.referrer || null
455
+ referrer: typeof document === "undefined" ? null : document.referrer || null,
456
+ environment,
457
+ active_gtag_ids: activeGtagIds
437
458
  };
438
459
  }
439
460
  function readTrackingParams() {
@@ -493,6 +514,8 @@ function createTrackingClient(config) {
493
514
  const maxQueueSize = Math.min(config.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE, HARD_MAX_BATCH);
494
515
  const sdkVersion = config.sdkVersion ?? null;
495
516
  const packageName = config.packageName ?? null;
517
+ const environment = config.environment ?? null;
518
+ const activeGtagIds = config.activeGtagIds ?? null;
496
519
  const endpointBase = config.endpoint.replace(/\/$/, "");
497
520
  const eventsUrl = `${endpointBase}/events`;
498
521
  let queue = [];
@@ -509,7 +532,8 @@ function createTrackingClient(config) {
509
532
  config.surface,
510
533
  sdkVersion,
511
534
  packageName,
512
- config.triggers ?? null
535
+ config.triggers ?? null,
536
+ activeGtagIds
513
537
  );
514
538
  queue.push({
515
539
  event_type: "sdk_heartbeat",
@@ -528,7 +552,7 @@ function createTrackingClient(config) {
528
552
  }
529
553
  sessionId = rotated.id;
530
554
  const params = readTrackingParams();
531
- const context = buildContext(config.surface, sdkVersion, packageName);
555
+ const context = buildContext(config.surface, sdkVersion, packageName, environment, activeGtagIds);
532
556
  return {
533
557
  session_id: sessionId,
534
558
  visitor_id: visitorId,
@@ -650,7 +674,8 @@ var sdkHeartbeatMetadataSchema = import_zod3.z.object({
650
674
  package_name: import_zod3.z.string().nullable(),
651
675
  surface: import_zod3.z.enum(["next", "react", "script"]),
652
676
  triggers: sdkHeartbeatTriggersSchema,
653
- trigger_config: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown())).nullable().optional()
677
+ trigger_config: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown())).nullable().optional(),
678
+ configured_gtag_ids: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.string()).nullable().optional()
654
679
  }).strict();
655
680
  var sdkHeartbeatConfigSchema = import_zod3.z.object({}).strict();
656
681
 
@@ -1139,17 +1164,22 @@ function useConsentState() {
1139
1164
  // src/GoogleAdsTracking.tsx
1140
1165
  var import_script = __toESM(require("next/script"));
1141
1166
  var import_jsx_runtime2 = require("react/jsx-runtime");
1142
- function GoogleAdsTracking({ gtagId }) {
1167
+ function GoogleAdsTracking(props) {
1168
+ const { gtagId, gtagIds } = props;
1169
+ const allIds = gtagIds ? Object.values(gtagIds) : gtagId ? [gtagId] : [];
1170
+ if (allIds.length === 0) return null;
1171
+ const loaderId = allIds[0];
1172
+ const initScript = allIds.length === 1 ? createGtagInitScript(allIds[0]) : createGtagInitScriptMulti(allIds);
1143
1173
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
1144
1174
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_script.default, { id: "consent-default", strategy: "beforeInteractive", children: createConsentDefaultScript() }),
1145
1175
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
1146
1176
  import_script.default,
1147
1177
  {
1148
- src: `https://www.googletagmanager.com/gtag/js?id=${gtagId}`,
1178
+ src: `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(loaderId)}`,
1149
1179
  strategy: "afterInteractive"
1150
1180
  }
1151
1181
  ),
1152
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_script.default, { id: "gtag-init", strategy: "afterInteractive", children: createGtagInitScript(gtagId) }),
1182
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_script.default, { id: "gtag-init", strategy: "afterInteractive", children: initScript }),
1153
1183
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_script.default, { id: "consent-restore", strategy: "afterInteractive", children: createConsentRestoreScript() })
1154
1184
  ] });
1155
1185
  }
@@ -1158,13 +1188,13 @@ function GoogleAdsTracking({ gtagId }) {
1158
1188
  var import_navigation = require("next/navigation");
1159
1189
 
1160
1190
  // package.json
1161
- var version = "0.6.0";
1191
+ var version = "0.7.0";
1162
1192
 
1163
1193
  // src/factory.tsx
1164
1194
  var import_react3 = require("react");
1165
1195
  var import_jsx_runtime3 = require("react/jsx-runtime");
1166
1196
  function createTracking(options) {
1167
- const { apiKey, endpoint, triggers, debug } = options;
1197
+ const { apiKey, endpoint, triggers, environment, gtagIds, debug } = options;
1168
1198
  if (!apiKey || !endpoint) {
1169
1199
  if (apiKey || endpoint) {
1170
1200
  console.warn(
@@ -1199,6 +1229,8 @@ function createTracking(options) {
1199
1229
  packageName: "@aranova/tracking-next",
1200
1230
  sdkVersion: version,
1201
1231
  triggers,
1232
+ environment,
1233
+ activeGtagIds: gtagIds,
1202
1234
  debug
1203
1235
  }),
1204
1236
  triggers,
@@ -1213,6 +1245,8 @@ function createTracking(options) {
1213
1245
  surface: "next",
1214
1246
  packageName: "@aranova/tracking-next",
1215
1247
  triggers,
1248
+ environment,
1249
+ activeGtagIds: gtagIds,
1216
1250
  debug
1217
1251
  });
1218
1252
  fireManualPageView(rawClient);
@@ -1226,6 +1260,8 @@ function createTracking(options) {
1226
1260
  surface: "next",
1227
1261
  packageName: "@aranova/tracking-next",
1228
1262
  triggers,
1263
+ environment,
1264
+ activeGtagIds: gtagIds,
1229
1265
  debug
1230
1266
  });
1231
1267
  for (const { name, pathPattern } of specificPageVisitConfig.pages) {
@@ -1248,6 +1284,8 @@ function createTracking(options) {
1248
1284
  surface: "next",
1249
1285
  packageName: "@aranova/tracking-next",
1250
1286
  triggers,
1287
+ environment,
1288
+ activeGtagIds: gtagIds,
1251
1289
  debug
1252
1290
  });
1253
1291
  return attachBfcacheRestore(rawClient);
@@ -1260,6 +1298,8 @@ function createTracking(options) {
1260
1298
  surface: "next",
1261
1299
  packageName: "@aranova/tracking-next",
1262
1300
  triggers,
1301
+ environment,
1302
+ activeGtagIds: gtagIds,
1263
1303
  debug
1264
1304
  });
1265
1305
  return attachTimeOnSite(rawClient, timeOnSiteConfig);
@@ -1294,7 +1334,10 @@ function createTracking(options) {
1294
1334
  endpoint,
1295
1335
  surface: "next",
1296
1336
  packageName: "@aranova/tracking-next",
1337
+ sdkVersion: version,
1297
1338
  triggers,
1339
+ environment,
1340
+ activeGtagIds: gtagIds,
1298
1341
  debug
1299
1342
  });
1300
1343
  rawClient.trackEvent({
@@ -1318,6 +1361,8 @@ function createTracking(options) {
1318
1361
  surface: "next",
1319
1362
  packageName: "@aranova/tracking-next",
1320
1363
  triggers,
1364
+ environment,
1365
+ activeGtagIds: gtagIds,
1321
1366
  debug
1322
1367
  });
1323
1368
  const scrollDepth = triggers.automatic.scroll_depth;