@aranova/tracking-react 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
@@ -81,7 +81,9 @@ function createTrackingClientContext(surface, input = {}) {
81
81
  package_name: input.packageName ?? null,
82
82
  site_origin: input.siteOrigin ?? (typeof window === "undefined" ? null : window.location.origin),
83
83
  page_title: input.pageTitle ?? (typeof document === "undefined" ? null : document.title || null),
84
- referrer: input.referrer ?? (typeof document === "undefined" ? null : document.referrer || null)
84
+ referrer: input.referrer ?? (typeof document === "undefined" ? null : document.referrer || null),
85
+ environment: input.environment ?? null,
86
+ active_gtag_ids: input.activeGtagIds ?? null
85
87
  };
86
88
  }
87
89
  function createTrackingSessionUpsertPayload(trackingParams, input, context) {
@@ -118,6 +120,10 @@ var TRACKING_SCRIPT_ATTRIBUTE = "data-aranova-tracking";
118
120
  function getScriptMarker(id) {
119
121
  return `aranova-${id}`;
120
122
  }
123
+ var GTAG_ID_PATTERN = /^[A-Z]{1,3}-[A-Za-z0-9_-]+$/;
124
+ function isValidGtagId(id) {
125
+ return GTAG_ID_PATTERN.test(id);
126
+ }
121
127
  function ensureGtagFunction() {
122
128
  window.dataLayer = window.dataLayer || [];
123
129
  if (typeof window.gtag === "function")
@@ -158,11 +164,28 @@ function initializeGtag(gtagId) {
158
164
  function bootstrapGoogleAdsTracking(gtagId) {
159
165
  if (typeof window === "undefined" || typeof document === "undefined")
160
166
  return;
167
+ if (!isValidGtagId(gtagId))
168
+ return;
161
169
  applyDefaultConsentState();
162
170
  loadGtagScript(gtagId);
163
171
  initializeGtag(gtagId);
164
172
  restoreStoredConsent();
165
173
  }
174
+ function bootstrapMultipleGtags(gtagIds) {
175
+ if (typeof window === "undefined" || typeof document === "undefined")
176
+ return;
177
+ const ids = Object.values(gtagIds).filter(isValidGtagId);
178
+ if (ids.length === 0)
179
+ return;
180
+ applyDefaultConsentState();
181
+ loadGtagScript(ids[0]);
182
+ const gtag = ensureGtagFunction();
183
+ gtag("js", /* @__PURE__ */ new Date());
184
+ for (const id of ids) {
185
+ gtag("config", id);
186
+ }
187
+ restoreStoredConsent();
188
+ }
166
189
 
167
190
  // ../tracking-core/src/tracking.ts
168
191
  var TRACKING_COOKIE_MAX_AGE_SECONDS = 7776e3;
@@ -437,7 +460,7 @@ function serializeValue(value) {
437
460
  }
438
461
  return value;
439
462
  }
440
- function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers) {
463
+ function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers, gtagIds = null) {
441
464
  const automaticNames = triggers ? Object.keys(triggers.automatic) : [];
442
465
  const manualNames = triggers?.manual ? Object.keys(triggers.manual) : [];
443
466
  let triggerConfig = null;
@@ -470,7 +493,8 @@ function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers) {
470
493
  automatic: automaticNames,
471
494
  manual: manualNames
472
495
  },
473
- trigger_config: triggerConfig
496
+ trigger_config: triggerConfig,
497
+ configured_gtag_ids: gtagIds
474
498
  };
475
499
  }
476
500
 
@@ -479,14 +503,16 @@ var DEFAULT_FLUSH_INTERVAL_MS = 2e3;
479
503
  var DEFAULT_MAX_QUEUE_SIZE = 10;
480
504
  var HARD_MAX_BATCH = 50;
481
505
  var API_KEY_HEADER = "X-Aranova-Api-Key";
482
- function buildContext(surface, sdkVersion, packageName) {
506
+ function buildContext(surface, sdkVersion, packageName, environment, activeGtagIds) {
483
507
  return {
484
508
  surface,
485
509
  sdk_version: sdkVersion,
486
510
  package_name: packageName,
487
511
  site_origin: typeof window === "undefined" ? null : window.location.origin,
488
512
  page_title: typeof document === "undefined" ? null : document.title || null,
489
- referrer: typeof document === "undefined" ? null : document.referrer || null
513
+ referrer: typeof document === "undefined" ? null : document.referrer || null,
514
+ environment,
515
+ active_gtag_ids: activeGtagIds
490
516
  };
491
517
  }
492
518
  function readTrackingParams() {
@@ -546,6 +572,8 @@ function createTrackingClient(config) {
546
572
  const maxQueueSize = Math.min(config.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE, HARD_MAX_BATCH);
547
573
  const sdkVersion = config.sdkVersion ?? null;
548
574
  const packageName = config.packageName ?? null;
575
+ const environment = config.environment ?? null;
576
+ const activeGtagIds = config.activeGtagIds ?? null;
549
577
  const endpointBase = config.endpoint.replace(/\/$/, "");
550
578
  const eventsUrl = `${endpointBase}/events`;
551
579
  let queue = [];
@@ -562,7 +590,8 @@ function createTrackingClient(config) {
562
590
  config.surface,
563
591
  sdkVersion,
564
592
  packageName,
565
- config.triggers ?? null
593
+ config.triggers ?? null,
594
+ activeGtagIds
566
595
  );
567
596
  queue.push({
568
597
  event_type: "sdk_heartbeat",
@@ -581,7 +610,7 @@ function createTrackingClient(config) {
581
610
  }
582
611
  sessionId = rotated.id;
583
612
  const params = readTrackingParams();
584
- const context = buildContext(config.surface, sdkVersion, packageName);
613
+ const context = buildContext(config.surface, sdkVersion, packageName, environment, activeGtagIds);
585
614
  return {
586
615
  session_id: sessionId,
587
616
  visitor_id: visitorId,
@@ -703,7 +732,8 @@ var sdkHeartbeatMetadataSchema = import_zod3.z.object({
703
732
  package_name: import_zod3.z.string().nullable(),
704
733
  surface: import_zod3.z.enum(["next", "react", "script"]),
705
734
  triggers: sdkHeartbeatTriggersSchema,
706
- trigger_config: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown())).nullable().optional()
735
+ trigger_config: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown())).nullable().optional(),
736
+ configured_gtag_ids: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.string()).nullable().optional()
707
737
  }).strict();
708
738
  var sdkHeartbeatConfigSchema = import_zod3.z.object({}).strict();
709
739
 
@@ -1293,10 +1323,19 @@ function ConsentBanner() {
1293
1323
 
1294
1324
  // src/GoogleAdsTracking.tsx
1295
1325
  var import_react2 = require("react");
1296
- function GoogleAdsTracking({ gtagId }) {
1326
+ function GoogleAdsTracking(props) {
1327
+ const { gtagId, gtagIds } = props;
1328
+ const gtagIdsKey = (0, import_react2.useMemo)(
1329
+ () => gtagIds ? JSON.stringify(gtagIds) : "",
1330
+ [gtagIds]
1331
+ );
1297
1332
  (0, import_react2.useEffect)(() => {
1298
- bootstrapGoogleAdsTracking(gtagId);
1299
- }, [gtagId]);
1333
+ if (gtagIds && Object.keys(gtagIds).length > 0) {
1334
+ bootstrapMultipleGtags(gtagIds);
1335
+ } else if (gtagId) {
1336
+ bootstrapGoogleAdsTracking(gtagId);
1337
+ }
1338
+ }, [gtagId, gtagIdsKey]);
1300
1339
  return null;
1301
1340
  }
1302
1341
 
@@ -1336,7 +1375,7 @@ function useConsentState() {
1336
1375
  var import_react4 = require("react");
1337
1376
 
1338
1377
  // package.json
1339
- var version = "0.6.0";
1378
+ var version = "0.7.0";
1340
1379
 
1341
1380
  // src/factory.tsx
1342
1381
  var import_jsx_runtime2 = require("react/jsx-runtime");
@@ -1349,7 +1388,7 @@ var NOOP_CLIENT = {
1349
1388
  getVisitorId: () => ""
1350
1389
  };
1351
1390
  function createTracking(options) {
1352
- const { apiKey, endpoint, triggers, debug } = options;
1391
+ const { apiKey, endpoint, triggers, environment, debug } = options;
1353
1392
  if (!apiKey || !endpoint) {
1354
1393
  if (apiKey || endpoint) {
1355
1394
  console.warn(
@@ -1363,7 +1402,8 @@ function createTracking(options) {
1363
1402
  };
1364
1403
  }
1365
1404
  const TrackingContext = (0, import_react4.createContext)(null);
1366
- function TrackingProvider({ gtagId, children }) {
1405
+ function TrackingProvider({ gtagId, gtagIds, children }) {
1406
+ const resolvedGtagIds = gtagIds ?? (gtagId ? { default: gtagId } : void 0);
1367
1407
  const client = (0, import_react4.useMemo)(
1368
1408
  () => createTypedClient(
1369
1409
  getOrCreateTrackingClient({
@@ -1373,6 +1413,8 @@ function createTracking(options) {
1373
1413
  packageName: "@aranova/tracking-react",
1374
1414
  sdkVersion: version,
1375
1415
  triggers,
1416
+ environment,
1417
+ activeGtagIds: resolvedGtagIds,
1376
1418
  debug
1377
1419
  }),
1378
1420
  triggers,
@@ -1380,10 +1422,14 @@ function createTracking(options) {
1380
1422
  ),
1381
1423
  []
1382
1424
  );
1425
+ const gtagIdsKey = (0, import_react4.useMemo)(() => gtagIds ? JSON.stringify(gtagIds) : "", [gtagIds]);
1383
1426
  (0, import_react4.useEffect)(() => {
1384
- if (!gtagId) return;
1385
- bootstrapGoogleAdsTracking(gtagId);
1386
- }, [gtagId]);
1427
+ if (gtagIds && Object.keys(gtagIds).length > 0) {
1428
+ bootstrapMultipleGtags(gtagIds);
1429
+ } else if (gtagId) {
1430
+ bootstrapGoogleAdsTracking(gtagId);
1431
+ }
1432
+ }, [gtagId, gtagIdsKey]);
1387
1433
  (0, import_react4.useEffect)(() => {
1388
1434
  const detachers = [];
1389
1435
  const rawClient = getOrCreateTrackingClient({
@@ -1392,6 +1438,8 @@ function createTracking(options) {
1392
1438
  surface: "react",
1393
1439
  packageName: "@aranova/tracking-react",
1394
1440
  triggers,
1441
+ environment,
1442
+ activeGtagIds: resolvedGtagIds,
1395
1443
  debug
1396
1444
  });
1397
1445
  detachers.push(attachAutoPageView(rawClient));