@aranova/tracking-react 1.2.0 → 1.2.2

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/client.js CHANGED
@@ -1697,6 +1697,91 @@ function withConversionAutoFire(client, autoFire) {
1697
1697
  };
1698
1698
  }
1699
1699
 
1700
+ // ../tracking-core/src/events/semantics.ts
1701
+ var EVENT_SEMANTICS = {
1702
+ page_view: {
1703
+ label: "Page view",
1704
+ category: "page",
1705
+ outcomeRole: "navigation",
1706
+ clientVisibility: "simple"
1707
+ },
1708
+ time_on_site: {
1709
+ label: "Time on site",
1710
+ category: "engagement",
1711
+ outcomeRole: "engagement",
1712
+ clientVisibility: "detailed"
1713
+ },
1714
+ specific_page_visit: {
1715
+ label: "Key page visit",
1716
+ category: "engagement",
1717
+ outcomeRole: "engagement",
1718
+ clientVisibility: "detailed"
1719
+ },
1720
+ scroll_depth: {
1721
+ label: "Scroll depth",
1722
+ category: "engagement",
1723
+ outcomeRole: "engagement",
1724
+ clientVisibility: "detailed"
1725
+ },
1726
+ multi_page_session: {
1727
+ label: "Multi-page session",
1728
+ category: "engagement",
1729
+ outcomeRole: "engagement",
1730
+ clientVisibility: "detailed"
1731
+ },
1732
+ form_start: {
1733
+ label: "Form started",
1734
+ category: "engagement",
1735
+ outcomeRole: "engagement",
1736
+ clientVisibility: "simple"
1737
+ },
1738
+ sdk_heartbeat: {
1739
+ label: "SDK heartbeat",
1740
+ category: "system",
1741
+ outcomeRole: "diagnostic",
1742
+ clientVisibility: "hidden"
1743
+ },
1744
+ page_exit: {
1745
+ label: "Page exit",
1746
+ category: "engagement",
1747
+ outcomeRole: "diagnostic",
1748
+ clientVisibility: "detailed"
1749
+ },
1750
+ form_submit: {
1751
+ label: "Form submitted",
1752
+ category: "lead",
1753
+ outcomeRole: "lead",
1754
+ clientVisibility: "simple"
1755
+ },
1756
+ phone_click: {
1757
+ label: "Phone click",
1758
+ category: "lead",
1759
+ outcomeRole: "lead",
1760
+ clientVisibility: "simple"
1761
+ },
1762
+ cta_click: {
1763
+ label: "CTA click",
1764
+ category: "engagement",
1765
+ outcomeRole: "engagement",
1766
+ clientVisibility: "simple"
1767
+ },
1768
+ site_link_click: {
1769
+ label: "Left for sibling site",
1770
+ category: "page",
1771
+ outcomeRole: "navigation",
1772
+ clientVisibility: "detailed"
1773
+ },
1774
+ site_link_arrival: {
1775
+ label: "Arrived from sibling site",
1776
+ category: "page",
1777
+ outcomeRole: "navigation",
1778
+ clientVisibility: "simple"
1779
+ }
1780
+ };
1781
+ function getEventSemantics(eventName) {
1782
+ return eventName in EVENT_SEMANTICS ? EVENT_SEMANTICS[eventName] : null;
1783
+ }
1784
+
1700
1785
  // ../tracking-core/src/page-view.ts
1701
1786
  var import_mini = require("zod/mini");
1702
1787
 
@@ -1743,6 +1828,13 @@ function writeSessionStorage(key, value) {
1743
1828
  } catch {
1744
1829
  }
1745
1830
  }
1831
+ function deleteSessionStorage(key) {
1832
+ try {
1833
+ if (typeof window === "undefined") return;
1834
+ window.sessionStorage.removeItem(key);
1835
+ } catch {
1836
+ }
1837
+ }
1746
1838
  function getLastFiredUrl() {
1747
1839
  if (!lastFiredUrlHydrated) {
1748
1840
  lastFiredUrlHydrated = true;
@@ -1756,6 +1848,31 @@ function setLastFiredUrl(url) {
1756
1848
  lastFiredUrlHydrated = true;
1757
1849
  writeSessionStorage(LAST_FIRED_URL_STORAGE_KEY, url);
1758
1850
  }
1851
+ function resetPageViewState() {
1852
+ lastFiredUrl = null;
1853
+ lastFiredUrlHydrated = false;
1854
+ deleteSessionStorage(LAST_FIRED_URL_STORAGE_KEY);
1855
+ autoPageViewSlot().key = null;
1856
+ }
1857
+ var AUTO_PAGE_VIEW_SLOT = /* @__PURE__ */ Symbol.for("aranova.tracking.autoPageView");
1858
+ function autoPageViewSlot() {
1859
+ const scope = globalThis;
1860
+ scope[AUTO_PAGE_VIEW_SLOT] ?? (scope[AUTO_PAGE_VIEW_SLOT] = { key: null });
1861
+ return scope[AUTO_PAGE_VIEW_SLOT];
1862
+ }
1863
+ function pageViewRouteKey(pathname, search) {
1864
+ const query = stripLinkParamFromSearch(search);
1865
+ return query.length > 1 ? `${pathname}${query}` : pathname;
1866
+ }
1867
+ function fireAutoPageView(client, options = {}) {
1868
+ if (typeof window === "undefined") return false;
1869
+ const key = pageViewRouteKey(window.location.pathname, window.location.search);
1870
+ const slot = autoPageViewSlot();
1871
+ if (!options.force && slot.key === key) return false;
1872
+ slot.key = key;
1873
+ fireManualPageView(client);
1874
+ return true;
1875
+ }
1759
1876
  function buildPageViewMetadata(referrerOverride) {
1760
1877
  if (typeof window === "undefined" || typeof document === "undefined") return null;
1761
1878
  return (0, import_mini.parse)(pageViewMetadataSchema, {
@@ -1791,49 +1908,41 @@ function attachBfcacheRestore(client) {
1791
1908
  };
1792
1909
  function handlePageShow(event) {
1793
1910
  if (!event.persisted) return;
1794
- fireManualPageView(client);
1911
+ fireAutoPageView(client, { force: true });
1795
1912
  }
1796
1913
  window.addEventListener("pageshow", handlePageShow);
1797
1914
  return () => {
1798
1915
  window.removeEventListener("pageshow", handlePageShow);
1799
1916
  };
1800
1917
  }
1801
- function attachAutoPageView(client, options = {}) {
1918
+ function attachAutoPageView(client) {
1802
1919
  if (typeof window === "undefined" || typeof history === "undefined") {
1803
1920
  return () => {
1804
1921
  };
1805
1922
  }
1806
- let lastPath = window.location.pathname + window.location.search;
1807
- function maybeFire() {
1808
- const current = window.location.pathname + window.location.search;
1809
- if (current === lastPath) return;
1810
- lastPath = current;
1811
- fireManualPageView(client);
1923
+ function fireForCurrentRoute() {
1924
+ fireAutoPageView(client);
1812
1925
  }
1813
1926
  const originalPushState = history.pushState.bind(history);
1814
1927
  const originalReplaceState = history.replaceState.bind(history);
1815
1928
  function patchedPushState(...args) {
1816
1929
  originalPushState(...args);
1817
- setTimeout(maybeFire, 0);
1930
+ setTimeout(fireForCurrentRoute, 0);
1818
1931
  }
1819
1932
  function patchedReplaceState(...args) {
1820
1933
  originalReplaceState(...args);
1821
- setTimeout(maybeFire, 0);
1822
- }
1823
- function handlePageShow(event) {
1824
- if (!event.persisted) return;
1825
- fireManualPageView(client);
1934
+ setTimeout(fireForCurrentRoute, 0);
1826
1935
  }
1827
1936
  history.pushState = patchedPushState;
1828
1937
  history.replaceState = patchedReplaceState;
1829
- window.addEventListener("popstate", maybeFire);
1830
- window.addEventListener("pageshow", handlePageShow);
1831
- if (!options.skipInitial) fireManualPageView(client);
1938
+ window.addEventListener("popstate", fireForCurrentRoute);
1939
+ const detachBfcacheRestore = attachBfcacheRestore(client);
1940
+ fireForCurrentRoute();
1832
1941
  return () => {
1833
1942
  history.pushState = originalPushState;
1834
1943
  history.replaceState = originalReplaceState;
1835
- window.removeEventListener("popstate", maybeFire);
1836
- window.removeEventListener("pageshow", handlePageShow);
1944
+ window.removeEventListener("popstate", fireForCurrentRoute);
1945
+ detachBfcacheRestore();
1837
1946
  };
1838
1947
  }
1839
1948
 
@@ -2011,6 +2120,7 @@ function getOrCreateTrackingClient(config) {
2011
2120
  }
2012
2121
  if (globalClient !== null) {
2013
2122
  globalClient.destroy();
2123
+ resetPageViewState();
2014
2124
  }
2015
2125
  globalClient = createTrackingClient(config);
2016
2126
  globalClientKey = key;
@@ -2217,11 +2327,12 @@ function createTrackingClient(config) {
2217
2327
  identityHeaders,
2218
2328
  false
2219
2329
  );
2330
+ if (destroyed) return;
2220
2331
  if (outcome === "retry") {
2221
2332
  retryAttempt += 1;
2222
2333
  return;
2223
2334
  }
2224
- pending = pending.slice(1);
2335
+ pending = pending.filter((entry) => entry !== batch);
2225
2336
  writePendingBatches(bufferKey, pending);
2226
2337
  retryAttempt = 0;
2227
2338
  }
@@ -2252,13 +2363,14 @@ function createTrackingClient(config) {
2252
2363
  metadata: input.metadata ?? null,
2253
2364
  occurred_at: occurredAt
2254
2365
  });
2255
- if (queue.length >= maxQueueSize) {
2366
+ if (queue.length >= maxQueueSize || getEventSemantics(input.eventType)?.outcomeRole === "lead") {
2256
2367
  void flush();
2257
2368
  } else {
2258
2369
  scheduleFlush();
2259
2370
  }
2260
2371
  }
2261
2372
  function flushOnUnload() {
2373
+ if (destroyed) return;
2262
2374
  bufferQueued();
2263
2375
  clearScheduledFlush();
2264
2376
  if (pending.length === 0) return;
@@ -2268,11 +2380,12 @@ function createTrackingClient(config) {
2268
2380
  const body = { session: batch.session, events: batch.events };
2269
2381
  void postWithFetch(eventsUrl, JSON.stringify(body), config.apiKey, identityHeaders, true);
2270
2382
  }
2383
+ function onVisibilityChange() {
2384
+ if (document.visibilityState === "hidden") flushOnUnload();
2385
+ }
2271
2386
  if (typeof window !== "undefined") {
2272
2387
  window.addEventListener("pagehide", flushOnUnload);
2273
- window.addEventListener("visibilitychange", () => {
2274
- if (document.visibilityState === "hidden") flushOnUnload();
2275
- });
2388
+ window.addEventListener("visibilitychange", onVisibilityChange);
2276
2389
  }
2277
2390
  return {
2278
2391
  trackEvent,
@@ -2281,14 +2394,16 @@ function createTrackingClient(config) {
2281
2394
  getSessionId: () => sessionId,
2282
2395
  getVisitorId: () => visitorId,
2283
2396
  destroy: () => {
2284
- destroyed = true;
2397
+ if (destroyed) return;
2285
2398
  if (queue.length > 0) {
2286
2399
  flushOnUnload();
2287
2400
  }
2401
+ destroyed = true;
2288
2402
  clearScheduledFlush();
2289
2403
  queue = [];
2290
2404
  if (typeof window !== "undefined") {
2291
2405
  window.removeEventListener("pagehide", flushOnUnload);
2406
+ window.removeEventListener("visibilitychange", onVisibilityChange);
2292
2407
  }
2293
2408
  }
2294
2409
  };
@@ -2511,88 +2626,6 @@ var timeOnSiteConfigSchema = z13.strictObject({
2511
2626
  thresholdSeconds: z13.int().check(z13.positive())
2512
2627
  });
2513
2628
 
2514
- // ../tracking-core/src/events/semantics.ts
2515
- var EVENT_SEMANTICS = {
2516
- page_view: {
2517
- label: "Page view",
2518
- category: "page",
2519
- outcomeRole: "navigation",
2520
- clientVisibility: "simple"
2521
- },
2522
- time_on_site: {
2523
- label: "Time on site",
2524
- category: "engagement",
2525
- outcomeRole: "engagement",
2526
- clientVisibility: "detailed"
2527
- },
2528
- specific_page_visit: {
2529
- label: "Key page visit",
2530
- category: "engagement",
2531
- outcomeRole: "engagement",
2532
- clientVisibility: "detailed"
2533
- },
2534
- scroll_depth: {
2535
- label: "Scroll depth",
2536
- category: "engagement",
2537
- outcomeRole: "engagement",
2538
- clientVisibility: "detailed"
2539
- },
2540
- multi_page_session: {
2541
- label: "Multi-page session",
2542
- category: "engagement",
2543
- outcomeRole: "engagement",
2544
- clientVisibility: "detailed"
2545
- },
2546
- form_start: {
2547
- label: "Form started",
2548
- category: "engagement",
2549
- outcomeRole: "engagement",
2550
- clientVisibility: "simple"
2551
- },
2552
- sdk_heartbeat: {
2553
- label: "SDK heartbeat",
2554
- category: "system",
2555
- outcomeRole: "diagnostic",
2556
- clientVisibility: "hidden"
2557
- },
2558
- page_exit: {
2559
- label: "Page exit",
2560
- category: "engagement",
2561
- outcomeRole: "diagnostic",
2562
- clientVisibility: "detailed"
2563
- },
2564
- form_submit: {
2565
- label: "Form submitted",
2566
- category: "lead",
2567
- outcomeRole: "lead",
2568
- clientVisibility: "simple"
2569
- },
2570
- phone_click: {
2571
- label: "Phone click",
2572
- category: "lead",
2573
- outcomeRole: "lead",
2574
- clientVisibility: "simple"
2575
- },
2576
- cta_click: {
2577
- label: "CTA click",
2578
- category: "engagement",
2579
- outcomeRole: "engagement",
2580
- clientVisibility: "simple"
2581
- },
2582
- site_link_click: {
2583
- label: "Left for sibling site",
2584
- category: "page",
2585
- outcomeRole: "navigation",
2586
- clientVisibility: "detailed"
2587
- },
2588
- site_link_arrival: {
2589
- label: "Arrived from sibling site",
2590
- category: "page",
2591
- outcomeRole: "navigation",
2592
- clientVisibility: "simple"
2593
- }
2594
- };
2595
-
2596
2629
  // ../tracking-core/src/events/registry.ts
2597
2630
  var EVENT_REGISTRY = {
2598
2631
  // --- automatic triggers ---
@@ -3700,7 +3733,7 @@ function GoogleAdsTracking(props) {
3700
3733
  var import_react6 = require("react");
3701
3734
 
3702
3735
  // package.json
3703
- var version = "1.2.0";
3736
+ var version = "1.2.2";
3704
3737
 
3705
3738
  // ../tracking-core/src/phone-react.tsx
3706
3739
  var import_react5 = require("react");
@@ -3906,7 +3939,6 @@ function createTracking(options) {
3906
3939
  }
3907
3940
  } : detectorClient;
3908
3941
  detachers.push(attachAutoPageView(pageClient));
3909
- detachers.push(attachBfcacheRestore(pageClient));
3910
3942
  detachers.push(attachPageExit(detectorClient));
3911
3943
  const timeOnSite = triggers.automatic.time_on_site;
3912
3944
  if (timeOnSite) {