nova-hooks 2.0.7 → 2.0.9
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/index.cjs.js +66 -12
- package/dist/index.es.js +66 -12
- package/dist/src/event-bus.d.ts +7 -0
- package/dist/src/hooks.d.ts +15 -9
- package/dist/src/socket.d.ts +16 -3
- package/package.json +1 -1
- package/readme.md +5 -0
package/dist/index.cjs.js
CHANGED
|
@@ -2,11 +2,23 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let socket_io_client = require("socket.io-client");
|
|
3
3
|
let react = require("react");
|
|
4
4
|
//#region src/socket.ts
|
|
5
|
+
/**
|
|
6
|
+
* Singleton instance of the Socket.IO client.
|
|
7
|
+
* Kept global to ensure only one connection exists per application lifecycle.
|
|
8
|
+
*/
|
|
5
9
|
var socket;
|
|
10
|
+
/**
|
|
11
|
+
* The global identifier for the current application/project.
|
|
12
|
+
* Attached to all emitted events to segregate data in the backend dashboard.
|
|
13
|
+
*/
|
|
6
14
|
var projectName;
|
|
7
15
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
16
|
+
* Initializes and establishes a real-time connection to the Nova socket server.
|
|
17
|
+
* This should typically be called once at the root of the application (e.g., App.tsx).
|
|
18
|
+
*
|
|
19
|
+
* @param socketUrl The absolute URL of the socket server to connect to
|
|
20
|
+
* @param project The unique identifier or name of the consuming application
|
|
21
|
+
* @throws {Error} If the socketUrl or project name is missing or empty
|
|
10
22
|
*/
|
|
11
23
|
var connectSocket = (socketUrl, project) => {
|
|
12
24
|
if (!socketUrl || socketUrl.trim() === "") throw new Error("Socket URL is required to connect.");
|
|
@@ -22,7 +34,8 @@ var connectSocket = (socketUrl, project) => {
|
|
|
22
34
|
});
|
|
23
35
|
};
|
|
24
36
|
/**
|
|
25
|
-
*
|
|
37
|
+
* Gracefully disconnects the active socket connection and completely
|
|
38
|
+
* destroys the local instance, freeing up resources.
|
|
26
39
|
*/
|
|
27
40
|
var disconnectSocket = () => {
|
|
28
41
|
if (socket) {
|
|
@@ -32,27 +45,53 @@ var disconnectSocket = () => {
|
|
|
32
45
|
};
|
|
33
46
|
//#endregion
|
|
34
47
|
//#region src/event-bus.ts
|
|
48
|
+
/**
|
|
49
|
+
* A lightweight custom event emitter implementation.
|
|
50
|
+
* Created to avoid bundling the bulky Node.js 'events' package.
|
|
51
|
+
*/
|
|
35
52
|
var SimpleEventEmitter = class {
|
|
36
53
|
listeners = {};
|
|
54
|
+
/**
|
|
55
|
+
* Registers a listener callback for a specific event.
|
|
56
|
+
* @param event The name of the event to listen for
|
|
57
|
+
* @param callback The function to execute when the event is emitted
|
|
58
|
+
*/
|
|
37
59
|
on(event, callback) {
|
|
38
60
|
if (!this.listeners[event]) this.listeners[event] = [];
|
|
39
61
|
this.listeners[event].push(callback);
|
|
40
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Emits an event, triggering all registered listeners synchronously.
|
|
65
|
+
* @param event The name of the event to emit
|
|
66
|
+
* @param data The payload to pass to the listener callbacks
|
|
67
|
+
*/
|
|
41
68
|
emit(event, data) {
|
|
42
69
|
if (this.listeners[event]) this.listeners[event].forEach((cb) => cb(data));
|
|
43
70
|
}
|
|
44
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* Predefined set of UI element types that can be tracked.
|
|
74
|
+
* Used to categorize standard user click interactions.
|
|
75
|
+
*/
|
|
45
76
|
var ActionType = {
|
|
46
77
|
Button: "Button",
|
|
47
78
|
Menu: "Menu",
|
|
48
79
|
Login: "Login",
|
|
49
80
|
Link: "Link"
|
|
50
81
|
};
|
|
82
|
+
/**
|
|
83
|
+
* Constant identifier representing a page dwell time tracking event.
|
|
84
|
+
*/
|
|
51
85
|
var PageVisitAction = "Page Visit";
|
|
86
|
+
/** Internal event identifier used for routing payloads within the EventBus */
|
|
52
87
|
var APP_EVENT = "APP_EVENT";
|
|
88
|
+
/** The singleton instance of the event bus */
|
|
53
89
|
var eventBus = new SimpleEventEmitter();
|
|
90
|
+
/** The socket event name used when streaming data to the backend */
|
|
54
91
|
var NOVA_USER_ACTIVITY_EVENT = "nova-user-activity";
|
|
92
|
+
/** Map storing aggregated, debounced click events keyed by their unique signature */
|
|
55
93
|
var pendingClicks = /* @__PURE__ */ new Map();
|
|
94
|
+
/** Reference to the active batching debounce timer */
|
|
56
95
|
var batchTimer = null;
|
|
57
96
|
/**
|
|
58
97
|
* Event listener for APP_EVENT that processes and enriches event data
|
|
@@ -104,6 +143,12 @@ function withEvent(eventData, callback) {
|
|
|
104
143
|
}
|
|
105
144
|
//#endregion
|
|
106
145
|
//#region src/hooks.ts
|
|
146
|
+
/**
|
|
147
|
+
* Helper utility to generate a unique CSS selector path for an HTML element.
|
|
148
|
+
* Used as a fallback identifier when a clicked element lacks semantic text or explicit tracking IDs.
|
|
149
|
+
* @param el The HTML element to generate a path for
|
|
150
|
+
* @returns A string representing the CSS selector path (e.g., 'div.container > button#submit')
|
|
151
|
+
*/
|
|
107
152
|
var getCssPath = (el) => {
|
|
108
153
|
const path = [];
|
|
109
154
|
let current = el;
|
|
@@ -120,10 +165,13 @@ var getCssPath = (el) => {
|
|
|
120
165
|
return path.join(" > ");
|
|
121
166
|
};
|
|
122
167
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
168
|
+
* React hook to track global click events and automatically emit them to the server.
|
|
169
|
+
* Attaches a single event listener to the document (event delegation) to capture clicks.
|
|
170
|
+
* It intelligently identifies interactive elements (buttons, links, pointer cursors) and
|
|
171
|
+
* extracts their labels or generates a CSS path fallback if no label is found.
|
|
172
|
+
*
|
|
173
|
+
* @param empId Optional employee ID for tracking identity
|
|
174
|
+
* @param roleId Optional role ID for tracking authorization/role
|
|
127
175
|
*/
|
|
128
176
|
var useGlobalClickTracker = (empId, roleId) => {
|
|
129
177
|
(0, react.useEffect)(() => {
|
|
@@ -165,13 +213,19 @@ var useGlobalClickTracker = (empId, roleId) => {
|
|
|
165
213
|
};
|
|
166
214
|
}, [empId, roleId]);
|
|
167
215
|
};
|
|
216
|
+
/**
|
|
217
|
+
* Minimum active dwell time (in seconds) required before a page visit event is considered valid and emitted.
|
|
218
|
+
*/
|
|
168
219
|
var DURATION_THRESHOLD = 5;
|
|
169
220
|
/**
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
221
|
+
* React hook to track accurate active page visit duration.
|
|
222
|
+
* Utilizes the native Page Visibility API to pause the timer when the user switches tabs
|
|
223
|
+
* or minimizes the browser, ensuring only actual active dwell time is recorded.
|
|
224
|
+
* Emits the payload automatically when the component unmounts.
|
|
225
|
+
*
|
|
226
|
+
* @param action The specific action name or page identifier for the visit
|
|
227
|
+
* @param empId Optional employee ID for tracking identity
|
|
228
|
+
* @param empRole Optional employee role for tracking authorization/role
|
|
175
229
|
*/
|
|
176
230
|
var usePageTimeTracker = (action, empId, empRole) => {
|
|
177
231
|
const activeTimeRef = (0, react.useRef)(0);
|
package/dist/index.es.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import { io } from "socket.io-client";
|
|
2
2
|
import { useEffect, useRef } from "react";
|
|
3
3
|
//#region src/socket.ts
|
|
4
|
+
/**
|
|
5
|
+
* Singleton instance of the Socket.IO client.
|
|
6
|
+
* Kept global to ensure only one connection exists per application lifecycle.
|
|
7
|
+
*/
|
|
4
8
|
var socket;
|
|
9
|
+
/**
|
|
10
|
+
* The global identifier for the current application/project.
|
|
11
|
+
* Attached to all emitted events to segregate data in the backend dashboard.
|
|
12
|
+
*/
|
|
5
13
|
var projectName;
|
|
6
14
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
15
|
+
* Initializes and establishes a real-time connection to the Nova socket server.
|
|
16
|
+
* This should typically be called once at the root of the application (e.g., App.tsx).
|
|
17
|
+
*
|
|
18
|
+
* @param socketUrl The absolute URL of the socket server to connect to
|
|
19
|
+
* @param project The unique identifier or name of the consuming application
|
|
20
|
+
* @throws {Error} If the socketUrl or project name is missing or empty
|
|
9
21
|
*/
|
|
10
22
|
var connectSocket = (socketUrl, project) => {
|
|
11
23
|
if (!socketUrl || socketUrl.trim() === "") throw new Error("Socket URL is required to connect.");
|
|
@@ -21,7 +33,8 @@ var connectSocket = (socketUrl, project) => {
|
|
|
21
33
|
});
|
|
22
34
|
};
|
|
23
35
|
/**
|
|
24
|
-
*
|
|
36
|
+
* Gracefully disconnects the active socket connection and completely
|
|
37
|
+
* destroys the local instance, freeing up resources.
|
|
25
38
|
*/
|
|
26
39
|
var disconnectSocket = () => {
|
|
27
40
|
if (socket) {
|
|
@@ -31,27 +44,53 @@ var disconnectSocket = () => {
|
|
|
31
44
|
};
|
|
32
45
|
//#endregion
|
|
33
46
|
//#region src/event-bus.ts
|
|
47
|
+
/**
|
|
48
|
+
* A lightweight custom event emitter implementation.
|
|
49
|
+
* Created to avoid bundling the bulky Node.js 'events' package.
|
|
50
|
+
*/
|
|
34
51
|
var SimpleEventEmitter = class {
|
|
35
52
|
listeners = {};
|
|
53
|
+
/**
|
|
54
|
+
* Registers a listener callback for a specific event.
|
|
55
|
+
* @param event The name of the event to listen for
|
|
56
|
+
* @param callback The function to execute when the event is emitted
|
|
57
|
+
*/
|
|
36
58
|
on(event, callback) {
|
|
37
59
|
if (!this.listeners[event]) this.listeners[event] = [];
|
|
38
60
|
this.listeners[event].push(callback);
|
|
39
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Emits an event, triggering all registered listeners synchronously.
|
|
64
|
+
* @param event The name of the event to emit
|
|
65
|
+
* @param data The payload to pass to the listener callbacks
|
|
66
|
+
*/
|
|
40
67
|
emit(event, data) {
|
|
41
68
|
if (this.listeners[event]) this.listeners[event].forEach((cb) => cb(data));
|
|
42
69
|
}
|
|
43
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* Predefined set of UI element types that can be tracked.
|
|
73
|
+
* Used to categorize standard user click interactions.
|
|
74
|
+
*/
|
|
44
75
|
var ActionType = {
|
|
45
76
|
Button: "Button",
|
|
46
77
|
Menu: "Menu",
|
|
47
78
|
Login: "Login",
|
|
48
79
|
Link: "Link"
|
|
49
80
|
};
|
|
81
|
+
/**
|
|
82
|
+
* Constant identifier representing a page dwell time tracking event.
|
|
83
|
+
*/
|
|
50
84
|
var PageVisitAction = "Page Visit";
|
|
85
|
+
/** Internal event identifier used for routing payloads within the EventBus */
|
|
51
86
|
var APP_EVENT = "APP_EVENT";
|
|
87
|
+
/** The singleton instance of the event bus */
|
|
52
88
|
var eventBus = new SimpleEventEmitter();
|
|
89
|
+
/** The socket event name used when streaming data to the backend */
|
|
53
90
|
var NOVA_USER_ACTIVITY_EVENT = "nova-user-activity";
|
|
91
|
+
/** Map storing aggregated, debounced click events keyed by their unique signature */
|
|
54
92
|
var pendingClicks = /* @__PURE__ */ new Map();
|
|
93
|
+
/** Reference to the active batching debounce timer */
|
|
55
94
|
var batchTimer = null;
|
|
56
95
|
/**
|
|
57
96
|
* Event listener for APP_EVENT that processes and enriches event data
|
|
@@ -103,6 +142,12 @@ function withEvent(eventData, callback) {
|
|
|
103
142
|
}
|
|
104
143
|
//#endregion
|
|
105
144
|
//#region src/hooks.ts
|
|
145
|
+
/**
|
|
146
|
+
* Helper utility to generate a unique CSS selector path for an HTML element.
|
|
147
|
+
* Used as a fallback identifier when a clicked element lacks semantic text or explicit tracking IDs.
|
|
148
|
+
* @param el The HTML element to generate a path for
|
|
149
|
+
* @returns A string representing the CSS selector path (e.g., 'div.container > button#submit')
|
|
150
|
+
*/
|
|
106
151
|
var getCssPath = (el) => {
|
|
107
152
|
const path = [];
|
|
108
153
|
let current = el;
|
|
@@ -119,10 +164,13 @@ var getCssPath = (el) => {
|
|
|
119
164
|
return path.join(" > ");
|
|
120
165
|
};
|
|
121
166
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
167
|
+
* React hook to track global click events and automatically emit them to the server.
|
|
168
|
+
* Attaches a single event listener to the document (event delegation) to capture clicks.
|
|
169
|
+
* It intelligently identifies interactive elements (buttons, links, pointer cursors) and
|
|
170
|
+
* extracts their labels or generates a CSS path fallback if no label is found.
|
|
171
|
+
*
|
|
172
|
+
* @param empId Optional employee ID for tracking identity
|
|
173
|
+
* @param roleId Optional role ID for tracking authorization/role
|
|
126
174
|
*/
|
|
127
175
|
var useGlobalClickTracker = (empId, roleId) => {
|
|
128
176
|
useEffect(() => {
|
|
@@ -164,13 +212,19 @@ var useGlobalClickTracker = (empId, roleId) => {
|
|
|
164
212
|
};
|
|
165
213
|
}, [empId, roleId]);
|
|
166
214
|
};
|
|
215
|
+
/**
|
|
216
|
+
* Minimum active dwell time (in seconds) required before a page visit event is considered valid and emitted.
|
|
217
|
+
*/
|
|
167
218
|
var DURATION_THRESHOLD = 5;
|
|
168
219
|
/**
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
220
|
+
* React hook to track accurate active page visit duration.
|
|
221
|
+
* Utilizes the native Page Visibility API to pause the timer when the user switches tabs
|
|
222
|
+
* or minimizes the browser, ensuring only actual active dwell time is recorded.
|
|
223
|
+
* Emits the payload automatically when the component unmounts.
|
|
224
|
+
*
|
|
225
|
+
* @param action The specific action name or page identifier for the visit
|
|
226
|
+
* @param empId Optional employee ID for tracking identity
|
|
227
|
+
* @param empRole Optional employee role for tracking authorization/role
|
|
174
228
|
*/
|
|
175
229
|
var usePageTimeTracker = (action, empId, empRole) => {
|
|
176
230
|
const activeTimeRef = useRef(0);
|
package/dist/src/event-bus.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Predefined set of UI element types that can be tracked.
|
|
3
|
+
* Used to categorize standard user click interactions.
|
|
4
|
+
*/
|
|
1
5
|
export declare const ActionType: {
|
|
2
6
|
readonly Button: "Button";
|
|
3
7
|
readonly Menu: "Menu";
|
|
4
8
|
readonly Login: "Login";
|
|
5
9
|
readonly Link: "Link";
|
|
6
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* Constant identifier representing a page dwell time tracking event.
|
|
13
|
+
*/
|
|
7
14
|
export declare const PageVisitAction = "Page Visit";
|
|
8
15
|
/**
|
|
9
16
|
* Represents the structure of event data that can be emitted
|
package/dist/src/hooks.d.ts
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
import { EventData } from './event-bus';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
* React hook to track global click events and automatically emit them to the server.
|
|
4
|
+
* Attaches a single event listener to the document (event delegation) to capture clicks.
|
|
5
|
+
* It intelligently identifies interactive elements (buttons, links, pointer cursors) and
|
|
6
|
+
* extracts their labels or generates a CSS path fallback if no label is found.
|
|
7
|
+
*
|
|
8
|
+
* @param empId Optional employee ID for tracking identity
|
|
9
|
+
* @param roleId Optional role ID for tracking authorization/role
|
|
7
10
|
*/
|
|
8
11
|
declare const useGlobalClickTracker: (empId?: string, roleId?: string) => void;
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
13
|
+
* React hook to track accurate active page visit duration.
|
|
14
|
+
* Utilizes the native Page Visibility API to pause the timer when the user switches tabs
|
|
15
|
+
* or minimizes the browser, ensuring only actual active dwell time is recorded.
|
|
16
|
+
* Emits the payload automatically when the component unmounts.
|
|
17
|
+
*
|
|
18
|
+
* @param action The specific action name or page identifier for the visit
|
|
19
|
+
* @param empId Optional employee ID for tracking identity
|
|
20
|
+
* @param empRole Optional employee role for tracking authorization/role
|
|
15
21
|
*/
|
|
16
22
|
declare const usePageTimeTracker: (action: EventData["Action"], empId?: EventData["EmpId"], empRole?: EventData["EmpRole"]) => void;
|
|
17
23
|
export { useGlobalClickTracker, usePageTimeTracker };
|
package/dist/src/socket.d.ts
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import { io } from 'socket.io-client';
|
|
2
|
+
/**
|
|
3
|
+
* Singleton instance of the Socket.IO client.
|
|
4
|
+
* Kept global to ensure only one connection exists per application lifecycle.
|
|
5
|
+
*/
|
|
2
6
|
export declare let socket: ReturnType<typeof io> | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* The global identifier for the current application/project.
|
|
9
|
+
* Attached to all emitted events to segregate data in the backend dashboard.
|
|
10
|
+
*/
|
|
3
11
|
export declare let projectName: string | number;
|
|
4
12
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
13
|
+
* Initializes and establishes a real-time connection to the Nova socket server.
|
|
14
|
+
* This should typically be called once at the root of the application (e.g., App.tsx).
|
|
15
|
+
*
|
|
16
|
+
* @param socketUrl The absolute URL of the socket server to connect to
|
|
17
|
+
* @param project The unique identifier or name of the consuming application
|
|
18
|
+
* @throws {Error} If the socketUrl or project name is missing or empty
|
|
7
19
|
*/
|
|
8
20
|
export declare const connectSocket: (socketUrl: string, project: string) => void;
|
|
9
21
|
/**
|
|
10
|
-
*
|
|
22
|
+
* Gracefully disconnects the active socket connection and completely
|
|
23
|
+
* destroys the local instance, freeing up resources.
|
|
11
24
|
*/
|
|
12
25
|
export declare const disconnectSocket: () => void;
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# nova-hooks
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/nova-hooks)
|
|
4
|
+
[](https://www.npmjs.com/package/nova-hooks)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://badge.socket.dev/npm/package/nova-hooks/2.0.7)
|
|
7
|
+
|
|
3
8
|
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
9
|
|
|
5
10
|
---
|