@aranova/tracking-next 0.17.1 → 0.17.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/index.js CHANGED
@@ -1122,6 +1122,27 @@ function getOrCreateTrackingClient(config) {
1122
1122
  globalClientKey = key;
1123
1123
  return globalClient;
1124
1124
  }
1125
+ var clientCaptureRegistry = /* @__PURE__ */ new WeakMap();
1126
+ function attachClientCapturesOnce(client, build) {
1127
+ let entry = clientCaptureRegistry.get(client);
1128
+ if (entry === void 0) {
1129
+ entry = { detach: build(), refCount: 0 };
1130
+ clientCaptureRegistry.set(client, entry);
1131
+ }
1132
+ entry.refCount += 1;
1133
+ let released = false;
1134
+ return () => {
1135
+ if (released) return;
1136
+ released = true;
1137
+ const current = clientCaptureRegistry.get(client);
1138
+ if (current === void 0) return;
1139
+ current.refCount -= 1;
1140
+ if (current.refCount <= 0) {
1141
+ current.detach();
1142
+ clientCaptureRegistry.delete(client);
1143
+ }
1144
+ };
1145
+ }
1125
1146
  function createTrackingClient(config) {
1126
1147
  const flushIntervalMs = config.flushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS;
1127
1148
  const maxQueueSize = Math.min(config.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE, HARD_MAX_BATCH);
@@ -2667,7 +2688,7 @@ function GoogleAdsTracking(props) {
2667
2688
  var import_navigation = require("next/navigation");
2668
2689
 
2669
2690
  // package.json
2670
- var version = "0.17.1";
2691
+ var version = "0.17.2";
2671
2692
 
2672
2693
  // src/factory.tsx
2673
2694
  var import_react4 = require("react");
@@ -2866,33 +2887,6 @@ function createTracking(options) {
2866
2887
  });
2867
2888
  }
2868
2889
  }, [pathname]);
2869
- (0, import_react4.useEffect)(() => {
2870
- const rawClient = getOrCreateTrackingClient({
2871
- apiKey,
2872
- endpoint,
2873
- surface: "next",
2874
- packageName: "@aranova/tracking-next",
2875
- triggers,
2876
- environment,
2877
- activeGtagIds: gtagIds,
2878
- debug
2879
- });
2880
- return attachBfcacheRestore(wrapClient(rawClient));
2881
- }, []);
2882
- (0, import_react4.useEffect)(() => {
2883
- if (!timeOnSiteConfig) return;
2884
- const rawClient = getOrCreateTrackingClient({
2885
- apiKey,
2886
- endpoint,
2887
- surface: "next",
2888
- packageName: "@aranova/tracking-next",
2889
- triggers,
2890
- environment,
2891
- activeGtagIds: gtagIds,
2892
- debug
2893
- });
2894
- return attachTimeOnSite(wrapClient(rawClient), timeOnSiteConfig);
2895
- }, []);
2896
2890
  const multiPageSessionConfig = triggers.automatic.multi_page_session ?? null;
2897
2891
  const mpsLastPathRef = (0, import_react4.useRef)(null);
2898
2892
  (0, import_react4.useEffect)(() => {
@@ -2943,7 +2937,6 @@ function createTracking(options) {
2943
2937
  }
2944
2938
  }, [pathname]);
2945
2939
  (0, import_react4.useEffect)(() => {
2946
- const detachers = [];
2947
2940
  const rawClient = getOrCreateTrackingClient({
2948
2941
  apiKey,
2949
2942
  endpoint,
@@ -2954,29 +2947,36 @@ function createTracking(options) {
2954
2947
  activeGtagIds: gtagIds,
2955
2948
  debug
2956
2949
  });
2957
- const detectorClient = wrapClient(rawClient);
2958
- detachers.push(attachPageExit(detectorClient));
2959
- const scrollDepth = triggers.automatic.scroll_depth;
2960
- if (scrollDepth) {
2961
- detachers.push(attachScrollDepth(detectorClient, scrollDepth));
2962
- }
2963
- const formStart = triggers.automatic.form_start;
2964
- if (formStart) {
2965
- detachers.push(attachFormStart(detectorClient, formStart));
2966
- }
2967
- const ctaClick = triggers.manual?.cta_click;
2968
- if (ctaClick) {
2969
- detachers.push(attachCtaClickCapture(detectorClient, ctaClick));
2970
- }
2971
- const phoneClick = triggers.manual?.phone_click;
2972
- if (phoneClick) {
2973
- detachers.push(attachPhoneClickCapture(detectorClient, phoneClick));
2974
- }
2975
- return () => {
2976
- for (let i = detachers.length - 1; i >= 0; i--) {
2977
- detachers[i]();
2950
+ return attachClientCapturesOnce(rawClient, () => {
2951
+ const detachers = [];
2952
+ const detectorClient = wrapClient(rawClient);
2953
+ detachers.push(attachPageExit(detectorClient));
2954
+ detachers.push(attachBfcacheRestore(detectorClient));
2955
+ if (timeOnSiteConfig) {
2956
+ detachers.push(attachTimeOnSite(detectorClient, timeOnSiteConfig));
2957
+ }
2958
+ const scrollDepth = triggers.automatic.scroll_depth;
2959
+ if (scrollDepth) {
2960
+ detachers.push(attachScrollDepth(detectorClient, scrollDepth));
2961
+ }
2962
+ const formStart = triggers.automatic.form_start;
2963
+ if (formStart) {
2964
+ detachers.push(attachFormStart(detectorClient, formStart));
2978
2965
  }
2979
- };
2966
+ const ctaClick = triggers.manual?.cta_click;
2967
+ if (ctaClick) {
2968
+ detachers.push(attachCtaClickCapture(detectorClient, ctaClick));
2969
+ }
2970
+ const phoneClick = triggers.manual?.phone_click;
2971
+ if (phoneClick) {
2972
+ detachers.push(attachPhoneClickCapture(detectorClient, phoneClick));
2973
+ }
2974
+ return () => {
2975
+ for (let i = detachers.length - 1; i >= 0; i--) {
2976
+ detachers[i]();
2977
+ }
2978
+ };
2979
+ });
2980
2980
  }, []);
2981
2981
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(TrackingContext.Provider, { value: client, children: [
2982
2982
  /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_react4.Suspense, { fallback: null, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PageViewTracker, { fire: firePageView }) }),