@aranova/tracking-react 1.2.1 → 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.mjs CHANGED
@@ -1651,6 +1651,91 @@ function withConversionAutoFire(client, autoFire) {
1651
1651
  };
1652
1652
  }
1653
1653
 
1654
+ // ../tracking-core/src/events/semantics.ts
1655
+ var EVENT_SEMANTICS = {
1656
+ page_view: {
1657
+ label: "Page view",
1658
+ category: "page",
1659
+ outcomeRole: "navigation",
1660
+ clientVisibility: "simple"
1661
+ },
1662
+ time_on_site: {
1663
+ label: "Time on site",
1664
+ category: "engagement",
1665
+ outcomeRole: "engagement",
1666
+ clientVisibility: "detailed"
1667
+ },
1668
+ specific_page_visit: {
1669
+ label: "Key page visit",
1670
+ category: "engagement",
1671
+ outcomeRole: "engagement",
1672
+ clientVisibility: "detailed"
1673
+ },
1674
+ scroll_depth: {
1675
+ label: "Scroll depth",
1676
+ category: "engagement",
1677
+ outcomeRole: "engagement",
1678
+ clientVisibility: "detailed"
1679
+ },
1680
+ multi_page_session: {
1681
+ label: "Multi-page session",
1682
+ category: "engagement",
1683
+ outcomeRole: "engagement",
1684
+ clientVisibility: "detailed"
1685
+ },
1686
+ form_start: {
1687
+ label: "Form started",
1688
+ category: "engagement",
1689
+ outcomeRole: "engagement",
1690
+ clientVisibility: "simple"
1691
+ },
1692
+ sdk_heartbeat: {
1693
+ label: "SDK heartbeat",
1694
+ category: "system",
1695
+ outcomeRole: "diagnostic",
1696
+ clientVisibility: "hidden"
1697
+ },
1698
+ page_exit: {
1699
+ label: "Page exit",
1700
+ category: "engagement",
1701
+ outcomeRole: "diagnostic",
1702
+ clientVisibility: "detailed"
1703
+ },
1704
+ form_submit: {
1705
+ label: "Form submitted",
1706
+ category: "lead",
1707
+ outcomeRole: "lead",
1708
+ clientVisibility: "simple"
1709
+ },
1710
+ phone_click: {
1711
+ label: "Phone click",
1712
+ category: "lead",
1713
+ outcomeRole: "lead",
1714
+ clientVisibility: "simple"
1715
+ },
1716
+ cta_click: {
1717
+ label: "CTA click",
1718
+ category: "engagement",
1719
+ outcomeRole: "engagement",
1720
+ clientVisibility: "simple"
1721
+ },
1722
+ site_link_click: {
1723
+ label: "Left for sibling site",
1724
+ category: "page",
1725
+ outcomeRole: "navigation",
1726
+ clientVisibility: "detailed"
1727
+ },
1728
+ site_link_arrival: {
1729
+ label: "Arrived from sibling site",
1730
+ category: "page",
1731
+ outcomeRole: "navigation",
1732
+ clientVisibility: "simple"
1733
+ }
1734
+ };
1735
+ function getEventSemantics(eventName) {
1736
+ return eventName in EVENT_SEMANTICS ? EVENT_SEMANTICS[eventName] : null;
1737
+ }
1738
+
1654
1739
  // ../tracking-core/src/page-view.ts
1655
1740
  import { parse } from "zod/mini";
1656
1741
 
@@ -1697,6 +1782,13 @@ function writeSessionStorage(key, value) {
1697
1782
  } catch {
1698
1783
  }
1699
1784
  }
1785
+ function deleteSessionStorage(key) {
1786
+ try {
1787
+ if (typeof window === "undefined") return;
1788
+ window.sessionStorage.removeItem(key);
1789
+ } catch {
1790
+ }
1791
+ }
1700
1792
  function getLastFiredUrl() {
1701
1793
  if (!lastFiredUrlHydrated) {
1702
1794
  lastFiredUrlHydrated = true;
@@ -1710,6 +1802,31 @@ function setLastFiredUrl(url) {
1710
1802
  lastFiredUrlHydrated = true;
1711
1803
  writeSessionStorage(LAST_FIRED_URL_STORAGE_KEY, url);
1712
1804
  }
1805
+ function resetPageViewState() {
1806
+ lastFiredUrl = null;
1807
+ lastFiredUrlHydrated = false;
1808
+ deleteSessionStorage(LAST_FIRED_URL_STORAGE_KEY);
1809
+ autoPageViewSlot().key = null;
1810
+ }
1811
+ var AUTO_PAGE_VIEW_SLOT = /* @__PURE__ */ Symbol.for("aranova.tracking.autoPageView");
1812
+ function autoPageViewSlot() {
1813
+ const scope = globalThis;
1814
+ scope[AUTO_PAGE_VIEW_SLOT] ?? (scope[AUTO_PAGE_VIEW_SLOT] = { key: null });
1815
+ return scope[AUTO_PAGE_VIEW_SLOT];
1816
+ }
1817
+ function pageViewRouteKey(pathname, search) {
1818
+ const query = stripLinkParamFromSearch(search);
1819
+ return query.length > 1 ? `${pathname}${query}` : pathname;
1820
+ }
1821
+ function fireAutoPageView(client, options = {}) {
1822
+ if (typeof window === "undefined") return false;
1823
+ const key = pageViewRouteKey(window.location.pathname, window.location.search);
1824
+ const slot = autoPageViewSlot();
1825
+ if (!options.force && slot.key === key) return false;
1826
+ slot.key = key;
1827
+ fireManualPageView(client);
1828
+ return true;
1829
+ }
1713
1830
  function buildPageViewMetadata(referrerOverride) {
1714
1831
  if (typeof window === "undefined" || typeof document === "undefined") return null;
1715
1832
  return parse(pageViewMetadataSchema, {
@@ -1745,49 +1862,41 @@ function attachBfcacheRestore(client) {
1745
1862
  };
1746
1863
  function handlePageShow(event) {
1747
1864
  if (!event.persisted) return;
1748
- fireManualPageView(client);
1865
+ fireAutoPageView(client, { force: true });
1749
1866
  }
1750
1867
  window.addEventListener("pageshow", handlePageShow);
1751
1868
  return () => {
1752
1869
  window.removeEventListener("pageshow", handlePageShow);
1753
1870
  };
1754
1871
  }
1755
- function attachAutoPageView(client, options = {}) {
1872
+ function attachAutoPageView(client) {
1756
1873
  if (typeof window === "undefined" || typeof history === "undefined") {
1757
1874
  return () => {
1758
1875
  };
1759
1876
  }
1760
- let lastPath = window.location.pathname + window.location.search;
1761
- function maybeFire() {
1762
- const current = window.location.pathname + window.location.search;
1763
- if (current === lastPath) return;
1764
- lastPath = current;
1765
- fireManualPageView(client);
1877
+ function fireForCurrentRoute() {
1878
+ fireAutoPageView(client);
1766
1879
  }
1767
1880
  const originalPushState = history.pushState.bind(history);
1768
1881
  const originalReplaceState = history.replaceState.bind(history);
1769
1882
  function patchedPushState(...args) {
1770
1883
  originalPushState(...args);
1771
- setTimeout(maybeFire, 0);
1884
+ setTimeout(fireForCurrentRoute, 0);
1772
1885
  }
1773
1886
  function patchedReplaceState(...args) {
1774
1887
  originalReplaceState(...args);
1775
- setTimeout(maybeFire, 0);
1776
- }
1777
- function handlePageShow(event) {
1778
- if (!event.persisted) return;
1779
- fireManualPageView(client);
1888
+ setTimeout(fireForCurrentRoute, 0);
1780
1889
  }
1781
1890
  history.pushState = patchedPushState;
1782
1891
  history.replaceState = patchedReplaceState;
1783
- window.addEventListener("popstate", maybeFire);
1784
- window.addEventListener("pageshow", handlePageShow);
1785
- if (!options.skipInitial) fireManualPageView(client);
1892
+ window.addEventListener("popstate", fireForCurrentRoute);
1893
+ const detachBfcacheRestore = attachBfcacheRestore(client);
1894
+ fireForCurrentRoute();
1786
1895
  return () => {
1787
1896
  history.pushState = originalPushState;
1788
1897
  history.replaceState = originalReplaceState;
1789
- window.removeEventListener("popstate", maybeFire);
1790
- window.removeEventListener("pageshow", handlePageShow);
1898
+ window.removeEventListener("popstate", fireForCurrentRoute);
1899
+ detachBfcacheRestore();
1791
1900
  };
1792
1901
  }
1793
1902
 
@@ -1965,6 +2074,7 @@ function getOrCreateTrackingClient(config) {
1965
2074
  }
1966
2075
  if (globalClient !== null) {
1967
2076
  globalClient.destroy();
2077
+ resetPageViewState();
1968
2078
  }
1969
2079
  globalClient = createTrackingClient(config);
1970
2080
  globalClientKey = key;
@@ -2171,11 +2281,12 @@ function createTrackingClient(config) {
2171
2281
  identityHeaders,
2172
2282
  false
2173
2283
  );
2284
+ if (destroyed) return;
2174
2285
  if (outcome === "retry") {
2175
2286
  retryAttempt += 1;
2176
2287
  return;
2177
2288
  }
2178
- pending = pending.slice(1);
2289
+ pending = pending.filter((entry) => entry !== batch);
2179
2290
  writePendingBatches(bufferKey, pending);
2180
2291
  retryAttempt = 0;
2181
2292
  }
@@ -2206,13 +2317,14 @@ function createTrackingClient(config) {
2206
2317
  metadata: input.metadata ?? null,
2207
2318
  occurred_at: occurredAt
2208
2319
  });
2209
- if (queue.length >= maxQueueSize) {
2320
+ if (queue.length >= maxQueueSize || getEventSemantics(input.eventType)?.outcomeRole === "lead") {
2210
2321
  void flush();
2211
2322
  } else {
2212
2323
  scheduleFlush();
2213
2324
  }
2214
2325
  }
2215
2326
  function flushOnUnload() {
2327
+ if (destroyed) return;
2216
2328
  bufferQueued();
2217
2329
  clearScheduledFlush();
2218
2330
  if (pending.length === 0) return;
@@ -2222,11 +2334,12 @@ function createTrackingClient(config) {
2222
2334
  const body = { session: batch.session, events: batch.events };
2223
2335
  void postWithFetch(eventsUrl, JSON.stringify(body), config.apiKey, identityHeaders, true);
2224
2336
  }
2337
+ function onVisibilityChange() {
2338
+ if (document.visibilityState === "hidden") flushOnUnload();
2339
+ }
2225
2340
  if (typeof window !== "undefined") {
2226
2341
  window.addEventListener("pagehide", flushOnUnload);
2227
- window.addEventListener("visibilitychange", () => {
2228
- if (document.visibilityState === "hidden") flushOnUnload();
2229
- });
2342
+ window.addEventListener("visibilitychange", onVisibilityChange);
2230
2343
  }
2231
2344
  return {
2232
2345
  trackEvent,
@@ -2235,14 +2348,16 @@ function createTrackingClient(config) {
2235
2348
  getSessionId: () => sessionId,
2236
2349
  getVisitorId: () => visitorId,
2237
2350
  destroy: () => {
2238
- destroyed = true;
2351
+ if (destroyed) return;
2239
2352
  if (queue.length > 0) {
2240
2353
  flushOnUnload();
2241
2354
  }
2355
+ destroyed = true;
2242
2356
  clearScheduledFlush();
2243
2357
  queue = [];
2244
2358
  if (typeof window !== "undefined") {
2245
2359
  window.removeEventListener("pagehide", flushOnUnload);
2360
+ window.removeEventListener("visibilitychange", onVisibilityChange);
2246
2361
  }
2247
2362
  }
2248
2363
  };
@@ -2465,88 +2580,6 @@ var timeOnSiteConfigSchema = z13.strictObject({
2465
2580
  thresholdSeconds: z13.int().check(z13.positive())
2466
2581
  });
2467
2582
 
2468
- // ../tracking-core/src/events/semantics.ts
2469
- var EVENT_SEMANTICS = {
2470
- page_view: {
2471
- label: "Page view",
2472
- category: "page",
2473
- outcomeRole: "navigation",
2474
- clientVisibility: "simple"
2475
- },
2476
- time_on_site: {
2477
- label: "Time on site",
2478
- category: "engagement",
2479
- outcomeRole: "engagement",
2480
- clientVisibility: "detailed"
2481
- },
2482
- specific_page_visit: {
2483
- label: "Key page visit",
2484
- category: "engagement",
2485
- outcomeRole: "engagement",
2486
- clientVisibility: "detailed"
2487
- },
2488
- scroll_depth: {
2489
- label: "Scroll depth",
2490
- category: "engagement",
2491
- outcomeRole: "engagement",
2492
- clientVisibility: "detailed"
2493
- },
2494
- multi_page_session: {
2495
- label: "Multi-page session",
2496
- category: "engagement",
2497
- outcomeRole: "engagement",
2498
- clientVisibility: "detailed"
2499
- },
2500
- form_start: {
2501
- label: "Form started",
2502
- category: "engagement",
2503
- outcomeRole: "engagement",
2504
- clientVisibility: "simple"
2505
- },
2506
- sdk_heartbeat: {
2507
- label: "SDK heartbeat",
2508
- category: "system",
2509
- outcomeRole: "diagnostic",
2510
- clientVisibility: "hidden"
2511
- },
2512
- page_exit: {
2513
- label: "Page exit",
2514
- category: "engagement",
2515
- outcomeRole: "diagnostic",
2516
- clientVisibility: "detailed"
2517
- },
2518
- form_submit: {
2519
- label: "Form submitted",
2520
- category: "lead",
2521
- outcomeRole: "lead",
2522
- clientVisibility: "simple"
2523
- },
2524
- phone_click: {
2525
- label: "Phone click",
2526
- category: "lead",
2527
- outcomeRole: "lead",
2528
- clientVisibility: "simple"
2529
- },
2530
- cta_click: {
2531
- label: "CTA click",
2532
- category: "engagement",
2533
- outcomeRole: "engagement",
2534
- clientVisibility: "simple"
2535
- },
2536
- site_link_click: {
2537
- label: "Left for sibling site",
2538
- category: "page",
2539
- outcomeRole: "navigation",
2540
- clientVisibility: "detailed"
2541
- },
2542
- site_link_arrival: {
2543
- label: "Arrived from sibling site",
2544
- category: "page",
2545
- outcomeRole: "navigation",
2546
- clientVisibility: "simple"
2547
- }
2548
- };
2549
-
2550
2583
  // ../tracking-core/src/events/registry.ts
2551
2584
  var EVENT_REGISTRY = {
2552
2585
  // --- automatic triggers ---
@@ -3654,7 +3687,7 @@ function GoogleAdsTracking(props) {
3654
3687
  import { createContext as createContext2, useContext as useContext2, useEffect as useEffect5, useMemo as useMemo4 } from "react";
3655
3688
 
3656
3689
  // package.json
3657
- var version = "1.2.1";
3690
+ var version = "1.2.2";
3658
3691
 
3659
3692
  // ../tracking-core/src/phone-react.tsx
3660
3693
  import {
@@ -3867,7 +3900,6 @@ function createTracking(options) {
3867
3900
  }
3868
3901
  } : detectorClient;
3869
3902
  detachers.push(attachAutoPageView(pageClient));
3870
- detachers.push(attachBfcacheRestore(pageClient));
3871
3903
  detachers.push(attachPageExit(detectorClient));
3872
3904
  const timeOnSite = triggers.automatic.time_on_site;
3873
3905
  if (timeOnSite) {