nova-hooks 2.0.0 → 2.0.3

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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +2 -2
  3. package/readme.md +146 -141
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mohamed Jabarullah
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nova-hooks",
3
- "version": "2.0.0",
3
+ "version": "2.0.3",
4
4
  "description": "Nova hooks package",
5
5
  "keywords": [
6
6
  "nova",
@@ -14,7 +14,7 @@
14
14
  "type": "git",
15
15
  "url": "git+https://github.com/mjjabarullah/nova-hooks.git"
16
16
  },
17
- "license": "ISC",
17
+ "license": "MIT",
18
18
  "author": "Mohamed Jabarullah",
19
19
  "type": "module",
20
20
  "main": "dist/index.cjs.js",
package/readme.md CHANGED
@@ -1,141 +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@https://github.com/mjjabarullah/nova-hooks.git#v2.0.0
24
- ```
25
-
26
- ```bash
27
- npm i nova-hooks@https://github.com/mjjabarullah/nova-hooks.git#v2.0.0
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
- 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!
54
-
55
- ```tsx
56
- import { useGlobalClickTracker } from "nova-hooks";
57
-
58
- const ProtectedLayout = ({ children }) => {
59
- const { user } = useAuth();
60
-
61
- // Magic happens here! Tracks all UI clicks across the entire app.
62
- useGlobalClickTracker(user?.empId, user?.roleId);
63
-
64
- return <>{children}</>;
65
- };
66
- ```
67
- *Note: It will intelligently extract labels from `innerText`, `title`, `aria-label`, or auto-generate a fallback CSS Path!*
68
-
69
- ### 2. Explicit Tracking (Optional Overrides)
70
- If you want to override the auto-detected name for a specific button, just attach the `data-nova-*` attributes:
71
-
72
- ```tsx
73
- <button
74
- data-nova-track-id="Custom Task Action"
75
- data-nova-track-type={ActionType.Button}
76
- >
77
- Track Me Specifically
78
- </button>
79
- ```
80
-
81
- ### 3. Accurate Page Dwell Time
82
- Track exactly how long a user actively stares at a specific page/component. The timer pauses if they switch tabs or minimize the browser!
83
-
84
- ```tsx
85
- import { usePageTimeTracker } from "nova-hooks";
86
-
87
- export const Dashboard = () => {
88
- const { user } = useAuth();
89
-
90
- // Tracks active dwell time and emits it automatically when the user leaves the page
91
- usePageTimeTracker("Home Dashboard", user?.empId, user?.roleId);
92
-
93
- return <div>Welcome to the Dashboard</div>;
94
- };
95
- ```
96
-
97
- ### 4. Manual / Imperative Tracking
98
- If you need to track events programmatically (like inside an API success callback), use the `withEvent` method:
99
-
100
- ```tsx
101
- import { withEvent, ActionType } from "nova-hooks";
102
-
103
- const doLogin = () => {
104
- api.login()
105
- .then(() => {
106
- withEvent({
107
- Action: "Login Success",
108
- ActionType: ActionType.Login,
109
- EmpId: "EMP-123",
110
- EmpRole: "Admin",
111
- Count: 1,
112
- });
113
- });
114
- };
115
- ```
116
-
117
- ---
118
-
119
- ## 📦 Exports & Types
120
-
121
- | Name | Type | Description |
122
- | ----------------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------ |
123
- | `ActionType` | `object` | Predefined enum-like values: `"Button"`, `"Menu"`, `"Login"`, `"Link"` |
124
- | `PageVisitAction` | `string` | Constant string `"Page Visit"` used for tracking page visits |
125
- | `connectSocket` | `(socketUrl: string, project: string) => void` | Connects to the socket server and sets the project name. |
126
- | `disconnectSocket` | `() => void` | Disconnects and cleans up the active socket connection. |
127
- | `withEvent` | `(eventData: EventData, callback?: Function) => void` | Emits a structured event optionally after running a callback. |
128
- | `useGlobalClickTracker` | `(empId?: string, roleId?: string) => void` | React hook to automatically track global click events. |
129
- | `usePageTimeTracker` | `(action: string; empId?: string; empRole?: string) => void` | React hook to track active time spent on a page via the Visibility API. |
130
-
131
- ### EventData Structure
132
- All events are automatically enriched with `Project`, `PageUrl`, `PageTitle`, and an ISO `CreatedDate`.
133
-
134
- | Property | Type | Description |
135
- | ------------ | ----------------------------------------------------------------- | ------------------------------------------------------------------- |
136
- | `ActionType` | `keyof typeof ActionType` \| `typeof PageVisitAction` | Type of the action. |
137
- | `Action` | `string` | Specific action performed (extracted from DOM or manually set). |
138
- | `EmpId` | `string` | Unique identifier of the employee. |
139
- | `EmpRole` | `string` | Role of the employee performing the action. |
140
- | `Count` | `number` _(only when `ActionType` is from `ActionType`)_ | Number of rapid occurrences of the action (batched). |
141
- | `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. |