Negative `engagementDuration` Values Due to Time Synchronization Issues
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- typescript
- Domain
- analytics
Research direction
Start in src/index.ts at the engagementStart logic around line 97 and the worker-side engagementDuration calculation around line 130. Reproduce the discrepancy with a client clock four minutes ahead, then compare the resulting GA4 engagement_time_msec values in BigQuery. Done means the chosen timestamp strategy no longer produces negative or inflated engagement durations under clock differences.
Written by the indexing model from the issue text.
Description
We use Cloudflare Zaraz to integrate GA4 for event monitoring. While analyzing the underlying GA4 event data, we discovered an interesting phenomenon: a significant number of engagement_time_msec values are appearing as negative.
Root Cause Analysis
After investigating, we identified that this occurs when there's a time synchronization discrepancy between the user's device and Cloudflare Worker (Zaraz's execution environment). Specifically, negative engagement_time_msec values appear when the user's device time is ahead of the Worker time.
We conducted a test to reproduce this behavior:
- Set client time 4 minutes ahead of actual time
- Focus on a page, where client time is used to mark engagementStart
- When the user leaves the page, a
visibilitychangeevent triggers, andengagementDurationis calculated on the worker side as approximatelynow - engagementStart. In cases where the client time is ahead of the server time, this results in a negativeengagementDuration - This can be verified by querying the underlying event data in BigQuery for GA4, where
engagement_time_msecreflects this time difference
It's worth noting that the issue also affects accuracy when client time lags behind server time, resulting in inflated engagement duration values.
Suggested Solution
It might be worth considering using timestamps consistently (either all client-side or all edge-side) when calculating engagementDuration. This could help address potential issues since client-side time discrepancies are fairly common.
- Dominant language
- TypeScript
- Stars
- 27
- Forks
- 9
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from managed-components/google-analytics-4
-
User ID gets ignoredOpen
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 42/100
managed-components/google-analytics-4#39 · 1 comment · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
managed-components/google-analytics-4#38 · 1 comment · 1 reaction ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
managed-components/google-analytics-4#25 · 1 comment ·
-
Allow setting query parameters without prefix in tool settingsMay be free again @omarmosid claimed this 1130 days ago, and no pull request is open. Open
managed-components/google-analytics-4#24 · 2 comments · 1 assignee ·
All issues in managed-components/google-analytics-4
Similar issues
-
freshness freshness:observed-only
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
thoroc/context-radar#280 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
documentation story
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
bug category:billing priority:p3 track:fast
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
LanternOps/breeze#9223 ·
Maintainers usually reply within 1 day