-
Notifications
You must be signed in to change notification settings - Fork 49
feat: detect URL changes #1192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: detect URL changes #1192
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: URL Tracker Initialization Overwrites Without Cleanup
The initializeUrlTracking
method creates a new URLTracker
instance without stopping the existing one. Repeated calls to init()
or teardownEventListeners(false)
result in multiple URLTracker
instances running simultaneously, leading to duplicate URL change events, memory leaks, and potential incorrect behavior. The method should stop the existing URLTracker
before creating a new instance.
packages/session-replay-browser/src/session-replay.ts#L112-L126
Amplitude-TypeScript/packages/session-replay-browser/src/session-replay.ts
Lines 112 to 126 in 47e1edf
private initializeUrlTracking = () => { | |
if (!this.config) return; | |
// Initialize URLTracker with configuration | |
this.urlTracker = new URLTracker({ | |
ugcFilterRules: this.config.interactionConfig?.ugcFilterRules || [], | |
enablePolling: this.config.enableUrlChangePolling || false, | |
}); | |
// Start tracking and handle URL changes | |
this.urlTracker.start((event) => { | |
void this.addCustomRRWebEvent(CustomRRwebEvent.URL_CHANGE, event); | |
}); | |
}; |
Comment bugbot run
to trigger another review on this PR
Was this report helpful? Give feedback by reacting with 👍 or 👎
📝 Documentation updates detected! New suggestion: Document URL change tracking for Session Replay |
METADATA = 'metadata', | ||
URL_CHANGE = 'url-change', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we reuse this metadata?
* Emit a URL change event if the URL has actually changed | ||
* Uses arrow function to preserve 'this' context when called from patched methods | ||
*/ | ||
private emitUrlChange = (): void => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we emit the screen size? (viewport)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, especially since we chatted through most of this. I just would want to add make sure things are changeable in config before we ship it.
export type URLChangeCallback = (event: URLChangeEvent) => void; | ||
|
||
// Constants | ||
const POLLING_INTERVAL_MS = 1000; // Check for URL changes every second when polling is enabled |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this a config instead?
Summary
URL Tracking Feature Changes
Overview
This document outlines the comprehensive changes made to the URL tracking feature in the session-replay-browser package. The changes include converting dynamic imports to static imports, developing comprehensive test coverage, refactoring code for better maintainability, and adding detailed documentation.
Table of Contents
1. Dynamic Import to Static Import Conversion
Changes Made
File:
packages/session-replay-browser/src/session-replay.ts
Before
After
Implementation Details
async
keyword frominitializeUrlTracking
methodawait import('./observers')
dynamic importvoid
wrapper since method is no longer asyncBenefits
2. Comprehensive Test Development
New Test File Created
File:
packages/session-replay-browser/test/url-tracker.test.ts
Test Coverage Categories
Constructor Tests
Lifecycle Management
URL Change Detection
UGC (User Generated Content) Filtering
getPageUrl
helper functionPolling Mechanism
Configuration Updates
Edge Case Handling
Integration with Session Replay
File:
packages/session-replay-browser/test/session-replay.test.ts
Added comprehensive integration tests:
3. Code Refactoring
URLTracker Implementation Refactoring
File:
packages/session-replay-browser/src/observers/url-tracker.ts
Key Improvements
Constants Extraction
Type Safety Enhancements
Method Decomposition
Before: Large monolithic methods
After: Focused, single-responsibility methods
Setup Method Breakdown
setupUrlTracking()
→ Main orchestrationstoreOriginalHistoryMethods()
→ Method storagepatchHistoryMethods()
→ History API patchingsetupEventListeners()
→ Event listener setupsetupPolling()
→ Polling configurationTeardown Method Breakdown
teardownUrlTracking()
→ Main orchestrationrestoreOriginalHistoryMethods()
→ Method restorationremoveEventListeners()
→ Event cleanupclearPolling()
→ Polling cleanupresetState()
→ State resetCode Duplication Elimination
Generic History Method Patching:
Helper Method Addition
Benefits of Refactoring
globalThis
usageTest Refactoring
File:
packages/session-replay-browser/test/url-tracker.test.ts
Test Helper Functions Created
Benefits of Test Refactoring
4. Documentation Enhancements
Comprehensive Code Documentation
File:
packages/session-replay-browser/src/observers/url-tracker.ts
Documentation Added
Class-Level Documentation
Method Documentation
Property Organization
Inline Comments
Documentation Benefits
5. Performance and Maintainability Benefits
Performance Improvements
Static Import Benefits
Refactoring Benefits
Maintainability Improvements
Code Structure
Testing
Documentation
6. Testing Summary
Coverage Metrics
Final Coverage Results:
Test Structure
URL Tracker Tests (
url-tracker.test.ts
)Session Replay Integration Tests (
session-replay.test.ts
)Test Quality Improvements
Before Optimization
After Optimization
Edge Cases Covered
Browser API Availability
Method State Management
URL Change Scenarios
Configuration Edge Cases
Summary
The URL tracking feature has undergone comprehensive improvements across multiple dimensions:
✅ Code Quality
✅ Performance
✅ Maintainability
✅ Developer Experience
These changes establish a solid foundation for the URL tracking feature that is performant, maintainable, and thoroughly tested.
Checklist