@aranova/tracking-next 0.6.1 → 0.7.1

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.mjs CHANGED
@@ -34,7 +34,9 @@ function createTrackingClientContext(surface, input = {}) {
34
34
  package_name: input.packageName ?? null,
35
35
  site_origin: input.siteOrigin ?? (typeof window === "undefined" ? null : window.location.origin),
36
36
  page_title: input.pageTitle ?? (typeof document === "undefined" ? null : document.title || null),
37
- referrer: input.referrer ?? (typeof document === "undefined" ? null : document.referrer || null)
37
+ referrer: input.referrer ?? (typeof document === "undefined" ? null : document.referrer || null),
38
+ environment: input.environment ?? "production",
39
+ active_gtag_ids: input.activeGtagIds ?? null
38
40
  };
39
41
  }
40
42
  function createTrackingSessionUpsertPayload(trackingParams, input, context) {
@@ -66,6 +68,10 @@ function createTrackingEventCreatePayload(trackingParams, input, context) {
66
68
  }
67
69
 
68
70
  // ../tracking-core/src/gtag.ts
71
+ var GTAG_ID_PATTERN = /^[A-Z]{1,3}-[A-Za-z0-9_-]+$/;
72
+ function isValidGtagId(id) {
73
+ return GTAG_ID_PATTERN.test(id);
74
+ }
69
75
  function createConsentDefaultScript() {
70
76
  return `
71
77
  window.dataLayer = window.dataLayer || [];
@@ -81,6 +87,7 @@ gtag('consent', 'default', {
81
87
  `.trim();
82
88
  }
83
89
  function createGtagInitScript(gtagId) {
90
+ if (!isValidGtagId(gtagId)) return "";
84
91
  return `
85
92
  window.dataLayer = window.dataLayer || [];
86
93
  function gtag(){dataLayer.push(arguments);}
@@ -102,6 +109,17 @@ if (window.gtag && (consentState === 'granted' || consentState === 'denied')) {
102
109
  }
103
110
  `.trim();
104
111
  }
112
+ function createGtagInitScriptMulti(gtagIds) {
113
+ const safeIds = gtagIds.filter(isValidGtagId);
114
+ const configs = safeIds.map((id) => `gtag('config', '${id}');`).join("\n");
115
+ return `
116
+ window.dataLayer = window.dataLayer || [];
117
+ function gtag(){dataLayer.push(arguments);}
118
+ window.gtag = gtag;
119
+ gtag('js', new Date());
120
+ ${configs}
121
+ `.trim();
122
+ }
105
123
 
106
124
  // ../tracking-core/src/tracking.ts
107
125
  var TRACKING_COOKIE_MAX_AGE_SECONDS = 7776e3;
@@ -336,7 +354,7 @@ function serializeValue(value) {
336
354
  }
337
355
  return value;
338
356
  }
339
- function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers) {
357
+ function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers, gtagIds = null) {
340
358
  const automaticNames = triggers ? Object.keys(triggers.automatic) : [];
341
359
  const manualNames = triggers?.manual ? Object.keys(triggers.manual) : [];
342
360
  let triggerConfig = null;
@@ -369,7 +387,8 @@ function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers) {
369
387
  automatic: automaticNames,
370
388
  manual: manualNames
371
389
  },
372
- trigger_config: triggerConfig
390
+ trigger_config: triggerConfig,
391
+ configured_gtag_ids: gtagIds
373
392
  };
374
393
  }
375
394
 
@@ -378,14 +397,16 @@ var DEFAULT_FLUSH_INTERVAL_MS = 2e3;
378
397
  var DEFAULT_MAX_QUEUE_SIZE = 10;
379
398
  var HARD_MAX_BATCH = 50;
380
399
  var API_KEY_HEADER = "X-Aranova-Api-Key";
381
- function buildContext(surface, sdkVersion, packageName) {
400
+ function buildContext(surface, sdkVersion, packageName, environment, activeGtagIds) {
382
401
  return {
383
402
  surface,
384
403
  sdk_version: sdkVersion,
385
404
  package_name: packageName,
386
405
  site_origin: typeof window === "undefined" ? null : window.location.origin,
387
406
  page_title: typeof document === "undefined" ? null : document.title || null,
388
- referrer: typeof document === "undefined" ? null : document.referrer || null
407
+ referrer: typeof document === "undefined" ? null : document.referrer || null,
408
+ environment,
409
+ active_gtag_ids: activeGtagIds
389
410
  };
390
411
  }
391
412
  function readTrackingParams() {
@@ -445,6 +466,8 @@ function createTrackingClient(config) {
445
466
  const maxQueueSize = Math.min(config.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE, HARD_MAX_BATCH);
446
467
  const sdkVersion = config.sdkVersion ?? null;
447
468
  const packageName = config.packageName ?? null;
469
+ const environment = config.environment ?? "production";
470
+ const activeGtagIds = config.activeGtagIds ?? null;
448
471
  const endpointBase = config.endpoint.replace(/\/$/, "");
449
472
  const eventsUrl = `${endpointBase}/events`;
450
473
  let queue = [];
@@ -461,7 +484,8 @@ function createTrackingClient(config) {
461
484
  config.surface,
462
485
  sdkVersion,
463
486
  packageName,
464
- config.triggers ?? null
487
+ config.triggers ?? null,
488
+ activeGtagIds
465
489
  );
466
490
  queue.push({
467
491
  event_type: "sdk_heartbeat",
@@ -480,7 +504,7 @@ function createTrackingClient(config) {
480
504
  }
481
505
  sessionId = rotated.id;
482
506
  const params = readTrackingParams();
483
- const context = buildContext(config.surface, sdkVersion, packageName);
507
+ const context = buildContext(config.surface, sdkVersion, packageName, environment, activeGtagIds);
484
508
  return {
485
509
  session_id: sessionId,
486
510
  visitor_id: visitorId,
@@ -602,7 +626,8 @@ var sdkHeartbeatMetadataSchema = z3.object({
602
626
  package_name: z3.string().nullable(),
603
627
  surface: z3.enum(["next", "react", "script"]),
604
628
  triggers: sdkHeartbeatTriggersSchema,
605
- trigger_config: z3.record(z3.string(), z3.record(z3.string(), z3.unknown())).nullable().optional()
629
+ trigger_config: z3.record(z3.string(), z3.record(z3.string(), z3.unknown())).nullable().optional(),
630
+ configured_gtag_ids: z3.record(z3.string(), z3.string()).nullable().optional()
606
631
  }).strict();
607
632
  var sdkHeartbeatConfigSchema = z3.object({}).strict();
608
633
 
@@ -1091,17 +1116,22 @@ function useConsentState() {
1091
1116
  // src/GoogleAdsTracking.tsx
1092
1117
  import Script from "next/script";
1093
1118
  import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
1094
- function GoogleAdsTracking({ gtagId }) {
1119
+ function GoogleAdsTracking(props) {
1120
+ const { gtagId, gtagIds } = props;
1121
+ const allIds = gtagIds ? Object.values(gtagIds) : gtagId ? [gtagId] : [];
1122
+ if (allIds.length === 0) return null;
1123
+ const loaderId = allIds[0];
1124
+ const initScript = allIds.length === 1 ? createGtagInitScript(allIds[0]) : createGtagInitScriptMulti(allIds);
1095
1125
  return /* @__PURE__ */ jsxs2(Fragment, { children: [
1096
1126
  /* @__PURE__ */ jsx2(Script, { id: "consent-default", strategy: "beforeInteractive", children: createConsentDefaultScript() }),
1097
1127
  /* @__PURE__ */ jsx2(
1098
1128
  Script,
1099
1129
  {
1100
- src: `https://www.googletagmanager.com/gtag/js?id=${gtagId}`,
1130
+ src: `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(loaderId)}`,
1101
1131
  strategy: "afterInteractive"
1102
1132
  }
1103
1133
  ),
1104
- /* @__PURE__ */ jsx2(Script, { id: "gtag-init", strategy: "afterInteractive", children: createGtagInitScript(gtagId) }),
1134
+ /* @__PURE__ */ jsx2(Script, { id: "gtag-init", strategy: "afterInteractive", children: initScript }),
1105
1135
  /* @__PURE__ */ jsx2(Script, { id: "consent-restore", strategy: "afterInteractive", children: createConsentRestoreScript() })
1106
1136
  ] });
1107
1137
  }
@@ -1110,7 +1140,7 @@ function GoogleAdsTracking({ gtagId }) {
1110
1140
  import { usePathname, useSearchParams } from "next/navigation";
1111
1141
 
1112
1142
  // package.json
1113
- var version = "0.6.1";
1143
+ var version = "0.7.1";
1114
1144
 
1115
1145
  // src/factory.tsx
1116
1146
  import {
@@ -1122,7 +1152,7 @@ import {
1122
1152
  } from "react";
1123
1153
  import { jsx as jsx3 } from "react/jsx-runtime";
1124
1154
  function createTracking(options) {
1125
- const { apiKey, endpoint, triggers, debug } = options;
1155
+ const { apiKey, endpoint, triggers, environment, gtagIds, debug } = options;
1126
1156
  if (!apiKey || !endpoint) {
1127
1157
  if (apiKey || endpoint) {
1128
1158
  console.warn(
@@ -1157,6 +1187,8 @@ function createTracking(options) {
1157
1187
  packageName: "@aranova/tracking-next",
1158
1188
  sdkVersion: version,
1159
1189
  triggers,
1190
+ environment,
1191
+ activeGtagIds: gtagIds,
1160
1192
  debug
1161
1193
  }),
1162
1194
  triggers,
@@ -1171,6 +1203,8 @@ function createTracking(options) {
1171
1203
  surface: "next",
1172
1204
  packageName: "@aranova/tracking-next",
1173
1205
  triggers,
1206
+ environment,
1207
+ activeGtagIds: gtagIds,
1174
1208
  debug
1175
1209
  });
1176
1210
  fireManualPageView(rawClient);
@@ -1184,6 +1218,8 @@ function createTracking(options) {
1184
1218
  surface: "next",
1185
1219
  packageName: "@aranova/tracking-next",
1186
1220
  triggers,
1221
+ environment,
1222
+ activeGtagIds: gtagIds,
1187
1223
  debug
1188
1224
  });
1189
1225
  for (const { name, pathPattern } of specificPageVisitConfig.pages) {
@@ -1206,6 +1242,8 @@ function createTracking(options) {
1206
1242
  surface: "next",
1207
1243
  packageName: "@aranova/tracking-next",
1208
1244
  triggers,
1245
+ environment,
1246
+ activeGtagIds: gtagIds,
1209
1247
  debug
1210
1248
  });
1211
1249
  return attachBfcacheRestore(rawClient);
@@ -1218,6 +1256,8 @@ function createTracking(options) {
1218
1256
  surface: "next",
1219
1257
  packageName: "@aranova/tracking-next",
1220
1258
  triggers,
1259
+ environment,
1260
+ activeGtagIds: gtagIds,
1221
1261
  debug
1222
1262
  });
1223
1263
  return attachTimeOnSite(rawClient, timeOnSiteConfig);
@@ -1252,7 +1292,10 @@ function createTracking(options) {
1252
1292
  endpoint,
1253
1293
  surface: "next",
1254
1294
  packageName: "@aranova/tracking-next",
1295
+ sdkVersion: version,
1255
1296
  triggers,
1297
+ environment,
1298
+ activeGtagIds: gtagIds,
1256
1299
  debug
1257
1300
  });
1258
1301
  rawClient.trackEvent({
@@ -1276,6 +1319,8 @@ function createTracking(options) {
1276
1319
  surface: "next",
1277
1320
  packageName: "@aranova/tracking-next",
1278
1321
  triggers,
1322
+ environment,
1323
+ activeGtagIds: gtagIds,
1279
1324
  debug
1280
1325
  });
1281
1326
  const scrollDepth = triggers.automatic.scroll_depth;