@obsrviq/react 0.3.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/LICENSE +21 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
- package/src/index.ts +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Obsrviq
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { ObsrviqInitConfig } from '@obsrviq/types';
|
|
4
|
+
|
|
5
|
+
/** The public surface of the Obsrviq client (the singleton structurally satisfies it). */
|
|
6
|
+
interface ObsrviqClient {
|
|
7
|
+
readonly sessionId: string | null;
|
|
8
|
+
init(config: ObsrviqInitConfig): void;
|
|
9
|
+
identify(userId: string, traits?: Record<string, unknown>): void;
|
|
10
|
+
setMetadata(props: Record<string, unknown>): void;
|
|
11
|
+
reset(): void;
|
|
12
|
+
track(name: string, props?: Record<string, unknown>): void;
|
|
13
|
+
/** Mark a client-defined conversion by name (e.g. "checkout", "signup"). */
|
|
14
|
+
conversion(name: string, props?: Record<string, unknown>): void;
|
|
15
|
+
/** Attach free-form, searchable labels to the current session. */
|
|
16
|
+
tag(...names: string[]): void;
|
|
17
|
+
/** Open a named sub-event span; returns a handle whose end() closes it. */
|
|
18
|
+
startTask(name: string, props?: Record<string, unknown>): {
|
|
19
|
+
end: (endProps?: Record<string, unknown>) => void;
|
|
20
|
+
};
|
|
21
|
+
/** Close a named sub-event span opened by startTask(name). */
|
|
22
|
+
endTask(name: string, props?: Record<string, unknown>): void;
|
|
23
|
+
setConsent(granted: boolean): void;
|
|
24
|
+
flush(): Promise<boolean>;
|
|
25
|
+
stop(): void;
|
|
26
|
+
}
|
|
27
|
+
interface ObsrviqProviderProps extends Omit<ObsrviqInitConfig, 'userId' | 'metadata'> {
|
|
28
|
+
/** Raw application user id. Re-applied via identify() whenever it changes
|
|
29
|
+
* (e.g. it resolves after login). */
|
|
30
|
+
userId?: string;
|
|
31
|
+
/** Session metadata. Re-merged via setMetadata() whenever it changes. */
|
|
32
|
+
metadata?: Record<string, unknown>;
|
|
33
|
+
children: ReactNode;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Initialises the Obsrviq tracker once and keeps identity in sync with props.
|
|
37
|
+
*
|
|
38
|
+
* Late identity is first-class: pass `userId` once it resolves after login and
|
|
39
|
+
* the provider calls Obsrviq.identify(), which retro-stamps the already-running
|
|
40
|
+
* session. Safe under React 18 StrictMode (double-mount) and alongside a prior
|
|
41
|
+
* script-tag init — Obsrviq.init() is idempotent, so the first init wins and any
|
|
42
|
+
* later identity is applied via identify()/setMetadata().
|
|
43
|
+
*/
|
|
44
|
+
declare function ObsrviqProvider({ children, userId, metadata, ...config }: ObsrviqProviderProps): react.FunctionComponentElement<react.ProviderProps<ObsrviqClient | null>>;
|
|
45
|
+
/** Access the Obsrviq client (identify / setMetadata / track / reset / setConsent). */
|
|
46
|
+
declare function useObsrviq(): ObsrviqClient;
|
|
47
|
+
|
|
48
|
+
export { type ObsrviqClient, ObsrviqProvider, type ObsrviqProviderProps, useObsrviq };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
createElement,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef
|
|
8
|
+
} from "react";
|
|
9
|
+
import { Obsrviq } from "@obsrviq/tracker";
|
|
10
|
+
var client = Obsrviq;
|
|
11
|
+
var ObsrviqContext = createContext(null);
|
|
12
|
+
function ObsrviqProvider({ children, userId, metadata, ...config }) {
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
Obsrviq.init(config);
|
|
15
|
+
if (userId != null) Obsrviq.identify(userId, metadata);
|
|
16
|
+
else if (metadata) Obsrviq.setMetadata(metadata);
|
|
17
|
+
}, []);
|
|
18
|
+
const metaKey = metadata ? JSON.stringify(metadata) : "";
|
|
19
|
+
const mounted = useRef(false);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (!mounted.current) {
|
|
22
|
+
mounted.current = true;
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (userId != null) Obsrviq.identify(userId, metadata);
|
|
26
|
+
else if (metadata) Obsrviq.setMetadata(metadata);
|
|
27
|
+
}, [userId, metaKey]);
|
|
28
|
+
return createElement(ObsrviqContext.Provider, { value: client }, children);
|
|
29
|
+
}
|
|
30
|
+
function useObsrviq() {
|
|
31
|
+
const ctx = useContext(ObsrviqContext);
|
|
32
|
+
if (!ctx) throw new Error("useObsrviq() must be used within a <ObsrviqProvider>.");
|
|
33
|
+
return ctx;
|
|
34
|
+
}
|
|
35
|
+
export {
|
|
36
|
+
ObsrviqProvider,
|
|
37
|
+
useObsrviq
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n createContext,\n createElement,\n useContext,\n useEffect,\n useRef,\n type ReactNode,\n} from 'react';\nimport { Obsrviq } from '@obsrviq/tracker';\nimport type { ObsrviqInitConfig } from '@obsrviq/types';\n\n/** The public surface of the Obsrviq client (the singleton structurally satisfies it). */\nexport interface ObsrviqClient {\n readonly sessionId: string | null;\n init(config: ObsrviqInitConfig): void;\n identify(userId: string, traits?: Record<string, unknown>): void;\n setMetadata(props: Record<string, unknown>): void;\n reset(): void;\n track(name: string, props?: Record<string, unknown>): void;\n /** Mark a client-defined conversion by name (e.g. \"checkout\", \"signup\"). */\n conversion(name: string, props?: Record<string, unknown>): void;\n /** Attach free-form, searchable labels to the current session. */\n tag(...names: string[]): void;\n /** Open a named sub-event span; returns a handle whose end() closes it. */\n startTask(name: string, props?: Record<string, unknown>): { end: (endProps?: Record<string, unknown>) => void };\n /** Close a named sub-event span opened by startTask(name). */\n endTask(name: string, props?: Record<string, unknown>): void;\n setConsent(granted: boolean): void;\n flush(): Promise<boolean>;\n stop(): void;\n}\n\nconst client: ObsrviqClient = Obsrviq;\nconst ObsrviqContext = createContext<ObsrviqClient | null>(null);\n\nexport interface ObsrviqProviderProps extends Omit<ObsrviqInitConfig, 'userId' | 'metadata'> {\n /** Raw application user id. Re-applied via identify() whenever it changes\n * (e.g. it resolves after login). */\n userId?: string;\n /** Session metadata. Re-merged via setMetadata() whenever it changes. */\n metadata?: Record<string, unknown>;\n children: ReactNode;\n}\n\n/**\n * Initialises the Obsrviq tracker once and keeps identity in sync with props.\n *\n * Late identity is first-class: pass `userId` once it resolves after login and\n * the provider calls Obsrviq.identify(), which retro-stamps the already-running\n * session. Safe under React 18 StrictMode (double-mount) and alongside a prior\n * script-tag init — Obsrviq.init() is idempotent, so the first init wins and any\n * later identity is applied via identify()/setMetadata().\n */\nexport function ObsrviqProvider({ children, userId, metadata, ...config }: ObsrviqProviderProps) {\n // Init once (idempotent in the tracker; StrictMode double-invoke is harmless).\n useEffect(() => {\n Obsrviq.init(config as ObsrviqInitConfig);\n if (userId != null) Obsrviq.identify(userId, metadata);\n else if (metadata) Obsrviq.setMetadata(metadata);\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n // Sync identity on subsequent changes (login, plan change, …).\n const metaKey = metadata ? JSON.stringify(metadata) : '';\n const mounted = useRef(false);\n useEffect(() => {\n if (!mounted.current) {\n mounted.current = true;\n return; // mount run already handled above\n }\n if (userId != null) Obsrviq.identify(userId, metadata);\n else if (metadata) Obsrviq.setMetadata(metadata);\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [userId, metaKey]);\n\n return createElement(ObsrviqContext.Provider, { value: client }, children);\n}\n\n/** Access the Obsrviq client (identify / setMetadata / track / reset / setConsent). */\nexport function useObsrviq(): ObsrviqClient {\n const ctx = useContext(ObsrviqContext);\n if (!ctx) throw new Error('useObsrviq() must be used within a <ObsrviqProvider>.');\n return ctx;\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,eAAe;AAwBxB,IAAM,SAAwB;AAC9B,IAAM,iBAAiB,cAAoC,IAAI;AAoBxD,SAAS,gBAAgB,EAAE,UAAU,QAAQ,UAAU,GAAG,OAAO,GAAyB;AAE/F,YAAU,MAAM;AACd,YAAQ,KAAK,MAA2B;AACxC,QAAI,UAAU,KAAM,SAAQ,SAAS,QAAQ,QAAQ;AAAA,aAC5C,SAAU,SAAQ,YAAY,QAAQ;AAAA,EAEjD,GAAG,CAAC,CAAC;AAGL,QAAM,UAAU,WAAW,KAAK,UAAU,QAAQ,IAAI;AACtD,QAAM,UAAU,OAAO,KAAK;AAC5B,YAAU,MAAM;AACd,QAAI,CAAC,QAAQ,SAAS;AACpB,cAAQ,UAAU;AAClB;AAAA,IACF;AACA,QAAI,UAAU,KAAM,SAAQ,SAAS,QAAQ,QAAQ;AAAA,aAC5C,SAAU,SAAQ,YAAY,QAAQ;AAAA,EAEjD,GAAG,CAAC,QAAQ,OAAO,CAAC;AAEpB,SAAO,cAAc,eAAe,UAAU,EAAE,OAAO,OAAO,GAAG,QAAQ;AAC3E;AAGO,SAAS,aAA4B;AAC1C,QAAM,MAAM,WAAW,cAAc;AACrC,MAAI,CAAC,IAAK,OAAM,IAAI,MAAM,uDAAuD;AACjF,SAAO;AACT;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@obsrviq/react",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"description": "React adapter for the Obsrviq tracker — <ObsrviqProvider> + useObsrviq().",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
22
|
+
],
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^18.0.0"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@obsrviq/tracker": "0.3.0",
|
|
28
|
+
"@obsrviq/types": "0.3.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/react": "^18.3.17",
|
|
32
|
+
"react": "^18.3.1",
|
|
33
|
+
"tsup": "^8.3.5",
|
|
34
|
+
"typescript": "^5.7.2"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup",
|
|
38
|
+
"dev": "tsup --watch",
|
|
39
|
+
"typecheck": "tsc --noEmit"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
createElement,
|
|
4
|
+
useContext,
|
|
5
|
+
useEffect,
|
|
6
|
+
useRef,
|
|
7
|
+
type ReactNode,
|
|
8
|
+
} from 'react';
|
|
9
|
+
import { Obsrviq } from '@obsrviq/tracker';
|
|
10
|
+
import type { ObsrviqInitConfig } from '@obsrviq/types';
|
|
11
|
+
|
|
12
|
+
/** The public surface of the Obsrviq client (the singleton structurally satisfies it). */
|
|
13
|
+
export interface ObsrviqClient {
|
|
14
|
+
readonly sessionId: string | null;
|
|
15
|
+
init(config: ObsrviqInitConfig): void;
|
|
16
|
+
identify(userId: string, traits?: Record<string, unknown>): void;
|
|
17
|
+
setMetadata(props: Record<string, unknown>): void;
|
|
18
|
+
reset(): void;
|
|
19
|
+
track(name: string, props?: Record<string, unknown>): void;
|
|
20
|
+
/** Mark a client-defined conversion by name (e.g. "checkout", "signup"). */
|
|
21
|
+
conversion(name: string, props?: Record<string, unknown>): void;
|
|
22
|
+
/** Attach free-form, searchable labels to the current session. */
|
|
23
|
+
tag(...names: string[]): void;
|
|
24
|
+
/** Open a named sub-event span; returns a handle whose end() closes it. */
|
|
25
|
+
startTask(name: string, props?: Record<string, unknown>): { end: (endProps?: Record<string, unknown>) => void };
|
|
26
|
+
/** Close a named sub-event span opened by startTask(name). */
|
|
27
|
+
endTask(name: string, props?: Record<string, unknown>): void;
|
|
28
|
+
setConsent(granted: boolean): void;
|
|
29
|
+
flush(): Promise<boolean>;
|
|
30
|
+
stop(): void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const client: ObsrviqClient = Obsrviq;
|
|
34
|
+
const ObsrviqContext = createContext<ObsrviqClient | null>(null);
|
|
35
|
+
|
|
36
|
+
export interface ObsrviqProviderProps extends Omit<ObsrviqInitConfig, 'userId' | 'metadata'> {
|
|
37
|
+
/** Raw application user id. Re-applied via identify() whenever it changes
|
|
38
|
+
* (e.g. it resolves after login). */
|
|
39
|
+
userId?: string;
|
|
40
|
+
/** Session metadata. Re-merged via setMetadata() whenever it changes. */
|
|
41
|
+
metadata?: Record<string, unknown>;
|
|
42
|
+
children: ReactNode;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Initialises the Obsrviq tracker once and keeps identity in sync with props.
|
|
47
|
+
*
|
|
48
|
+
* Late identity is first-class: pass `userId` once it resolves after login and
|
|
49
|
+
* the provider calls Obsrviq.identify(), which retro-stamps the already-running
|
|
50
|
+
* session. Safe under React 18 StrictMode (double-mount) and alongside a prior
|
|
51
|
+
* script-tag init — Obsrviq.init() is idempotent, so the first init wins and any
|
|
52
|
+
* later identity is applied via identify()/setMetadata().
|
|
53
|
+
*/
|
|
54
|
+
export function ObsrviqProvider({ children, userId, metadata, ...config }: ObsrviqProviderProps) {
|
|
55
|
+
// Init once (idempotent in the tracker; StrictMode double-invoke is harmless).
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
Obsrviq.init(config as ObsrviqInitConfig);
|
|
58
|
+
if (userId != null) Obsrviq.identify(userId, metadata);
|
|
59
|
+
else if (metadata) Obsrviq.setMetadata(metadata);
|
|
60
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
61
|
+
}, []);
|
|
62
|
+
|
|
63
|
+
// Sync identity on subsequent changes (login, plan change, …).
|
|
64
|
+
const metaKey = metadata ? JSON.stringify(metadata) : '';
|
|
65
|
+
const mounted = useRef(false);
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (!mounted.current) {
|
|
68
|
+
mounted.current = true;
|
|
69
|
+
return; // mount run already handled above
|
|
70
|
+
}
|
|
71
|
+
if (userId != null) Obsrviq.identify(userId, metadata);
|
|
72
|
+
else if (metadata) Obsrviq.setMetadata(metadata);
|
|
73
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
74
|
+
}, [userId, metaKey]);
|
|
75
|
+
|
|
76
|
+
return createElement(ObsrviqContext.Provider, { value: client }, children);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Access the Obsrviq client (identify / setMetadata / track / reset / setConsent). */
|
|
80
|
+
export function useObsrviq(): ObsrviqClient {
|
|
81
|
+
const ctx = useContext(ObsrviqContext);
|
|
82
|
+
if (!ctx) throw new Error('useObsrviq() must be used within a <ObsrviqProvider>.');
|
|
83
|
+
return ctx;
|
|
84
|
+
}
|