Commit 3bf0319
authored
fix(analytics): update storage handling during keepalive flush to prevent duplicate event sends (#34877)
PR Description:
## Summary
Fixes duplicate analytics event sends caused by keepalive flushes (page
unload) skipping the storage update. The stale events left in
`sessionStorage` were re-sent on the next
page load.
## Changes Made
### Frontend (SDK)
- **`dot-analytics.queue.utils.ts`**: Removed the `if (!keepalive)`
guard around storage cleanup after batch sends. Storage is now always
updated after flushing — both for
normal and keepalive flushes — since `sessionStorage` writes are
synchronous and complete before page unload.
- **`dot-analytics.queue.utils.spec.ts`**: Updated the keepalive flush
test to assert that storage **is** cleared (previously asserted it was
not), with comments explaining why.
## Technical Details
The previous implementation treated keepalive flushes (triggered during
`visibilitychange`/`beforeunload`) as a special case: it intentionally
left sent events in
`sessionStorage` as a "backup" in case the beacon request failed.
However, because `sessionStorage.removeItem()` is synchronous and
guaranteed to complete before the page
unloads, this backup was unnecessary. Worse, it caused the next page
load to pick up the stale persisted events and re-send them, resulting
in duplicate analytics data.
The fix removes the keepalive branch so that `clearStorage()` /
`persistToStorage()` always runs after a successful batch send,
regardless of the flush trigger.
## Breaking Changes
None
## Testing
- [x] Unit tests updated
- [ ] Integration tests added/updated
- [ ] Manual testing performed
- [ ] E2E tests added/updated
## Related Issues
Closes #34357
## Additional Notes
None1 parent d0b89fa commit 3bf0319
2 files changed
Lines changed: 21 additions & 14 deletions
File tree
Lines changed: 10 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
800 | 800 | | |
801 | 801 | | |
802 | 802 | | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
803 | 806 | | |
804 | 807 | | |
805 | 808 | | |
806 | | - | |
807 | | - | |
| 809 | + | |
| 810 | + | |
808 | 811 | | |
809 | 812 | | |
810 | | - | |
| 813 | + | |
811 | 814 | | |
812 | 815 | | |
813 | 816 | | |
| |||
829 | 832 | | |
830 | 833 | | |
831 | 834 | | |
832 | | - | |
833 | | - | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
834 | 839 | | |
835 | 840 | | |
836 | 841 | | |
| |||
Lines changed: 11 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
249 | 251 | | |
250 | 252 | | |
251 | 253 | | |
| |||
0 commit comments