nova-hooks 2.0.3 → 2.0.7
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/package.json +52 -52
- package/readme.md +146 -146
package/package.json
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "nova-hooks",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "Nova hooks package",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"nova",
|
|
7
|
-
"hooks"
|
|
8
|
-
],
|
|
9
|
-
"homepage": "https://github.com/mjjabarullah/nova-hooks#readme",
|
|
10
|
-
"bugs": {
|
|
11
|
-
"url": "https://github.com/mjjabarullah/nova-hooks/issues"
|
|
12
|
-
},
|
|
13
|
-
"repository": {
|
|
14
|
-
"type": "git",
|
|
15
|
-
"url": "git+https://github.com/mjjabarullah/nova-hooks.git"
|
|
16
|
-
},
|
|
17
|
-
"license": "MIT",
|
|
18
|
-
"author": "Mohamed Jabarullah",
|
|
19
|
-
"type": "module",
|
|
20
|
-
"main": "dist/index.cjs.js",
|
|
21
|
-
"module": "dist/index.es.js",
|
|
22
|
-
"types": "dist/index.d.ts",
|
|
23
|
-
"files": [
|
|
24
|
-
"dist"
|
|
25
|
-
],
|
|
26
|
-
"scripts": {
|
|
27
|
-
"build": "vite build",
|
|
28
|
-
"prepare": "vite build",
|
|
29
|
-
"test": "vitest run",
|
|
30
|
-
"test:watch": "vitest",
|
|
31
|
-
"coverage": "vitest run --coverage"
|
|
32
|
-
},
|
|
33
|
-
"dependencies": {
|
|
34
|
-
"socket.io-client": "^4.8.3"
|
|
35
|
-
},
|
|
36
|
-
"peerDependencies": {
|
|
37
|
-
"react": "^18.0.0 || ^19.0.0"
|
|
38
|
-
},
|
|
39
|
-
"devDependencies": {
|
|
40
|
-
"@testing-library/jest-dom": "^6.9.1",
|
|
41
|
-
"@testing-library/react": "^16.3.2",
|
|
42
|
-
"@types/node": "^25.6.2",
|
|
43
|
-
"@types/react": "^19.2.14",
|
|
44
|
-
"@types/react-dom": "^19.2.3",
|
|
45
|
-
"@vitest/coverage-v8": "^3.2.4",
|
|
46
|
-
"jsdom": "^27.0.1",
|
|
47
|
-
"typescript": "~6.0.3",
|
|
48
|
-
"vite": "^8.0.12",
|
|
49
|
-
"vite-plugin-dts": "^5.0.0",
|
|
50
|
-
"vitest": "^3.2.4"
|
|
51
|
-
}
|
|
52
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "nova-hooks",
|
|
3
|
+
"version": "2.0.7",
|
|
4
|
+
"description": "Nova hooks package",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"nova",
|
|
7
|
+
"hooks"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/mjjabarullah/nova-hooks#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/mjjabarullah/nova-hooks/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/mjjabarullah/nova-hooks.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Mohamed Jabarullah",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "dist/index.cjs.js",
|
|
21
|
+
"module": "dist/index.es.js",
|
|
22
|
+
"types": "dist/index.d.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "vite build",
|
|
28
|
+
"prepare": "vite build",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"test:watch": "vitest",
|
|
31
|
+
"coverage": "vitest run --coverage"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"socket.io-client": "^4.8.3"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
41
|
+
"@testing-library/react": "^16.3.2",
|
|
42
|
+
"@types/node": "^25.6.2",
|
|
43
|
+
"@types/react": "^19.2.14",
|
|
44
|
+
"@types/react-dom": "^19.2.3",
|
|
45
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
46
|
+
"jsdom": "^27.0.1",
|
|
47
|
+
"typescript": "~6.0.3",
|
|
48
|
+
"vite": "^8.0.12",
|
|
49
|
+
"vite-plugin-dts": "^5.0.0",
|
|
50
|
+
"vitest": "^3.2.4"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/readme.md
CHANGED
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
# nova-hooks
|
|
2
|
-
|
|
3
|
-
A highly-optimized, **zero-configuration** event tracking utility for capturing user interactions and page visits in React applications. Built with a custom, lightweight `EventBus` and `socket.io-client`.
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## ✨ Features
|
|
8
|
-
|
|
9
|
-
- **Zero-Config Tracking:** Automatically captures clicks on buttons, links, and interactive elements without needing manual attributes!
|
|
10
|
-
- **True Page Dwell Time:** Uses the native Page Visibility API to calculate the _actual_ active time spent on a page (pauses when the user switches tabs).
|
|
11
|
-
- **Network Optimized:** Employs a built-in 1-second debouncing and click-batching mechanism to minimize network overhead.
|
|
12
|
-
- **Ultra-Lightweight:** Zero dependencies (other than `socket.io-client`). Fully framework-agnostic core logic.
|
|
13
|
-
- **Auto-Enriched Context:** Automatically attaches `PageUrl`, `PageTitle`, `Project`, and `CreatedDate` to every single event payload.
|
|
14
|
-
- **SSR Safe:** Fully compatible with Next.js and Remix.
|
|
15
|
-
|
|
16
|
-
---
|
|
17
|
-
|
|
18
|
-
## 🚀 Getting Started
|
|
19
|
-
|
|
20
|
-
### 1. Install
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
yarn add nova-hooks
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
```bash
|
|
27
|
-
npm i nova-hooks
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### 2. Initialization
|
|
31
|
-
|
|
32
|
-
> [!IMPORTANT]
|
|
33
|
-
> Ensure the socket connection is established at the root of your application using the `connectSocket` method.
|
|
34
|
-
|
|
35
|
-
```tsx
|
|
36
|
-
import { connectSocket } from "nova-hooks";
|
|
37
|
-
import { useEffect } from "react";
|
|
38
|
-
|
|
39
|
-
const App = () => {
|
|
40
|
-
useEffect(() => {
|
|
41
|
-
connectSocket(import.meta.env.VITE_APP_SOCKET, "WolfPack");
|
|
42
|
-
}, []);
|
|
43
|
-
|
|
44
|
-
// app code...
|
|
45
|
-
};
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
---
|
|
49
|
-
|
|
50
|
-
## 🛠️ Usage Examples
|
|
51
|
-
|
|
52
|
-
### 1. Zero-Config Global Click Tracking
|
|
53
|
-
|
|
54
|
-
Place `useGlobalClickTracker` in your authenticated layout. It will automatically listen to the DOM and seamlessly track clicks on any `<button>`, `<a>`, or `cursor: pointer` element!
|
|
55
|
-
|
|
56
|
-
```tsx
|
|
57
|
-
import { useGlobalClickTracker } from "nova-hooks";
|
|
58
|
-
|
|
59
|
-
const ProtectedLayout = ({ children }) => {
|
|
60
|
-
const { user } = useAuth();
|
|
61
|
-
|
|
62
|
-
// Magic happens here! Tracks all UI clicks across the entire app.
|
|
63
|
-
useGlobalClickTracker(user?.empId, user?.roleId);
|
|
64
|
-
|
|
65
|
-
return <>{children}</>;
|
|
66
|
-
};
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
_Note: It will intelligently extract labels from `innerText`, `title`, `aria-label`, or auto-generate a fallback CSS Path!_
|
|
70
|
-
|
|
71
|
-
### 2. Explicit Tracking (Optional Overrides)
|
|
72
|
-
|
|
73
|
-
If you want to override the auto-detected name for a specific button, just attach the `data-nova-*` attributes:
|
|
74
|
-
|
|
75
|
-
```tsx
|
|
76
|
-
<button
|
|
77
|
-
data-nova-track-id="Custom Task Action"
|
|
78
|
-
data-nova-track-type={ActionType.Button}
|
|
79
|
-
>
|
|
80
|
-
Track Me Specifically
|
|
81
|
-
</button>
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### 3. Accurate Page Dwell Time
|
|
85
|
-
|
|
86
|
-
Track exactly how long a user actively stares at a specific page/component. The timer pauses if they switch tabs or minimize the browser!
|
|
87
|
-
|
|
88
|
-
```tsx
|
|
89
|
-
import { usePageTimeTracker } from "nova-hooks";
|
|
90
|
-
|
|
91
|
-
export const Dashboard = () => {
|
|
92
|
-
const { user } = useAuth();
|
|
93
|
-
|
|
94
|
-
// Tracks active dwell time and emits it automatically when the user leaves the page
|
|
95
|
-
usePageTimeTracker("Home Dashboard", user?.empId, user?.roleId);
|
|
96
|
-
|
|
97
|
-
return <div>Welcome to the Dashboard</div>;
|
|
98
|
-
};
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### 4. Manual / Imperative Tracking
|
|
102
|
-
|
|
103
|
-
If you need to track events programmatically (like inside an API success callback), use the `withEvent` method:
|
|
104
|
-
|
|
105
|
-
```tsx
|
|
106
|
-
import { withEvent, ActionType } from "nova-hooks";
|
|
107
|
-
|
|
108
|
-
const doLogin = () => {
|
|
109
|
-
api.login().then(() => {
|
|
110
|
-
withEvent({
|
|
111
|
-
Action: "Login Success",
|
|
112
|
-
ActionType: ActionType.Login,
|
|
113
|
-
EmpId: "EMP-123",
|
|
114
|
-
EmpRole: "Admin",
|
|
115
|
-
Count: 1,
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
};
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
---
|
|
122
|
-
|
|
123
|
-
## 📦 Exports & Types
|
|
124
|
-
|
|
125
|
-
| Name | Type | Description |
|
|
126
|
-
| ----------------------- | ------------------------------------------------------------ | ----------------------------------------------------------------------- |
|
|
127
|
-
| `ActionType` | `object` | Predefined enum-like values: `"Button"`, `"Menu"`, `"Login"`, `"Link"` |
|
|
128
|
-
| `PageVisitAction` | `string` | Constant string `"Page Visit"` used for tracking page visits |
|
|
129
|
-
| `connectSocket` | `(socketUrl: string, project: string) => void` | Connects to the socket server and sets the project name. |
|
|
130
|
-
| `disconnectSocket` | `() => void` | Disconnects and cleans up the active socket connection. |
|
|
131
|
-
| `withEvent` | `(eventData: EventData, callback?: Function) => void` | Emits a structured event optionally after running a callback. |
|
|
132
|
-
| `useGlobalClickTracker` | `(empId?: string, roleId?: string) => void` | React hook to automatically track global click events. |
|
|
133
|
-
| `usePageTimeTracker` | `(action: string; empId?: string; empRole?: string) => void` | React hook to track active time spent on a page via the Visibility API. |
|
|
134
|
-
|
|
135
|
-
### EventData Structure
|
|
136
|
-
|
|
137
|
-
All events are automatically enriched with `Project`, `PageUrl`, `PageTitle`, and an ISO `CreatedDate`.
|
|
138
|
-
|
|
139
|
-
| Property | Type | Description |
|
|
140
|
-
| ------------ | ----------------------------------------------------------------- | --------------------------------------------------------------- |
|
|
141
|
-
| `ActionType` | `keyof typeof ActionType` \| `typeof PageVisitAction` | Type of the action. |
|
|
142
|
-
| `Action` | `string` | Specific action performed (extracted from DOM or manually set). |
|
|
143
|
-
| `EmpId` | `string` | Unique identifier of the employee. |
|
|
144
|
-
| `EmpRole` | `string` | Role of the employee performing the action. |
|
|
145
|
-
| `Count` | `number` _(only when `ActionType` is from `ActionType`)_ | Number of rapid occurrences of the action (batched). |
|
|
146
|
-
| `Duration` | `number` _(seconds, only when `ActionType` is `PageVisitAction`)_ | Duration of the _active_ visit in seconds. |
|
|
1
|
+
# nova-hooks
|
|
2
|
+
|
|
3
|
+
A highly-optimized, **zero-configuration** event tracking utility for capturing user interactions and page visits in React applications. Built with a custom, lightweight `EventBus` and `socket.io-client`.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## ✨ Features
|
|
8
|
+
|
|
9
|
+
- **Zero-Config Tracking:** Automatically captures clicks on buttons, links, and interactive elements without needing manual attributes!
|
|
10
|
+
- **True Page Dwell Time:** Uses the native Page Visibility API to calculate the _actual_ active time spent on a page (pauses when the user switches tabs).
|
|
11
|
+
- **Network Optimized:** Employs a built-in 1-second debouncing and click-batching mechanism to minimize network overhead.
|
|
12
|
+
- **Ultra-Lightweight:** Zero dependencies (other than `socket.io-client`). Fully framework-agnostic core logic.
|
|
13
|
+
- **Auto-Enriched Context:** Automatically attaches `PageUrl`, `PageTitle`, `Project`, and `CreatedDate` to every single event payload.
|
|
14
|
+
- **SSR Safe:** Fully compatible with Next.js and Remix.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 🚀 Getting Started
|
|
19
|
+
|
|
20
|
+
### 1. Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
yarn add nova-hooks
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm i nova-hooks
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### 2. Initialization
|
|
31
|
+
|
|
32
|
+
> [!IMPORTANT]
|
|
33
|
+
> Ensure the socket connection is established at the root of your application using the `connectSocket` method.
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import { connectSocket } from "nova-hooks";
|
|
37
|
+
import { useEffect } from "react";
|
|
38
|
+
|
|
39
|
+
const App = () => {
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
connectSocket(import.meta.env.VITE_APP_SOCKET, "WolfPack");
|
|
42
|
+
}, []);
|
|
43
|
+
|
|
44
|
+
// app code...
|
|
45
|
+
};
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 🛠️ Usage Examples
|
|
51
|
+
|
|
52
|
+
### 1. Zero-Config Global Click Tracking
|
|
53
|
+
|
|
54
|
+
Place `useGlobalClickTracker` in your authenticated layout. It will automatically listen to the DOM and seamlessly track clicks on any `<button>`, `<a>`, or `cursor: pointer` element!
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { useGlobalClickTracker } from "nova-hooks";
|
|
58
|
+
|
|
59
|
+
const ProtectedLayout = ({ children }) => {
|
|
60
|
+
const { user } = useAuth();
|
|
61
|
+
|
|
62
|
+
// Magic happens here! Tracks all UI clicks across the entire app.
|
|
63
|
+
useGlobalClickTracker(user?.empId, user?.roleId);
|
|
64
|
+
|
|
65
|
+
return <>{children}</>;
|
|
66
|
+
};
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
_Note: It will intelligently extract labels from `innerText`, `title`, `aria-label`, or auto-generate a fallback CSS Path!_
|
|
70
|
+
|
|
71
|
+
### 2. Explicit Tracking (Optional Overrides)
|
|
72
|
+
|
|
73
|
+
If you want to override the auto-detected name for a specific button, just attach the `data-nova-*` attributes:
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
<button
|
|
77
|
+
data-nova-track-id="Custom Task Action"
|
|
78
|
+
data-nova-track-type={ActionType.Button}
|
|
79
|
+
>
|
|
80
|
+
Track Me Specifically
|
|
81
|
+
</button>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 3. Accurate Page Dwell Time
|
|
85
|
+
|
|
86
|
+
Track exactly how long a user actively stares at a specific page/component. The timer pauses if they switch tabs or minimize the browser!
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
import { usePageTimeTracker } from "nova-hooks";
|
|
90
|
+
|
|
91
|
+
export const Dashboard = () => {
|
|
92
|
+
const { user } = useAuth();
|
|
93
|
+
|
|
94
|
+
// Tracks active dwell time and emits it automatically when the user leaves the page
|
|
95
|
+
usePageTimeTracker("Home Dashboard", user?.empId, user?.roleId);
|
|
96
|
+
|
|
97
|
+
return <div>Welcome to the Dashboard</div>;
|
|
98
|
+
};
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 4. Manual / Imperative Tracking
|
|
102
|
+
|
|
103
|
+
If you need to track events programmatically (like inside an API success callback), use the `withEvent` method:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
import { withEvent, ActionType } from "nova-hooks";
|
|
107
|
+
|
|
108
|
+
const doLogin = () => {
|
|
109
|
+
api.login().then(() => {
|
|
110
|
+
withEvent({
|
|
111
|
+
Action: "Login Success",
|
|
112
|
+
ActionType: ActionType.Login,
|
|
113
|
+
EmpId: "EMP-123",
|
|
114
|
+
EmpRole: "Admin",
|
|
115
|
+
Count: 1,
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## 📦 Exports & Types
|
|
124
|
+
|
|
125
|
+
| Name | Type | Description |
|
|
126
|
+
| ----------------------- | ------------------------------------------------------------ | ----------------------------------------------------------------------- |
|
|
127
|
+
| `ActionType` | `object` | Predefined enum-like values: `"Button"`, `"Menu"`, `"Login"`, `"Link"` |
|
|
128
|
+
| `PageVisitAction` | `string` | Constant string `"Page Visit"` used for tracking page visits |
|
|
129
|
+
| `connectSocket` | `(socketUrl: string, project: string) => void` | Connects to the socket server and sets the project name. |
|
|
130
|
+
| `disconnectSocket` | `() => void` | Disconnects and cleans up the active socket connection. |
|
|
131
|
+
| `withEvent` | `(eventData: EventData, callback?: Function) => void` | Emits a structured event optionally after running a callback. |
|
|
132
|
+
| `useGlobalClickTracker` | `(empId?: string, roleId?: string) => void` | React hook to automatically track global click events. |
|
|
133
|
+
| `usePageTimeTracker` | `(action: string; empId?: string; empRole?: string) => void` | React hook to track active time spent on a page via the Visibility API. |
|
|
134
|
+
|
|
135
|
+
### EventData Structure
|
|
136
|
+
|
|
137
|
+
All events are automatically enriched with `Project`, `PageUrl`, `PageTitle`, and an ISO `CreatedDate`.
|
|
138
|
+
|
|
139
|
+
| Property | Type | Description |
|
|
140
|
+
| ------------ | ----------------------------------------------------------------- | --------------------------------------------------------------- |
|
|
141
|
+
| `ActionType` | `keyof typeof ActionType` \| `typeof PageVisitAction` | Type of the action. |
|
|
142
|
+
| `Action` | `string` | Specific action performed (extracted from DOM or manually set). |
|
|
143
|
+
| `EmpId` | `string` | Unique identifier of the employee. |
|
|
144
|
+
| `EmpRole` | `string` | Role of the employee performing the action. |
|
|
145
|
+
| `Count` | `number` _(only when `ActionType` is from `ActionType`)_ | Number of rapid occurrences of the action (batched). |
|
|
146
|
+
| `Duration` | `number` _(seconds, only when `ActionType` is `PageVisitAction`)_ | Duration of the _active_ visit in seconds. |
|