fix(login): send client timezone for analytics requests#27
fix(login): send client timezone for analytics requests#27Suyash-ka-github wants to merge 3 commits intobrowseping:mainfrom
Conversation
|
Related to: browseping/server#20 |
|
Hello @akash-kumar-dev this is the frontend changes Made for the issue 20 from server |
There was a problem hiding this comment.
Pull request overview
This PR adds client timezone information to analytics API requests to ensure accurate hourly presence and tab usage calculations on the backend. The changes send the user's timezone (obtained via JavaScript's Intl API) as a query parameter to analytics endpoints.
Changes:
- Added timezone detection using
Intl.DateTimeFormat().resolvedOptions().timeZone - Updated
fetchWeeklyTabUsageto include timezone query parameter - Updated
fetchHourlyPresenceto include timezone query parameter
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| headers: { Authorization: `Bearer ${token}` } | ||
| } | ||
| ); | ||
| return res.json(); |
There was a problem hiding this comment.
Missing await keyword before res.json(). While this doesn't break functionality (the promise is still returned correctly), it's inconsistent with other similar functions in this file (see lines 135, 153, 160, 168, 176, 211, 219, 261) that use return await res.json(). For consistency and better error handling within this function's scope, add the await keyword.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
|
|
There was a problem hiding this comment.
The timezone retrieval using Intl.DateTimeFormat().resolvedOptions().timeZone lacks error handling. If this API fails or returns undefined in some browsers or environments, the function will silently pass an undefined or malformed timezone parameter. Consider adding a try-catch block with a fallback timezone (e.g., 'UTC') or validation to ensure the timezone is valid before using it.
| const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; | |
| let timezone = 'UTC'; | |
| try { | |
| const options = typeof Intl !== 'undefined' && typeof Intl.DateTimeFormat === 'function' | |
| ? Intl.DateTimeFormat().resolvedOptions() | |
| : null; | |
| if (options && typeof options.timeZone === 'string' && options.timeZone.trim() !== '') { | |
| timezone = options.timeZone; | |
| } | |
| } catch { | |
| // Fallback to default 'UTC' timezone if retrieval fails | |
| } |
| const res = await fetch(`${BACKEND_URL}/api/analytics/presence/hourly?days=${days}`, { | ||
| headers: { Authorization: `Bearer ${token}` } | ||
| }); | ||
| const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; |
There was a problem hiding this comment.
The timezone retrieval using Intl.DateTimeFormat().resolvedOptions().timeZone lacks error handling. If this API fails or returns undefined in some browsers or environments, the function will silently pass an undefined or malformed timezone parameter. Consider adding a try-catch block with a fallback timezone (e.g., 'UTC') or validation to ensure the timezone is valid before using it.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Description
This PR updates the frontend to send the user’s local timezone with analytics API requests so that analytics data (especially hourly presence) is calculated correctly on the backend.
This change is the frontend counterpart to the server-side fix introduced in PR browseping/server#20 (
fix(analytics): correct hourly presence using user timezone).Type of Change
Related Issue
Changes Made
Intl.DateTimeFormat().resolvedOptions().timeZonefetchHourlyPresenceAPI call to includetimezoneas a query parameterTesting Done
Checklist
fix/login-send-timezone)fix:)Screenshots (if applicable)
Not applicable — no UI changes.
Additional Notes
This PR complements the backend analytics timezone fix by ensuring the frontend sends the correct user timezone without introducing additional logic on the client side.