@aranova/tracking-react 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
@@ -43,7 +43,9 @@ function createTrackingClientContext(surface, input = {}) {
43
43
  package_name: input.packageName ?? null,
44
44
  site_origin: input.siteOrigin ?? (typeof window === "undefined" ? null : window.location.origin),
45
45
  page_title: input.pageTitle ?? (typeof document === "undefined" ? null : document.title || null),
46
- referrer: input.referrer ?? (typeof document === "undefined" ? null : document.referrer || null)
46
+ referrer: input.referrer ?? (typeof document === "undefined" ? null : document.referrer || null),
47
+ environment: input.environment ?? "production",
48
+ active_gtag_ids: input.activeGtagIds ?? null
47
49
  };
48
50
  }
49
51
  function createTrackingSessionUpsertPayload(trackingParams, input, context) {
@@ -80,6 +82,10 @@ var TRACKING_SCRIPT_ATTRIBUTE = "data-aranova-tracking";
80
82
  function getScriptMarker(id) {
81
83
  return `aranova-${id}`;
82
84
  }
85
+ var GTAG_ID_PATTERN = /^[A-Z]{1,3}-[A-Za-z0-9_-]+$/;
86
+ function isValidGtagId(id) {
87
+ return GTAG_ID_PATTERN.test(id);
88
+ }
83
89
  function ensureGtagFunction() {
84
90
  window.dataLayer = window.dataLayer || [];
85
91
  if (typeof window.gtag === "function")
@@ -120,11 +126,28 @@ function initializeGtag(gtagId) {
120
126
  function bootstrapGoogleAdsTracking(gtagId) {
121
127
  if (typeof window === "undefined" || typeof document === "undefined")
122
128
  return;
129
+ if (!isValidGtagId(gtagId))
130
+ return;
123
131
  applyDefaultConsentState();
124
132
  loadGtagScript(gtagId);
125
133
  initializeGtag(gtagId);
126
134
  restoreStoredConsent();
127
135
  }
136
+ function bootstrapMultipleGtags(gtagIds) {
137
+ if (typeof window === "undefined" || typeof document === "undefined")
138
+ return;
139
+ const ids = Object.values(gtagIds).filter(isValidGtagId);
140
+ if (ids.length === 0)
141
+ return;
142
+ applyDefaultConsentState();
143
+ loadGtagScript(ids[0]);
144
+ const gtag = ensureGtagFunction();
145
+ gtag("js", /* @__PURE__ */ new Date());
146
+ for (const id of ids) {
147
+ gtag("config", id);
148
+ }
149
+ restoreStoredConsent();
150
+ }
128
151
 
129
152
  // ../tracking-core/src/tracking.ts
130
153
  var TRACKING_COOKIE_MAX_AGE_SECONDS = 7776e3;
@@ -399,7 +422,7 @@ function serializeValue(value) {
399
422
  }
400
423
  return value;
401
424
  }
402
- function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers) {
425
+ function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers, gtagIds = null) {
403
426
  const automaticNames = triggers ? Object.keys(triggers.automatic) : [];
404
427
  const manualNames = triggers?.manual ? Object.keys(triggers.manual) : [];
405
428
  let triggerConfig = null;
@@ -432,7 +455,8 @@ function buildHeartbeatMetadata(surface, sdkVersion, packageName, triggers) {
432
455
  automatic: automaticNames,
433
456
  manual: manualNames
434
457
  },
435
- trigger_config: triggerConfig
458
+ trigger_config: triggerConfig,
459
+ configured_gtag_ids: gtagIds
436
460
  };
437
461
  }
438
462
 
@@ -441,14 +465,16 @@ var DEFAULT_FLUSH_INTERVAL_MS = 2e3;
441
465
  var DEFAULT_MAX_QUEUE_SIZE = 10;
442
466
  var HARD_MAX_BATCH = 50;
443
467
  var API_KEY_HEADER = "X-Aranova-Api-Key";
444
- function buildContext(surface, sdkVersion, packageName) {
468
+ function buildContext(surface, sdkVersion, packageName, environment, activeGtagIds) {
445
469
  return {
446
470
  surface,
447
471
  sdk_version: sdkVersion,
448
472
  package_name: packageName,
449
473
  site_origin: typeof window === "undefined" ? null : window.location.origin,
450
474
  page_title: typeof document === "undefined" ? null : document.title || null,
451
- referrer: typeof document === "undefined" ? null : document.referrer || null
475
+ referrer: typeof document === "undefined" ? null : document.referrer || null,
476
+ environment,
477
+ active_gtag_ids: activeGtagIds
452
478
  };
453
479
  }
454
480
  function readTrackingParams() {
@@ -508,6 +534,8 @@ function createTrackingClient(config) {
508
534
  const maxQueueSize = Math.min(config.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE, HARD_MAX_BATCH);
509
535
  const sdkVersion = config.sdkVersion ?? null;
510
536
  const packageName = config.packageName ?? null;
537
+ const environment = config.environment ?? "production";
538
+ const activeGtagIds = config.activeGtagIds ?? null;
511
539
  const endpointBase = config.endpoint.replace(/\/$/, "");
512
540
  const eventsUrl = `${endpointBase}/events`;
513
541
  let queue = [];
@@ -524,7 +552,8 @@ function createTrackingClient(config) {
524
552
  config.surface,
525
553
  sdkVersion,
526
554
  packageName,
527
- config.triggers ?? null
555
+ config.triggers ?? null,
556
+ activeGtagIds
528
557
  );
529
558
  queue.push({
530
559
  event_type: "sdk_heartbeat",
@@ -543,7 +572,7 @@ function createTrackingClient(config) {
543
572
  }
544
573
  sessionId = rotated.id;
545
574
  const params = readTrackingParams();
546
- const context = buildContext(config.surface, sdkVersion, packageName);
575
+ const context = buildContext(config.surface, sdkVersion, packageName, environment, activeGtagIds);
547
576
  return {
548
577
  session_id: sessionId,
549
578
  visitor_id: visitorId,
@@ -665,7 +694,8 @@ var sdkHeartbeatMetadataSchema = z3.object({
665
694
  package_name: z3.string().nullable(),
666
695
  surface: z3.enum(["next", "react", "script"]),
667
696
  triggers: sdkHeartbeatTriggersSchema,
668
- trigger_config: z3.record(z3.string(), z3.record(z3.string(), z3.unknown())).nullable().optional()
697
+ trigger_config: z3.record(z3.string(), z3.record(z3.string(), z3.unknown())).nullable().optional(),
698
+ configured_gtag_ids: z3.record(z3.string(), z3.string()).nullable().optional()
669
699
  }).strict();
670
700
  var sdkHeartbeatConfigSchema = z3.object({}).strict();
671
701
 
@@ -1254,11 +1284,20 @@ function ConsentBanner() {
1254
1284
  }
1255
1285
 
1256
1286
  // src/GoogleAdsTracking.tsx
1257
- import { useEffect as useEffect2 } from "react";
1258
- function GoogleAdsTracking({ gtagId }) {
1287
+ import { useEffect as useEffect2, useMemo } from "react";
1288
+ function GoogleAdsTracking(props) {
1289
+ const { gtagId, gtagIds } = props;
1290
+ const gtagIdsKey = useMemo(
1291
+ () => gtagIds ? JSON.stringify(gtagIds) : "",
1292
+ [gtagIds]
1293
+ );
1259
1294
  useEffect2(() => {
1260
- bootstrapGoogleAdsTracking(gtagId);
1261
- }, [gtagId]);
1295
+ if (gtagIds && Object.keys(gtagIds).length > 0) {
1296
+ bootstrapMultipleGtags(gtagIds);
1297
+ } else if (gtagId) {
1298
+ bootstrapGoogleAdsTracking(gtagId);
1299
+ }
1300
+ }, [gtagId, gtagIdsKey]);
1262
1301
  return null;
1263
1302
  }
1264
1303
 
@@ -1299,11 +1338,11 @@ import {
1299
1338
  createContext,
1300
1339
  useContext,
1301
1340
  useEffect as useEffect4,
1302
- useMemo
1341
+ useMemo as useMemo2
1303
1342
  } from "react";
1304
1343
 
1305
1344
  // package.json
1306
- var version = "0.6.1";
1345
+ var version = "0.7.1";
1307
1346
 
1308
1347
  // src/factory.tsx
1309
1348
  import { jsx as jsx2 } from "react/jsx-runtime";
@@ -1316,7 +1355,7 @@ var NOOP_CLIENT = {
1316
1355
  getVisitorId: () => ""
1317
1356
  };
1318
1357
  function createTracking(options) {
1319
- const { apiKey, endpoint, triggers, debug } = options;
1358
+ const { apiKey, endpoint, triggers, environment, debug } = options;
1320
1359
  if (!apiKey || !endpoint) {
1321
1360
  if (apiKey || endpoint) {
1322
1361
  console.warn(
@@ -1330,8 +1369,9 @@ function createTracking(options) {
1330
1369
  };
1331
1370
  }
1332
1371
  const TrackingContext = createContext(null);
1333
- function TrackingProvider({ gtagId, children }) {
1334
- const client = useMemo(
1372
+ function TrackingProvider({ gtagId, gtagIds, children }) {
1373
+ const resolvedGtagIds = gtagIds ?? (gtagId ? { default: gtagId } : void 0);
1374
+ const client = useMemo2(
1335
1375
  () => createTypedClient(
1336
1376
  getOrCreateTrackingClient({
1337
1377
  apiKey,
@@ -1340,6 +1380,8 @@ function createTracking(options) {
1340
1380
  packageName: "@aranova/tracking-react",
1341
1381
  sdkVersion: version,
1342
1382
  triggers,
1383
+ environment,
1384
+ activeGtagIds: resolvedGtagIds,
1343
1385
  debug
1344
1386
  }),
1345
1387
  triggers,
@@ -1347,10 +1389,14 @@ function createTracking(options) {
1347
1389
  ),
1348
1390
  []
1349
1391
  );
1392
+ const gtagIdsKey = useMemo2(() => gtagIds ? JSON.stringify(gtagIds) : "", [gtagIds]);
1350
1393
  useEffect4(() => {
1351
- if (!gtagId) return;
1352
- bootstrapGoogleAdsTracking(gtagId);
1353
- }, [gtagId]);
1394
+ if (gtagIds && Object.keys(gtagIds).length > 0) {
1395
+ bootstrapMultipleGtags(gtagIds);
1396
+ } else if (gtagId) {
1397
+ bootstrapGoogleAdsTracking(gtagId);
1398
+ }
1399
+ }, [gtagId, gtagIdsKey]);
1354
1400
  useEffect4(() => {
1355
1401
  const detachers = [];
1356
1402
  const rawClient = getOrCreateTrackingClient({
@@ -1359,6 +1405,8 @@ function createTracking(options) {
1359
1405
  surface: "react",
1360
1406
  packageName: "@aranova/tracking-react",
1361
1407
  triggers,
1408
+ environment,
1409
+ activeGtagIds: resolvedGtagIds,
1362
1410
  debug
1363
1411
  });
1364
1412
  detachers.push(attachAutoPageView(rawClient));