use-page-view 0.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Christopher S. Aondona
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/README.md ADDED
@@ -0,0 +1,208 @@
1
+ # use-page-view
2
+
3
+ A React hook for tracking page views and user engagement time. This hook provides real-time monitoring of user activity, page visibility, and time spent on pages.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/use-page-view.svg)](https://www.npmjs.com/package/use-page-view)
6
+ [![npm downloads](https://img.shields.io/npm/dm/use-page-view.svg)](https://www.npmjs.com/package/use-page-view)
7
+ [![License](https://img.shields.io/npm/l/use-page-view.svg)](https://github.com/christophersesugh/use-page-view/blob/main/LICENSE)
8
+ [![CI Status](https://img.shields.io/github/actions/workflow/status/christophersesugh/use-page-view/ci.yml)](https://github.com/christophersesugh/use-page-view/actions)
9
+ [![Coverage](https://img.shields.io/codecov/c/github/christophersesugh/use-page-view)](https://codecov.io/gh/christophersesugh/use-page-view)
10
+
11
+ ## Features
12
+
13
+ - 📊 Track time spent on pages
14
+ - 👀 Monitor user activity and page visibility
15
+ - ⏱️ Configurable tracking intervals
16
+ - 🔄 Real-time updates
17
+ - 🎯 Support for both continuous and one-time tracking
18
+ - 🛡️ TypeScript support
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ npm install use-page-view
24
+ # or
25
+ yarn add use-page-view
26
+ # or
27
+ pnpm add use-page-view
28
+ ```
29
+
30
+ ## Basic Usage
31
+
32
+ ```tsx
33
+ import { usePageView } from 'use-page-view';
34
+
35
+ function BlogPost() {
36
+ const { timeSpent, isActive } = usePageView({
37
+ pageId: 'blog-post-123',
38
+ onPageView: (data) => {
39
+ // Handle page view data
40
+ console.log(data);
41
+ },
42
+ });
43
+
44
+ return (
45
+ <div>
46
+ <div>
47
+ Time spent: {timeSpent}s {isActive ? '🟢' : '🔴'}
48
+ </div>
49
+ <article>Your content here...</article>
50
+ </div>
51
+ );
52
+ }
53
+ ```
54
+
55
+ ## API Reference
56
+
57
+ ### Parameters
58
+
59
+ The `usePageView` hook accepts an options object with the following properties:
60
+
61
+ | Parameter | Type | Required | Default | Description |
62
+ | --------------------- | ------------------------------ | -------- | ------- | ---------------------------------------------------------------------- |
63
+ | `pageId` | `string` | Yes | - | Unique identifier for the page being tracked |
64
+ | `userId` | `string` | No | - | Optional user identifier if user is logged in |
65
+ | `minTimeThreshold` | `number` | No | `5` | Minimum time in seconds before recording a view |
66
+ | `heartbeatInterval` | `number` | No | `30` | How often to send updates in seconds |
67
+ | `inactivityThreshold` | `number` | No | `30` | Time in seconds before user is considered inactive |
68
+ | `onPageView` | `(data: PageViewData) => void` | No | - | Callback function to handle page view data |
69
+ | `trackOnce` | `boolean` | No | `false` | Track only the initial view |
70
+ | `trackOnceDelay` | `number` | No | `0` | Minimum time in seconds before recording a view when trackOnce is true |
71
+
72
+ ### Return Value
73
+
74
+ The hook returns an object with the following properties:
75
+
76
+ | Property | Type | Description |
77
+ | ----------- | --------- | ------------------------------------------------ |
78
+ | `timeSpent` | `number` | Total time spent on the page in seconds |
79
+ | `isActive` | `boolean` | Whether the user is currently active on the page |
80
+
81
+ ### PageViewData Interface
82
+
83
+ The `onPageView` callback receives a `PageViewData` object with the following structure:
84
+
85
+ ```typescript
86
+ interface PageViewData {
87
+ pageId: string; // The page identifier
88
+ userId?: string; // Optional user identifier
89
+ timeSpent: number; // Time spent in seconds
90
+ isActive: boolean; // Whether the user is active
91
+ }
92
+ ```
93
+
94
+ ## Advanced Usage
95
+
96
+ ### Tracking User-Specific Views
97
+
98
+ ```tsx
99
+ function UserProfile() {
100
+ const { timeSpent, isActive } = usePageView({
101
+ pageId: 'user-profile',
102
+ userId: 'user-123', // Track for specific user
103
+ onPageView: (data) => {
104
+ // Send to your analytics service
105
+ analytics.trackPageView(data);
106
+ },
107
+ });
108
+ }
109
+ ```
110
+
111
+ ### Custom Tracking Intervals
112
+
113
+ ```tsx
114
+ function Article() {
115
+ const { timeSpent, isActive } = usePageView({
116
+ pageId: 'article-456',
117
+ minTimeThreshold: 10, // Only track after 10 seconds
118
+ heartbeatInterval: 60, // Send updates every minute
119
+ inactivityThreshold: 120, // Consider user inactive after 2 minutes
120
+ onPageView: (data) => {
121
+ // Handle page view data
122
+ },
123
+ });
124
+ }
125
+ ```
126
+
127
+ ### One-Time Tracking
128
+
129
+ ```tsx
130
+ function LandingPage() {
131
+ const { timeSpent, isActive } = usePageView({
132
+ pageId: 'landing-page',
133
+ trackOnce: true, // Only track the initial view
134
+ trackOnceDelay: 5, // Wait 5 seconds before tracking
135
+ onPageView: (data) => {
136
+ // Handle initial page view
137
+ },
138
+ });
139
+ }
140
+ ```
141
+
142
+ ### Custom Time Formatting
143
+
144
+ ```tsx
145
+ function formatTime(seconds: number): string {
146
+ const mins = Math.floor(seconds / 60);
147
+ const secs = seconds % 60;
148
+ return `${mins}:${secs.toString().padStart(2, '0')}`;
149
+ }
150
+
151
+ function BlogPost() {
152
+ const { timeSpent, isActive } = usePageView({
153
+ pageId: 'blog-post-123',
154
+ onPageView: (data) => {
155
+ // Handle page view data
156
+ },
157
+ });
158
+
159
+ return (
160
+ <div>
161
+ <div>
162
+ Time: {formatTime(timeSpent)} {isActive ? '🟢' : '🔴'}
163
+ </div>
164
+ </div>
165
+ );
166
+ }
167
+ ```
168
+
169
+ ## Best Practices
170
+
171
+ 1. **Unique Page IDs**: Always use unique identifiers for each page to ensure accurate tracking.
172
+
173
+ 2. **User Identification**: Include `userId` when tracking authenticated users for better analytics.
174
+
175
+ 3. **Performance**:
176
+
177
+ - Use appropriate `heartbeatInterval` values to balance real-time updates with performance
178
+ - Consider using `trackOnce` for pages where continuous tracking isn't needed
179
+
180
+ 4. **Error Handling**: Always implement error handling in your `onPageView` callback:
181
+
182
+ ```tsx
183
+ const handlePageView = async (data: PageViewData) => {
184
+ try {
185
+ await analytics.trackPageView(data);
186
+ } catch (error) {
187
+ console.error('Failed to track page view:', error);
188
+ // Implement fallback or retry logic
189
+ }
190
+ };
191
+ ```
192
+
193
+ ## Browser Support
194
+
195
+ The hook uses the following browser APIs:
196
+
197
+ - `document.visibilitychange`
198
+ - `window.addEventListener` for user activity events
199
+
200
+ It's compatible with all modern browsers that support these features.
201
+
202
+ ## Contributing
203
+
204
+ Contributions are welcome! Please feel free to submit a Pull Request.
205
+
206
+ ## License
207
+
208
+ MIT © [Christopher S. Aondona](https://codingsimba.com)
@@ -0,0 +1 @@
1
+ export { };