@depsmith/depsmith-sdk-react 0.1.0 → 0.2.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.
- package/dist/{chunk-VMODQRAL.cjs → chunk-6P5WIXSS.cjs} +11 -5
- package/dist/chunk-6P5WIXSS.cjs.map +1 -0
- package/dist/{chunk-U2TD45YM.js → chunk-GCBPFIL3.js} +11 -5
- package/dist/chunk-GCBPFIL3.js.map +1 -0
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +1 -1
- package/dist/react-router.cjs +4 -3
- package/dist/react-router.cjs.map +1 -1
- package/dist/react-router.d.cts +2 -0
- package/dist/react-router.d.ts +2 -0
- package/dist/react-router.js +3 -2
- package/dist/react-router.js.map +1 -1
- package/package.json +3 -3
- package/dist/chunk-U2TD45YM.js.map +0 -1
- package/dist/chunk-VMODQRAL.cjs.map +0 -1
|
@@ -31,14 +31,18 @@ function useDepsmithContext() {
|
|
|
31
31
|
function autocapturesPageViews(config) {
|
|
32
32
|
const ac = config.autocapture;
|
|
33
33
|
if (ac === true) return true;
|
|
34
|
-
if (ac && typeof ac === "object")
|
|
34
|
+
if (ac && typeof ac === "object") {
|
|
35
|
+
const pageviews = ac.pageviews;
|
|
36
|
+
return pageviews === void 0 || Boolean(pageviews);
|
|
37
|
+
}
|
|
35
38
|
return false;
|
|
36
39
|
}
|
|
37
40
|
function bindClient(c) {
|
|
38
41
|
return {
|
|
39
42
|
track: (name, properties) => c.track(name, properties),
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
// Spread, so the `{ type }` options argument reaches the client.
|
|
44
|
+
page: (...args) => c.page(...args),
|
|
45
|
+
screen: (...args) => c.screen(...args),
|
|
42
46
|
identify: (userId, traits) => c.identify(userId, traits),
|
|
43
47
|
alias: (aliasId) => c.alias(aliasId),
|
|
44
48
|
reset: () => c.reset(),
|
|
@@ -78,7 +82,9 @@ function usePageView(key, options = {}) {
|
|
|
78
82
|
}
|
|
79
83
|
return;
|
|
80
84
|
}
|
|
81
|
-
|
|
85
|
+
const { name, properties, type } = latest.current;
|
|
86
|
+
if (type === void 0) api.page(name, properties);
|
|
87
|
+
else api.page(name, properties, { type });
|
|
82
88
|
}, [key, api, pageViewsAutocaptured, debug]);
|
|
83
89
|
}
|
|
84
90
|
|
|
@@ -87,4 +93,4 @@ function usePageView(key, options = {}) {
|
|
|
87
93
|
|
|
88
94
|
|
|
89
95
|
exports.DepsmithProvider = DepsmithProvider; exports.useDepsmith = useDepsmith; exports.usePageView = usePageView;
|
|
90
|
-
//# sourceMappingURL=chunk-
|
|
96
|
+
//# sourceMappingURL=chunk-6P5WIXSS.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/animesh/devspace/depsmith-dev/depsmith-sdk/react/dist/chunk-6P5WIXSS.cjs","../src/provider.tsx","../src/page-view.ts"],"names":[],"mappings":"AAAA,yLAAY;AACZ;AACA;ACFA,8BAAuF;AACvF,qDAA4E;AA0DnE,+CAAA;AA1CT,IAAM,gBAAA,EAAkB,kCAAA,IAA+C,CAAA;AA6BhE,SAAS,gBAAA,CAAiB,KAAA,EAA4C;AAC3E,EAAA,MAAM,CAAC,KAAK,EAAA,EAAI,6BAAA,CAA+B,EAAA,GAAM;AACnD,IAAA,GAAA,CAAI,KAAA,CAAM,MAAA,EAAQ;AAChB,MAAA,OAAO,EAAE,GAAA,EAAK,UAAA,CAAW,KAAA,CAAM,MAAM,CAAA,EAAG,qBAAA,EAAuB,KAAA,EAAO,KAAA,EAAO,MAAM,CAAA;AAAA,IACrF;AACA,IAAA,MAAM,OAAA,EAAS,KAAA,CAAM,MAAA;AACrB,IAAA,GAAA,CAAI,OAAO,OAAA,IAAW,WAAA,EAAa,wBAAA,CAAY,IAAA,CAAK,MAAM,CAAA;AAC1D,IAAA,OAAO;AAAA,MACL,GAAA,EAAK,wBAAA;AA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AAA,EAAW,GAAA,CAAI,IAAA,CAAK,IAAA,EAAM,UAAU,CAAA;AAAA,IAAA,KAC5C,GAAA,CAAI,IAAA,CAAK,IAAA,EAAM,UAAA,EAAY,EAAE,KAAK,CAAC,CAAA;AAAA,EAC1C,CAAA,EAAG,CAAC,GAAA,EAAK,GAAA,EAAK,qBAAA,EAAuB,KAAK,CAAC,CAAA;AAC7C;AFoCA;AACA;AACE;AACA;AACA;AACF,kHAAC","file":"/home/animesh/devspace/depsmith-dev/depsmith-sdk/react/dist/chunk-6P5WIXSS.cjs","sourcesContent":[null,"import { createContext, useContext, useState, type ReactElement, type ReactNode } from 'react';\nimport { DepsmithSdk, type DepsmithSdkClient, type DepsmithSdkConfig } from '@depsmith/depsmith-sdk';\n\n/** Every tracking call the singleton facade offers, minus its lifecycle (init/shutdown). */\nexport type DepsmithApi = Omit<typeof DepsmithSdk, 'init' | 'shutdown'>;\n\ninterface DepsmithContextValue {\n api: DepsmithApi;\n /**\n * True when the SDK's own autocapture already emits `$page_view` on every History navigation.\n * Router-driven page views must then stand down, or each route change is counted twice: the\n * SDK's pushState patch fires first, and an explicit `page()` always emits.\n */\n pageViewsAutocaptured: boolean;\n debug: boolean;\n}\n\nconst DepsmithContext = createContext<DepsmithContextValue | null>(null);\n\nexport type DepsmithProviderProps = {\n children?: ReactNode;\n} & (\n | {\n /** Passed to `DepsmithSdk.init()`. Read once, on first render — later changes are ignored. */\n config: DepsmithSdkConfig;\n client?: never;\n }\n | {\n /** A client you constructed yourself, e.g. a second instance alongside the singleton. */\n client: DepsmithSdkClient;\n config?: never;\n }\n);\n\n/**\n * Initialises the SDK and makes it available to `useDepsmith()` and the page-view hooks.\n *\n * Initialises during the first render, not in an effect. Child effects run before their\n * parent's, so an effect-time init would drop every event a descendant tracks on mount. Render-time\n * is safe to repeat: `DepsmithSdk.init` is a no-op for the same apiKey, which also covers\n * StrictMode's double render and hot reload. On the server (no `window`) it does nothing, and\n * every call through the hooks is a silent no-op until the browser renders.\n *\n * The client is never shut down on unmount: it owns the persisted queue and the `pagehide` flush,\n * which must outlive any one component tree.\n */\nexport function DepsmithProvider(props: DepsmithProviderProps): ReactElement {\n const [value] = useState<DepsmithContextValue>(() => {\n if (props.client) {\n return { api: bindClient(props.client), pageViewsAutocaptured: false, debug: false };\n }\n const config = props.config;\n if (typeof window !== 'undefined') DepsmithSdk.init(config);\n return {\n api: DepsmithSdk,\n pageViewsAutocaptured: autocapturesPageViews(config),\n debug: config.debug ?? false,\n };\n });\n return <DepsmithContext.Provider value={value}>{props.children}</DepsmithContext.Provider>;\n}\n\n/** The SDK, from the nearest `<DepsmithProvider>`. Every method is safe to call during SSR. */\nexport function useDepsmith(): DepsmithApi {\n return useDepsmithContext().api;\n}\n\nexport function useDepsmithContext(): DepsmithContextValue {\n const ctx = useContext(DepsmithContext);\n if (!ctx) throw new Error('useDepsmith must be used inside <DepsmithProvider>');\n return ctx;\n}\n\n/**\n * Mirrors the web SDK's resolveConfig: `true` means all defaults, which include pageviews.\n * `pageviews` is `'path' | 'all' | boolean` from SDK 0.8.0: `'path'`, `'all'` and `true` all\n * autocapture, an absent key keeps the default (`'path'`), and only a falsy value turns it off.\n */\nfunction autocapturesPageViews(config: DepsmithSdkConfig): boolean {\n const ac = config.autocapture;\n if (ac === true) return true;\n if (ac && typeof ac === 'object') {\n // Widened: the published SDK's types may still declare a boolean here.\n const pageviews: unknown = ac.pageviews;\n return pageviews === undefined || Boolean(pageviews);\n }\n return false;\n}\n\nfunction bindClient(c: DepsmithSdkClient): DepsmithApi {\n return {\n track: (name, properties) => c.track(name, properties),\n // Spread, so the `{ type }` options argument reaches the client.\n page: (...args: Parameters<DepsmithSdkClient['page']>) => c.page(...args),\n screen: (...args: Parameters<DepsmithSdkClient['screen']>) => c.screen(...args),\n identify: (userId, traits) => c.identify(userId, traits),\n alias: (aliasId) => c.alias(aliasId),\n reset: () => c.reset(),\n group: (groupId, traits) => c.group(groupId, traits),\n setGroupTraits: (traits) => c.setGroupTraits(traits),\n setProperty: (key, value) => c.setProperty(key, value),\n setOnceProperty: (key, value) => c.setOnceProperty(key, value),\n unsetProperty: (key) => c.unsetProperty(key),\n setUserProperties: (ops) => c.setUserProperties(ops),\n register: (properties) => c.register(properties),\n registerOnce: (properties) => c.registerOnce(properties),\n unregister: (key) => c.unregister(key),\n optIn: () => c.optIn(),\n optOut: () => c.optOut(),\n flush: () => c.flush(),\n getAnonymousId: () => c.getAnonymousId(),\n getSessionId: () => c.getSessionId(),\n setSessionId: (sessionId, startedAt) => c.setSessionId(sessionId, startedAt),\n };\n}\n","import { useEffect, useRef } from 'react';\nimport type { Properties } from '@depsmith/depsmith-sdk';\nimport { useDepsmithContext } from './provider';\n\nexport interface PageViewOptions {\n /**\n * Page name. Defaults to the SDK's `pageNames` match for the location, else `document.title`\n * at the time the view is recorded.\n */\n name?: string;\n properties?: Properties;\n /** Page type, sent as `$page_type` (`product`, `collection`, `cart`, …). */\n type?: string;\n}\n\n/**\n * Emits one `$page_view` per distinct `key` — pass whatever identifies a page in your router:\n * the pathname, or pathname + search if query changes are separate pages for you (the\n * equivalent of `pageviews: 'all'`). Router-agnostic; `@depsmith/depsmith-sdk-react/react-router`\n * wraps it for React Router.\n *\n * Does nothing when the provider's config autocaptures page views (`autocapture: true`, or an\n * object without `pageviews: false`): the SDK already emits on every History navigation, and an\n * explicit `page()` on top would count each route twice. Set `autocapture.pageviews: false` to\n * let the router drive page views instead.\n *\n * `key === undefined` skips — useful while a router is still resolving. A repeated key is ignored,\n * which also absorbs StrictMode's mount → unmount → mount effect replay.\n */\nexport function usePageView(key: string | undefined, options: PageViewOptions = {}): void {\n const { api, pageViewsAutocaptured, debug } = useDepsmithContext();\n const lastKey = useRef<string | undefined>(undefined);\n // Read through a ref so inline `{ properties: {...} }` literals don't re-fire the effect.\n const latest = useRef(options);\n latest.current = options;\n\n useEffect(() => {\n if (key === undefined || key === lastKey.current) return;\n lastKey.current = key;\n if (pageViewsAutocaptured) {\n if (debug) {\n console.warn(\n '[depsmith-sdk-react] usePageView ignored: autocapture already emits page views. ' +\n 'Set autocapture.pageviews: false to drive them from the router.'\n );\n }\n return;\n }\n const { name, properties, type } = latest.current;\n // The options argument only when there is a type, so a call without one is unchanged.\n if (type === undefined) api.page(name, properties);\n else api.page(name, properties, { type });\n }, [key, api, pageViewsAutocaptured, debug]);\n}\n"]}
|
|
@@ -31,14 +31,18 @@ function useDepsmithContext() {
|
|
|
31
31
|
function autocapturesPageViews(config) {
|
|
32
32
|
const ac = config.autocapture;
|
|
33
33
|
if (ac === true) return true;
|
|
34
|
-
if (ac && typeof ac === "object")
|
|
34
|
+
if (ac && typeof ac === "object") {
|
|
35
|
+
const pageviews = ac.pageviews;
|
|
36
|
+
return pageviews === void 0 || Boolean(pageviews);
|
|
37
|
+
}
|
|
35
38
|
return false;
|
|
36
39
|
}
|
|
37
40
|
function bindClient(c) {
|
|
38
41
|
return {
|
|
39
42
|
track: (name, properties) => c.track(name, properties),
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
// Spread, so the `{ type }` options argument reaches the client.
|
|
44
|
+
page: (...args) => c.page(...args),
|
|
45
|
+
screen: (...args) => c.screen(...args),
|
|
42
46
|
identify: (userId, traits) => c.identify(userId, traits),
|
|
43
47
|
alias: (aliasId) => c.alias(aliasId),
|
|
44
48
|
reset: () => c.reset(),
|
|
@@ -78,7 +82,9 @@ function usePageView(key, options = {}) {
|
|
|
78
82
|
}
|
|
79
83
|
return;
|
|
80
84
|
}
|
|
81
|
-
|
|
85
|
+
const { name, properties, type } = latest.current;
|
|
86
|
+
if (type === void 0) api.page(name, properties);
|
|
87
|
+
else api.page(name, properties, { type });
|
|
82
88
|
}, [key, api, pageViewsAutocaptured, debug]);
|
|
83
89
|
}
|
|
84
90
|
|
|
@@ -87,4 +93,4 @@ export {
|
|
|
87
93
|
useDepsmith,
|
|
88
94
|
usePageView
|
|
89
95
|
};
|
|
90
|
-
//# sourceMappingURL=chunk-
|
|
96
|
+
//# sourceMappingURL=chunk-GCBPFIL3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/provider.tsx","../src/page-view.ts"],"sourcesContent":["import { createContext, useContext, useState, type ReactElement, type ReactNode } from 'react';\nimport { DepsmithSdk, type DepsmithSdkClient, type DepsmithSdkConfig } from '@depsmith/depsmith-sdk';\n\n/** Every tracking call the singleton facade offers, minus its lifecycle (init/shutdown). */\nexport type DepsmithApi = Omit<typeof DepsmithSdk, 'init' | 'shutdown'>;\n\ninterface DepsmithContextValue {\n api: DepsmithApi;\n /**\n * True when the SDK's own autocapture already emits `$page_view` on every History navigation.\n * Router-driven page views must then stand down, or each route change is counted twice: the\n * SDK's pushState patch fires first, and an explicit `page()` always emits.\n */\n pageViewsAutocaptured: boolean;\n debug: boolean;\n}\n\nconst DepsmithContext = createContext<DepsmithContextValue | null>(null);\n\nexport type DepsmithProviderProps = {\n children?: ReactNode;\n} & (\n | {\n /** Passed to `DepsmithSdk.init()`. Read once, on first render — later changes are ignored. */\n config: DepsmithSdkConfig;\n client?: never;\n }\n | {\n /** A client you constructed yourself, e.g. a second instance alongside the singleton. */\n client: DepsmithSdkClient;\n config?: never;\n }\n);\n\n/**\n * Initialises the SDK and makes it available to `useDepsmith()` and the page-view hooks.\n *\n * Initialises during the first render, not in an effect. Child effects run before their\n * parent's, so an effect-time init would drop every event a descendant tracks on mount. Render-time\n * is safe to repeat: `DepsmithSdk.init` is a no-op for the same apiKey, which also covers\n * StrictMode's double render and hot reload. On the server (no `window`) it does nothing, and\n * every call through the hooks is a silent no-op until the browser renders.\n *\n * The client is never shut down on unmount: it owns the persisted queue and the `pagehide` flush,\n * which must outlive any one component tree.\n */\nexport function DepsmithProvider(props: DepsmithProviderProps): ReactElement {\n const [value] = useState<DepsmithContextValue>(() => {\n if (props.client) {\n return { api: bindClient(props.client), pageViewsAutocaptured: false, debug: false };\n }\n const config = props.config;\n if (typeof window !== 'undefined') DepsmithSdk.init(config);\n return {\n api: DepsmithSdk,\n pageViewsAutocaptured: autocapturesPageViews(config),\n debug: config.debug ?? false,\n };\n });\n return <DepsmithContext.Provider value={value}>{props.children}</DepsmithContext.Provider>;\n}\n\n/** The SDK, from the nearest `<DepsmithProvider>`. Every method is safe to call during SSR. */\nexport function useDepsmith(): DepsmithApi {\n return useDepsmithContext().api;\n}\n\nexport function useDepsmithContext(): DepsmithContextValue {\n const ctx = useContext(DepsmithContext);\n if (!ctx) throw new Error('useDepsmith must be used inside <DepsmithProvider>');\n return ctx;\n}\n\n/**\n * Mirrors the web SDK's resolveConfig: `true` means all defaults, which include pageviews.\n * `pageviews` is `'path' | 'all' | boolean` from SDK 0.8.0: `'path'`, `'all'` and `true` all\n * autocapture, an absent key keeps the default (`'path'`), and only a falsy value turns it off.\n */\nfunction autocapturesPageViews(config: DepsmithSdkConfig): boolean {\n const ac = config.autocapture;\n if (ac === true) return true;\n if (ac && typeof ac === 'object') {\n // Widened: the published SDK's types may still declare a boolean here.\n const pageviews: unknown = ac.pageviews;\n return pageviews === undefined || Boolean(pageviews);\n }\n return false;\n}\n\nfunction bindClient(c: DepsmithSdkClient): DepsmithApi {\n return {\n track: (name, properties) => c.track(name, properties),\n // Spread, so the `{ type }` options argument reaches the client.\n page: (...args: Parameters<DepsmithSdkClient['page']>) => c.page(...args),\n screen: (...args: Parameters<DepsmithSdkClient['screen']>) => c.screen(...args),\n identify: (userId, traits) => c.identify(userId, traits),\n alias: (aliasId) => c.alias(aliasId),\n reset: () => c.reset(),\n group: (groupId, traits) => c.group(groupId, traits),\n setGroupTraits: (traits) => c.setGroupTraits(traits),\n setProperty: (key, value) => c.setProperty(key, value),\n setOnceProperty: (key, value) => c.setOnceProperty(key, value),\n unsetProperty: (key) => c.unsetProperty(key),\n setUserProperties: (ops) => c.setUserProperties(ops),\n register: (properties) => c.register(properties),\n registerOnce: (properties) => c.registerOnce(properties),\n unregister: (key) => c.unregister(key),\n optIn: () => c.optIn(),\n optOut: () => c.optOut(),\n flush: () => c.flush(),\n getAnonymousId: () => c.getAnonymousId(),\n getSessionId: () => c.getSessionId(),\n setSessionId: (sessionId, startedAt) => c.setSessionId(sessionId, startedAt),\n };\n}\n","import { useEffect, useRef } from 'react';\nimport type { Properties } from '@depsmith/depsmith-sdk';\nimport { useDepsmithContext } from './provider';\n\nexport interface PageViewOptions {\n /**\n * Page name. Defaults to the SDK's `pageNames` match for the location, else `document.title`\n * at the time the view is recorded.\n */\n name?: string;\n properties?: Properties;\n /** Page type, sent as `$page_type` (`product`, `collection`, `cart`, …). */\n type?: string;\n}\n\n/**\n * Emits one `$page_view` per distinct `key` — pass whatever identifies a page in your router:\n * the pathname, or pathname + search if query changes are separate pages for you (the\n * equivalent of `pageviews: 'all'`). Router-agnostic; `@depsmith/depsmith-sdk-react/react-router`\n * wraps it for React Router.\n *\n * Does nothing when the provider's config autocaptures page views (`autocapture: true`, or an\n * object without `pageviews: false`): the SDK already emits on every History navigation, and an\n * explicit `page()` on top would count each route twice. Set `autocapture.pageviews: false` to\n * let the router drive page views instead.\n *\n * `key === undefined` skips — useful while a router is still resolving. A repeated key is ignored,\n * which also absorbs StrictMode's mount → unmount → mount effect replay.\n */\nexport function usePageView(key: string | undefined, options: PageViewOptions = {}): void {\n const { api, pageViewsAutocaptured, debug } = useDepsmithContext();\n const lastKey = useRef<string | undefined>(undefined);\n // Read through a ref so inline `{ properties: {...} }` literals don't re-fire the effect.\n const latest = useRef(options);\n latest.current = options;\n\n useEffect(() => {\n if (key === undefined || key === lastKey.current) return;\n lastKey.current = key;\n if (pageViewsAutocaptured) {\n if (debug) {\n console.warn(\n '[depsmith-sdk-react] usePageView ignored: autocapture already emits page views. ' +\n 'Set autocapture.pageviews: false to drive them from the router.'\n );\n }\n return;\n }\n const { name, properties, type } = latest.current;\n // The options argument only when there is a type, so a call without one is unchanged.\n if (type === undefined) api.page(name, properties);\n else api.page(name, properties, { type });\n }, [key, api, pageViewsAutocaptured, debug]);\n}\n"],"mappings":";;;AAAA,SAAS,eAAe,YAAY,gBAAmD;A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AAW,KAAI,KAAK,MAAM,UAAU;AAAA,QAC5C,KAAI,KAAK,MAAM,YAAY,EAAE,KAAK,CAAC;AAAA,EAC1C,GAAG,CAAC,KAAK,KAAK,uBAAuB,KAAK,CAAC;AAC7C;","names":[]}
|
package/dist/index.cjs
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _chunk6P5WIXSScjs = require('./chunk-6P5WIXSS.cjs');
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
exports.DepsmithProvider =
|
|
12
|
+
exports.DepsmithProvider = _chunk6P5WIXSScjs.DepsmithProvider; exports.useDepsmith = _chunk6P5WIXSScjs.useDepsmith; exports.usePageView = _chunk6P5WIXSScjs.usePageView;
|
|
13
13
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/
|
|
1
|
+
{"version":3,"sources":["/home/animesh/devspace/depsmith-dev/depsmith-sdk/react/dist/index.cjs"],"names":[],"mappings":"AAAA,qFAAY;AACZ;AACE;AACA;AACA;AACF,wDAA6B;AAC7B;AACE;AACA;AACA;AACF,wKAAC","file":"/home/animesh/devspace/depsmith-dev/depsmith-sdk/react/dist/index.cjs"}
|
package/dist/index.d.cts
CHANGED
|
@@ -31,13 +31,19 @@ declare function DepsmithProvider(props: DepsmithProviderProps): ReactElement;
|
|
|
31
31
|
declare function useDepsmith(): DepsmithApi;
|
|
32
32
|
|
|
33
33
|
interface PageViewOptions {
|
|
34
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Page name. Defaults to the SDK's `pageNames` match for the location, else `document.title`
|
|
36
|
+
* at the time the view is recorded.
|
|
37
|
+
*/
|
|
35
38
|
name?: string;
|
|
36
39
|
properties?: Properties;
|
|
40
|
+
/** Page type, sent as `$page_type` (`product`, `collection`, `cart`, …). */
|
|
41
|
+
type?: string;
|
|
37
42
|
}
|
|
38
43
|
/**
|
|
39
|
-
* Emits one `$page_view` per distinct `key` — pass whatever identifies a page in your router
|
|
40
|
-
*
|
|
44
|
+
* Emits one `$page_view` per distinct `key` — pass whatever identifies a page in your router:
|
|
45
|
+
* the pathname, or pathname + search if query changes are separate pages for you (the
|
|
46
|
+
* equivalent of `pageviews: 'all'`). Router-agnostic; `@depsmith/depsmith-sdk-react/react-router`
|
|
41
47
|
* wraps it for React Router.
|
|
42
48
|
*
|
|
43
49
|
* Does nothing when the provider's config autocaptures page views (`autocapture: true`, or an
|
package/dist/index.d.ts
CHANGED
|
@@ -31,13 +31,19 @@ declare function DepsmithProvider(props: DepsmithProviderProps): ReactElement;
|
|
|
31
31
|
declare function useDepsmith(): DepsmithApi;
|
|
32
32
|
|
|
33
33
|
interface PageViewOptions {
|
|
34
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Page name. Defaults to the SDK's `pageNames` match for the location, else `document.title`
|
|
36
|
+
* at the time the view is recorded.
|
|
37
|
+
*/
|
|
35
38
|
name?: string;
|
|
36
39
|
properties?: Properties;
|
|
40
|
+
/** Page type, sent as `$page_type` (`product`, `collection`, `cart`, …). */
|
|
41
|
+
type?: string;
|
|
37
42
|
}
|
|
38
43
|
/**
|
|
39
|
-
* Emits one `$page_view` per distinct `key` — pass whatever identifies a page in your router
|
|
40
|
-
*
|
|
44
|
+
* Emits one `$page_view` per distinct `key` — pass whatever identifies a page in your router:
|
|
45
|
+
* the pathname, or pathname + search if query changes are separate pages for you (the
|
|
46
|
+
* equivalent of `pageviews: 'all'`). Router-agnostic; `@depsmith/depsmith-sdk-react/react-router`
|
|
41
47
|
* wraps it for React Router.
|
|
42
48
|
*
|
|
43
49
|
* Does nothing when the provider's config autocaptures page views (`autocapture: true`, or an
|
package/dist/index.js
CHANGED
package/dist/react-router.cjs
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }'use client';
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var _chunk6P5WIXSScjs = require('./chunk-6P5WIXSS.cjs');
|
|
6
6
|
|
|
7
7
|
// src/react-router.ts
|
|
8
8
|
var _reactrouter = require('react-router');
|
|
9
9
|
function useRouterPageViews(options = {}) {
|
|
10
10
|
const location = _reactrouter.useLocation.call(void 0, );
|
|
11
11
|
const key = options.includeSearch === false ? location.pathname : location.pathname + location.search;
|
|
12
|
-
|
|
12
|
+
_chunk6P5WIXSScjs.usePageView.call(void 0, key, {
|
|
13
13
|
name: _optionalChain([options, 'access', _ => _.getName, 'optionalCall', _2 => _2(location)]),
|
|
14
|
-
properties: _optionalChain([options, 'access', _3 => _3.getProperties, 'optionalCall', _4 => _4(location)])
|
|
14
|
+
properties: _optionalChain([options, 'access', _3 => _3.getProperties, 'optionalCall', _4 => _4(location)]),
|
|
15
|
+
type: _optionalChain([options, 'access', _5 => _5.getType, 'optionalCall', _6 => _6(location)])
|
|
15
16
|
});
|
|
16
17
|
}
|
|
17
18
|
function DepsmithRouterPageViews(props) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/
|
|
1
|
+
{"version":3,"sources":["/home/animesh/devspace/depsmith-dev/depsmith-sdk/react/dist/react-router.cjs","../src/react-router.ts"],"names":[],"mappings":"AAAA,ylBAAY;AACZ;AACE;AACF,wDAA6B;AAC7B;AACA;ACLA,2CAA2C;AAoBpC,SAAS,kBAAA,CAAmB,QAAA,EAAiC,CAAC,CAAA,EAAS;AAC5E,EAAA,MAAM,SAAA,EAAW,sCAAA,CAAY;AAC7B,EAAA,MAAM,IAAA,EACJ,OAAA,CAAQ,cAAA,IAAkB,MAAA,EAAQ,QAAA,CAAS,SAAA,EAAW,QAAA,CAAS,SAAA,EAAW,QAAA,CAAS,MAAA;AACrF,EAAA,2CAAA,GAAY,EAAK;AAAA,IACf,IAAA,kBAAM,OAAA,mBAAQ,OAAA,0BAAA,CAAU,QAAQ,GAAA;AAAA,IAChC,UAAA,kBAAY,OAAA,qBAAQ,aAAA,0BAAA,CAAgB,QAAQ,GAAA;AAAA,IAC5C,IAAA,kBAAM,OAAA,qBAAQ,OAAA,0BAAA,CAAU,QAAQ;AAAA,EAClC,CAAC,CAAA;AACH;AAGO,SAAS,uBAAA,CAAwB,KAAA,EAAoC;AAC1E,EAAA,kBAAA,CAAmB,KAAK,CAAA;AACxB,EAAA,OAAO,IAAA;AACT;ADfA;AACE;AACA;AACF,2GAAC","file":"/home/animesh/devspace/depsmith-dev/depsmith-sdk/react/dist/react-router.cjs","sourcesContent":[null,"import { useLocation, type Location } from 'react-router';\nimport type { Properties } from '@depsmith/depsmith-sdk';\nimport { usePageView } from './page-view';\n\nexport interface RouterPageViewOptions {\n /** Count a change in `?query` as a new page. Default true. The `#hash` never is. */\n includeSearch?: boolean;\n /** Page name for a location. Default: `document.title`. */\n getName?: (location: Location) => string | undefined;\n /** Page type for a location, sent as `$page_type`. */\n getType?: (location: Location) => string | undefined;\n /** Extra properties for a location's `$page_view`. */\n getProperties?: (location: Location) => Properties | undefined;\n}\n\n/**\n * One `$page_view` per React Router navigation. Must render inside the router and the\n * `<DepsmithProvider>`. Same autocapture rule as `usePageView`: with `autocapture.pageviews`\n * on, this stands down.\n */\nexport function useRouterPageViews(options: RouterPageViewOptions = {}): void {\n const location = useLocation();\n const key =\n options.includeSearch === false ? location.pathname : location.pathname + location.search;\n usePageView(key, {\n name: options.getName?.(location),\n properties: options.getProperties?.(location),\n type: options.getType?.(location),\n });\n}\n\n/** Component form of `useRouterPageViews`, for dropping into a layout route. Renders nothing. */\nexport function DepsmithRouterPageViews(props: RouterPageViewOptions): null {\n useRouterPageViews(props);\n return null;\n}\n"]}
|
package/dist/react-router.d.cts
CHANGED
|
@@ -6,6 +6,8 @@ interface RouterPageViewOptions {
|
|
|
6
6
|
includeSearch?: boolean;
|
|
7
7
|
/** Page name for a location. Default: `document.title`. */
|
|
8
8
|
getName?: (location: Location) => string | undefined;
|
|
9
|
+
/** Page type for a location, sent as `$page_type`. */
|
|
10
|
+
getType?: (location: Location) => string | undefined;
|
|
9
11
|
/** Extra properties for a location's `$page_view`. */
|
|
10
12
|
getProperties?: (location: Location) => Properties | undefined;
|
|
11
13
|
}
|
package/dist/react-router.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ interface RouterPageViewOptions {
|
|
|
6
6
|
includeSearch?: boolean;
|
|
7
7
|
/** Page name for a location. Default: `document.title`. */
|
|
8
8
|
getName?: (location: Location) => string | undefined;
|
|
9
|
+
/** Page type for a location, sent as `$page_type`. */
|
|
10
|
+
getType?: (location: Location) => string | undefined;
|
|
9
11
|
/** Extra properties for a location's `$page_view`. */
|
|
10
12
|
getProperties?: (location: Location) => Properties | undefined;
|
|
11
13
|
}
|
package/dist/react-router.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import {
|
|
3
3
|
usePageView
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-GCBPFIL3.js";
|
|
5
5
|
|
|
6
6
|
// src/react-router.ts
|
|
7
7
|
import { useLocation } from "react-router";
|
|
@@ -10,7 +10,8 @@ function useRouterPageViews(options = {}) {
|
|
|
10
10
|
const key = options.includeSearch === false ? location.pathname : location.pathname + location.search;
|
|
11
11
|
usePageView(key, {
|
|
12
12
|
name: options.getName?.(location),
|
|
13
|
-
properties: options.getProperties?.(location)
|
|
13
|
+
properties: options.getProperties?.(location),
|
|
14
|
+
type: options.getType?.(location)
|
|
14
15
|
});
|
|
15
16
|
}
|
|
16
17
|
function DepsmithRouterPageViews(props) {
|
package/dist/react-router.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/react-router.ts"],"sourcesContent":["import { useLocation, type Location } from 'react-router';\nimport type { Properties } from '@depsmith/depsmith-sdk';\nimport { usePageView } from './page-view';\n\nexport interface RouterPageViewOptions {\n /** Count a change in `?query` as a new page. Default true. The `#hash` never is. */\n includeSearch?: boolean;\n /** Page name for a location. Default: `document.title`. */\n getName?: (location: Location) => string | undefined;\n /** Extra properties for a location's `$page_view`. */\n getProperties?: (location: Location) => Properties | undefined;\n}\n\n/**\n * One `$page_view` per React Router navigation. Must render inside the router and the\n * `<DepsmithProvider>`. Same autocapture rule as `usePageView`: with `autocapture.pageviews`\n * on, this stands down.\n */\nexport function useRouterPageViews(options: RouterPageViewOptions = {}): void {\n const location = useLocation();\n const key =\n options.includeSearch === false ? location.pathname : location.pathname + location.search;\n usePageView(key, {\n name: options.getName?.(location),\n properties: options.getProperties?.(location),\n });\n}\n\n/** Component form of `useRouterPageViews`, for dropping into a layout route. Renders nothing. */\nexport function DepsmithRouterPageViews(props: RouterPageViewOptions): null {\n useRouterPageViews(props);\n return null;\n}\n"],"mappings":";;;;;;AAAA,SAAS,mBAAkC;
|
|
1
|
+
{"version":3,"sources":["../src/react-router.ts"],"sourcesContent":["import { useLocation, type Location } from 'react-router';\nimport type { Properties } from '@depsmith/depsmith-sdk';\nimport { usePageView } from './page-view';\n\nexport interface RouterPageViewOptions {\n /** Count a change in `?query` as a new page. Default true. The `#hash` never is. */\n includeSearch?: boolean;\n /** Page name for a location. Default: `document.title`. */\n getName?: (location: Location) => string | undefined;\n /** Page type for a location, sent as `$page_type`. */\n getType?: (location: Location) => string | undefined;\n /** Extra properties for a location's `$page_view`. */\n getProperties?: (location: Location) => Properties | undefined;\n}\n\n/**\n * One `$page_view` per React Router navigation. Must render inside the router and the\n * `<DepsmithProvider>`. Same autocapture rule as `usePageView`: with `autocapture.pageviews`\n * on, this stands down.\n */\nexport function useRouterPageViews(options: RouterPageViewOptions = {}): void {\n const location = useLocation();\n const key =\n options.includeSearch === false ? location.pathname : location.pathname + location.search;\n usePageView(key, {\n name: options.getName?.(location),\n properties: options.getProperties?.(location),\n type: options.getType?.(location),\n });\n}\n\n/** Component form of `useRouterPageViews`, for dropping into a layout route. Renders nothing. */\nexport function DepsmithRouterPageViews(props: RouterPageViewOptions): null {\n useRouterPageViews(props);\n return null;\n}\n"],"mappings":";;;;;;AAAA,SAAS,mBAAkC;AAoBpC,SAAS,mBAAmB,UAAiC,CAAC,GAAS;AAC5E,QAAM,WAAW,YAAY;AAC7B,QAAM,MACJ,QAAQ,kBAAkB,QAAQ,SAAS,WAAW,SAAS,WAAW,SAAS;AACrF,cAAY,KAAK;AAAA,IACf,MAAM,QAAQ,UAAU,QAAQ;AAAA,IAChC,YAAY,QAAQ,gBAAgB,QAAQ;AAAA,IAC5C,MAAM,QAAQ,UAAU,QAAQ;AAAA,EAClC,CAAC;AACH;AAGO,SAAS,wBAAwB,OAAoC;AAC1E,qBAAmB,KAAK;AACxB,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@depsmith/depsmith-sdk-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "DepSmith SDK — React bindings (provider, hooks, router page views) over @depsmith/depsmith-sdk",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://depsmith.com",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"typecheck": "tsc --noEmit"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@depsmith/depsmith-sdk": ">=0.
|
|
34
|
+
"@depsmith/depsmith-sdk": ">=0.8.0 <1.0.0",
|
|
35
35
|
"react": ">=16.8.0",
|
|
36
36
|
"react-router": ">=6.0.0"
|
|
37
37
|
},
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
}
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@depsmith/depsmith-sdk": "^0.
|
|
44
|
+
"@depsmith/depsmith-sdk": "^0.8.0",
|
|
45
45
|
"@testing-library/dom": "^10.4.2",
|
|
46
46
|
"@testing-library/react": "^16.3.0",
|
|
47
47
|
"@types/react": "^19.1.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/provider.tsx","../src/page-view.ts"],"sourcesContent":["import { createContext, useContext, useState, type ReactElement, type ReactNode } from 'react';\nimport { DepsmithSdk, type DepsmithSdkClient, type DepsmithSdkConfig } from '@depsmith/depsmith-sdk';\n\n/** Every tracking call the singleton facade offers, minus its lifecycle (init/shutdown). */\nexport type DepsmithApi = Omit<typeof DepsmithSdk, 'init' | 'shutdown'>;\n\ninterface DepsmithContextValue {\n api: DepsmithApi;\n /**\n * True when the SDK's own autocapture already emits `$page_view` on every History navigation.\n * Router-driven page views must then stand down, or each route change is counted twice: the\n * SDK's pushState patch fires first, and an explicit `page()` always emits.\n */\n pageViewsAutocaptured: boolean;\n debug: boolean;\n}\n\nconst DepsmithContext = createContext<DepsmithContextValue | null>(null);\n\nexport type DepsmithProviderProps = {\n children?: ReactNode;\n} & (\n | {\n /** Passed to `DepsmithSdk.init()`. Read once, on first render — later changes are ignored. */\n config: DepsmithSdkConfig;\n client?: never;\n }\n | {\n /** A client you constructed yourself, e.g. a second instance alongside the singleton. */\n client: DepsmithSdkClient;\n config?: never;\n }\n);\n\n/**\n * Initialises the SDK and makes it available to `useDepsmith()` and the page-view hooks.\n *\n * Initialises during the first render, not in an effect. Child effects run before their\n * parent's, so an effect-time init would drop every event a descendant tracks on mount. Render-time\n * is safe to repeat: `DepsmithSdk.init` is a no-op for the same apiKey, which also covers\n * StrictMode's double render and hot reload. On the server (no `window`) it does nothing, and\n * every call through the hooks is a silent no-op until the browser renders.\n *\n * The client is never shut down on unmount: it owns the persisted queue and the `pagehide` flush,\n * which must outlive any one component tree.\n */\nexport function DepsmithProvider(props: DepsmithProviderProps): ReactElement {\n const [value] = useState<DepsmithContextValue>(() => {\n if (props.client) {\n return { api: bindClient(props.client), pageViewsAutocaptured: false, debug: false };\n }\n const config = props.config;\n if (typeof window !== 'undefined') DepsmithSdk.init(config);\n return {\n api: DepsmithSdk,\n pageViewsAutocaptured: autocapturesPageViews(config),\n debug: config.debug ?? false,\n };\n });\n return <DepsmithContext.Provider value={value}>{props.children}</DepsmithContext.Provider>;\n}\n\n/** The SDK, from the nearest `<DepsmithProvider>`. Every method is safe to call during SSR. */\nexport function useDepsmith(): DepsmithApi {\n return useDepsmithContext().api;\n}\n\nexport function useDepsmithContext(): DepsmithContextValue {\n const ctx = useContext(DepsmithContext);\n if (!ctx) throw new Error('useDepsmith must be used inside <DepsmithProvider>');\n return ctx;\n}\n\n/** Mirrors the web SDK's resolveConfig: `true` means all defaults, which include pageviews. */\nfunction autocapturesPageViews(config: DepsmithSdkConfig): boolean {\n const ac = config.autocapture;\n if (ac === true) return true;\n if (ac && typeof ac === 'object') return ac.pageviews !== false;\n return false;\n}\n\nfunction bindClient(c: DepsmithSdkClient): DepsmithApi {\n return {\n track: (name, properties) => c.track(name, properties),\n page: (name, properties) => c.page(name, properties),\n screen: (name, properties) => c.screen(name, properties),\n identify: (userId, traits) => c.identify(userId, traits),\n alias: (aliasId) => c.alias(aliasId),\n reset: () => c.reset(),\n group: (groupId, traits) => c.group(groupId, traits),\n setGroupTraits: (traits) => c.setGroupTraits(traits),\n setProperty: (key, value) => c.setProperty(key, value),\n setOnceProperty: (key, value) => c.setOnceProperty(key, value),\n unsetProperty: (key) => c.unsetProperty(key),\n setUserProperties: (ops) => c.setUserProperties(ops),\n register: (properties) => c.register(properties),\n registerOnce: (properties) => c.registerOnce(properties),\n unregister: (key) => c.unregister(key),\n optIn: () => c.optIn(),\n optOut: () => c.optOut(),\n flush: () => c.flush(),\n getAnonymousId: () => c.getAnonymousId(),\n getSessionId: () => c.getSessionId(),\n setSessionId: (sessionId, startedAt) => c.setSessionId(sessionId, startedAt),\n };\n}\n","import { useEffect, useRef } from 'react';\nimport type { Properties } from '@depsmith/depsmith-sdk';\nimport { useDepsmithContext } from './provider';\n\nexport interface PageViewOptions {\n /** Page name. Defaults to `document.title` at the time the view is recorded. */\n name?: string;\n properties?: Properties;\n}\n\n/**\n * Emits one `$page_view` per distinct `key` — pass whatever identifies a page in your router,\n * typically pathname + search. Router-agnostic; `@depsmith/depsmith-sdk-react/react-router`\n * wraps it for React Router.\n *\n * Does nothing when the provider's config autocaptures page views (`autocapture: true`, or an\n * object without `pageviews: false`): the SDK already emits on every History navigation, and an\n * explicit `page()` on top would count each route twice. Set `autocapture.pageviews: false` to\n * let the router drive page views instead.\n *\n * `key === undefined` skips — useful while a router is still resolving. A repeated key is ignored,\n * which also absorbs StrictMode's mount → unmount → mount effect replay.\n */\nexport function usePageView(key: string | undefined, options: PageViewOptions = {}): void {\n const { api, pageViewsAutocaptured, debug } = useDepsmithContext();\n const lastKey = useRef<string | undefined>(undefined);\n // Read through a ref so inline `{ properties: {...} }` literals don't re-fire the effect.\n const latest = useRef(options);\n latest.current = options;\n\n useEffect(() => {\n if (key === undefined || key === lastKey.current) return;\n lastKey.current = key;\n if (pageViewsAutocaptured) {\n if (debug) {\n console.warn(\n '[depsmith-sdk-react] usePageView ignored: autocapture already emits page views. ' +\n 'Set autocapture.pageviews: false to drive them from the router.'\n );\n }\n return;\n }\n api.page(latest.current.name, latest.current.properties);\n }, [key, api, pageViewsAutocaptured, debug]);\n}\n"],"mappings":";;;AAAA,SAAS,eAAe,YAAY,gBAAmD;AACvF,SAAS,mBAAmE;AA0DnE;AA1CT,IAAM,kBAAkB,cAA2C,IAAI;AA6BhE,SAAS,iBAAiB,OAA4C;AAC3E,QAAM,CAAC,KAAK,IAAI,SAA+B,MAAM;AACnD,QAAI,MAAM,QAAQ;AAChB,aAAO,EAAE,KAAK,WAAW,MAAM,MA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AA,CAAU,EAAA,GAAM;AACd,IAAA,GAAA,CAAI,IAAA,IAAQ,KAAA,EAAA,GAAa,IAAA,IAAQ,OAAA,CAAQ,OAAA,EAAS,MAAA;AAClD,IAAA,OAAA,CAAQ,QAAA,EAAU,GAAA;AAClB,IAAA,GAAA,CAAI,qBAAA,EAAuB;AACzB,MAAA,GAAA,CAAI,KAAA,EAAO;AACT,QAAA,OAAA,CAAQ,IAAA;AAAA,UACN;AAAA,QAEF,CAAA;AAAA,MACF;AACA,MAAA,MAAA;AAAA,IACF;AACA,IAAA,GAAA,CAAI,IAAA,CAAK,MAAA,CAAO,OAAA,CAAQ,IAAA,EAAM,MAAA,CAAO,OAAA,CAAQ,UAAU,CAAA;AAAA,EACzD,CAAA,EAAG,CAAC,GAAA,EAAK,GAAA,EAAK,qBAAA,EAAuB,KAAK,CAAC,CAAA;AAC7C;AFuCA;AACA;AACE;AACA;AACA;AACF,kHAAC","file":"/tmp/depsmith-react-rel/react/dist/chunk-VMODQRAL.cjs","sourcesContent":[null,"import { createContext, useContext, useState, type ReactElement, type ReactNode } from 'react';\nimport { DepsmithSdk, type DepsmithSdkClient, type DepsmithSdkConfig } from '@depsmith/depsmith-sdk';\n\n/** Every tracking call the singleton facade offers, minus its lifecycle (init/shutdown). */\nexport type DepsmithApi = Omit<typeof DepsmithSdk, 'init' | 'shutdown'>;\n\ninterface DepsmithContextValue {\n api: DepsmithApi;\n /**\n * True when the SDK's own autocapture already emits `$page_view` on every History navigation.\n * Router-driven page views must then stand down, or each route change is counted twice: the\n * SDK's pushState patch fires first, and an explicit `page()` always emits.\n */\n pageViewsAutocaptured: boolean;\n debug: boolean;\n}\n\nconst DepsmithContext = createContext<DepsmithContextValue | null>(null);\n\nexport type DepsmithProviderProps = {\n children?: ReactNode;\n} & (\n | {\n /** Passed to `DepsmithSdk.init()`. Read once, on first render — later changes are ignored. */\n config: DepsmithSdkConfig;\n client?: never;\n }\n | {\n /** A client you constructed yourself, e.g. a second instance alongside the singleton. */\n client: DepsmithSdkClient;\n config?: never;\n }\n);\n\n/**\n * Initialises the SDK and makes it available to `useDepsmith()` and the page-view hooks.\n *\n * Initialises during the first render, not in an effect. Child effects run before their\n * parent's, so an effect-time init would drop every event a descendant tracks on mount. Render-time\n * is safe to repeat: `DepsmithSdk.init` is a no-op for the same apiKey, which also covers\n * StrictMode's double render and hot reload. On the server (no `window`) it does nothing, and\n * every call through the hooks is a silent no-op until the browser renders.\n *\n * The client is never shut down on unmount: it owns the persisted queue and the `pagehide` flush,\n * which must outlive any one component tree.\n */\nexport function DepsmithProvider(props: DepsmithProviderProps): ReactElement {\n const [value] = useState<DepsmithContextValue>(() => {\n if (props.client) {\n return { api: bindClient(props.client), pageViewsAutocaptured: false, debug: false };\n }\n const config = props.config;\n if (typeof window !== 'undefined') DepsmithSdk.init(config);\n return {\n api: DepsmithSdk,\n pageViewsAutocaptured: autocapturesPageViews(config),\n debug: config.debug ?? false,\n };\n });\n return <DepsmithContext.Provider value={value}>{props.children}</DepsmithContext.Provider>;\n}\n\n/** The SDK, from the nearest `<DepsmithProvider>`. Every method is safe to call during SSR. */\nexport function useDepsmith(): DepsmithApi {\n return useDepsmithContext().api;\n}\n\nexport function useDepsmithContext(): DepsmithContextValue {\n const ctx = useContext(DepsmithContext);\n if (!ctx) throw new Error('useDepsmith must be used inside <DepsmithProvider>');\n return ctx;\n}\n\n/** Mirrors the web SDK's resolveConfig: `true` means all defaults, which include pageviews. */\nfunction autocapturesPageViews(config: DepsmithSdkConfig): boolean {\n const ac = config.autocapture;\n if (ac === true) return true;\n if (ac && typeof ac === 'object') return ac.pageviews !== false;\n return false;\n}\n\nfunction bindClient(c: DepsmithSdkClient): DepsmithApi {\n return {\n track: (name, properties) => c.track(name, properties),\n page: (name, properties) => c.page(name, properties),\n screen: (name, properties) => c.screen(name, properties),\n identify: (userId, traits) => c.identify(userId, traits),\n alias: (aliasId) => c.alias(aliasId),\n reset: () => c.reset(),\n group: (groupId, traits) => c.group(groupId, traits),\n setGroupTraits: (traits) => c.setGroupTraits(traits),\n setProperty: (key, value) => c.setProperty(key, value),\n setOnceProperty: (key, value) => c.setOnceProperty(key, value),\n unsetProperty: (key) => c.unsetProperty(key),\n setUserProperties: (ops) => c.setUserProperties(ops),\n register: (properties) => c.register(properties),\n registerOnce: (properties) => c.registerOnce(properties),\n unregister: (key) => c.unregister(key),\n optIn: () => c.optIn(),\n optOut: () => c.optOut(),\n flush: () => c.flush(),\n getAnonymousId: () => c.getAnonymousId(),\n getSessionId: () => c.getSessionId(),\n setSessionId: (sessionId, startedAt) => c.setSessionId(sessionId, startedAt),\n };\n}\n","import { useEffect, useRef } from 'react';\nimport type { Properties } from '@depsmith/depsmith-sdk';\nimport { useDepsmithContext } from './provider';\n\nexport interface PageViewOptions {\n /** Page name. Defaults to `document.title` at the time the view is recorded. */\n name?: string;\n properties?: Properties;\n}\n\n/**\n * Emits one `$page_view` per distinct `key` — pass whatever identifies a page in your router,\n * typically pathname + search. Router-agnostic; `@depsmith/depsmith-sdk-react/react-router`\n * wraps it for React Router.\n *\n * Does nothing when the provider's config autocaptures page views (`autocapture: true`, or an\n * object without `pageviews: false`): the SDK already emits on every History navigation, and an\n * explicit `page()` on top would count each route twice. Set `autocapture.pageviews: false` to\n * let the router drive page views instead.\n *\n * `key === undefined` skips — useful while a router is still resolving. A repeated key is ignored,\n * which also absorbs StrictMode's mount → unmount → mount effect replay.\n */\nexport function usePageView(key: string | undefined, options: PageViewOptions = {}): void {\n const { api, pageViewsAutocaptured, debug } = useDepsmithContext();\n const lastKey = useRef<string | undefined>(undefined);\n // Read through a ref so inline `{ properties: {...} }` literals don't re-fire the effect.\n const latest = useRef(options);\n latest.current = options;\n\n useEffect(() => {\n if (key === undefined || key === lastKey.current) return;\n lastKey.current = key;\n if (pageViewsAutocaptured) {\n if (debug) {\n console.warn(\n '[depsmith-sdk-react] usePageView ignored: autocapture already emits page views. ' +\n 'Set autocapture.pageviews: false to drive them from the router.'\n );\n }\n return;\n }\n api.page(latest.current.name, latest.current.properties);\n }, [key, api, pageViewsAutocaptured, debug]);\n}\n"]}
|