@dcl/hooks 1.3.0 → 1.4.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.
|
@@ -1,2 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
type PageTrackingProperties = Record<string, unknown>;
|
|
2
|
+
/**
|
|
3
|
+
* Tracks a Segment `page()` event.
|
|
4
|
+
*
|
|
5
|
+
* Two call shapes:
|
|
6
|
+
*
|
|
7
|
+
* 1. `usePageTracking(path)` — fires `page(path)` whenever `path` changes.
|
|
8
|
+
* Backwards-compatible with the original signature.
|
|
9
|
+
*
|
|
10
|
+
* 2. `usePageTracking(name, properties)` — fires `page(name, properties)` only
|
|
11
|
+
* after analytics is initialized AND `name` is a non-empty string. Use this
|
|
12
|
+
* shape when the page title is resolved asynchronously (e.g. from a CMS via
|
|
13
|
+
* Helmet + RTK Query) so the event lands AFTER `document.title` updates,
|
|
14
|
+
* avoiding the SPA race that lets Segment auto-capture the previous route's
|
|
15
|
+
* title via `properties.title`.
|
|
16
|
+
*
|
|
17
|
+
* The hook is initialization-aware in both shapes: when `useAnalytics().page`
|
|
18
|
+
* is the no-op fallback (Segment not loaded yet), the call is skipped to avoid
|
|
19
|
+
* wasted work.
|
|
20
|
+
*/
|
|
21
|
+
declare function usePageTracking(path: string): void;
|
|
22
|
+
declare function usePageTracking(name: string | undefined, properties?: PageTrackingProperties): void;
|
|
2
23
|
export { usePageTracking };
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { useEffect } from "react";
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
2
|
import { useAnalytics } from "./useAnalytics";
|
|
3
|
-
|
|
4
|
-
const { page } = useAnalytics();
|
|
3
|
+
function usePageTracking(name, properties) {
|
|
4
|
+
const { isInitialized, page } = useAnalytics();
|
|
5
|
+
const propertiesKey = properties ? JSON.stringify(properties) : "";
|
|
6
|
+
const propertiesRef = useRef(properties);
|
|
7
|
+
propertiesRef.current = properties;
|
|
5
8
|
useEffect(() => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
if (!isInitialized || !name)
|
|
10
|
+
return;
|
|
11
|
+
page(name, propertiesRef.current);
|
|
12
|
+
}, [isInitialized, name, propertiesKey, page]);
|
|
13
|
+
}
|
|
9
14
|
export { usePageTracking };
|
|
10
15
|
//# sourceMappingURL=usePageTracking.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePageTracking.js","sourceRoot":"","sources":["../../src/hooks/usePageTracking.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"usePageTracking.js","sourceRoot":"","sources":["../../src/hooks/usePageTracking.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AA4B7C,SAAS,eAAe,CACtB,IAAwB,EACxB,UAAmC;IAEnC,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,GAAG,YAAY,EAAE,CAAA;IAC9C,MAAM,aAAa,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IAClE,MAAM,aAAa,GAAG,MAAM,CAAC,UAAU,CAAC,CAAA;IACxC,aAAa,CAAC,OAAO,GAAG,UAAU,CAAA;IAElC,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,aAAa,IAAI,CAAC,IAAI;YAAE,OAAM;QACnC,IAAI,CAAC,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;IACnC,CAAC,EAAE,CAAC,aAAa,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,CAAC,CAAC,CAAA;AAChD,CAAC;AAED,OAAO,EAAE,eAAe,EAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcl/hooks",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "esm/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -80,5 +80,5 @@
|
|
|
80
80
|
"<rootDir>/test/setup.ts"
|
|
81
81
|
]
|
|
82
82
|
},
|
|
83
|
-
"commit": "
|
|
83
|
+
"commit": "f4045d834438209730ba9ffdafe2d1f80bff29bb"
|
|
84
84
|
}
|