Skip to content

Commit

Permalink
✨ Add isActive attribute to view events
Browse files Browse the repository at this point in the history
  • Loading branch information
webNeat committed Dec 14, 2020
1 parent 3ba6e19 commit 35580e3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,46 @@ describe('rum track loading type', () => {
})
})

describe('rum track view is active', () => {
let setupBuilder: TestSetupBuilder
let handler: jasmine.Spy
let getViewEvent: (index: number) => View

beforeEach(() => {
;({ handler, getViewEvent } = spyOnViews())

setupBuilder = setup()
.withFakeClock()
.withFakeLocation('/foo')
.beforeBuild(({ location, lifeCycle }) => {
lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, handler)
return trackViews(location, lifeCycle)
})
})

afterEach(() => {
setupBuilder.cleanup()
})

it('should collect initial view as active', () => {
setupBuilder.build()
expect(getViewEvent(0).isActive).toBe(true)
})

it('should collect old view as inactive and new one as active after a route change', () => {
setupBuilder.build()
history.pushState({}, '', '/bar')
expect(getViewEvent(1).isActive).toBe(false)
expect(getViewEvent(2).isActive).toBe(true)
})

it('should collect view as active after a search change', () => {
setupBuilder.build()
history.pushState({}, '', '/foo?bar=qux')
expect(getViewEvent(1).isActive).toBe(true)
})
})

describe('rum track loading time', () => {
let setupBuilder: TestSetupBuilder
let handler: jasmine.Spy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface View {
documentVersion: number
startTime: number
duration: number
isActive: boolean
loadingTime?: number | undefined
loadingType: ViewLoadingType
cumulativeLayoutShift?: number
Expand Down Expand Up @@ -51,8 +52,8 @@ export function trackViews(location: Location, lifeCycle: LifeCycle) {
function onLocationChange() {
if (currentView.isDifferentView(location)) {
// Renew view on location changes
currentView.triggerUpdate()
currentView.end()
currentView.triggerUpdate()
currentView = newView(lifeCycle, location, ViewLoadingType.ROUTE_CHANGE, currentView.url)
} else {
currentView.updateLocation(location)
Expand All @@ -69,8 +70,8 @@ export function trackViews(location: Location, lifeCycle: LifeCycle) {

// End the current view on page unload
lifeCycle.subscribe(LifeCycleEventType.BEFORE_UNLOAD, () => {
currentView.triggerUpdate()
currentView.end()
currentView.triggerUpdate()
})

// Session keep alive
Expand Down Expand Up @@ -163,6 +164,7 @@ function newView(
startTime,
timings,
duration: (endTime === undefined ? performance.now() : endTime) - startTime,
isActive: endTime === undefined,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('viewCollection', () => {
userActionCount: 10,
},
id: 'xxx',
isActive: false,
loadingTime: 20,
loadingType: ViewLoadingType.INITIAL_LOAD,
location: {},
Expand Down Expand Up @@ -71,6 +72,7 @@ describe('viewCollection', () => {
},
firstContentfulPaint: 10 * 1e6,
firstInputDelay: 12 * 1e6,
isActive: false,
largestContentfulPaint: 10 * 1e6,
loadEvent: 10 * 1e6,
loadingTime: 20 * 1e6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function processViewUpdate(view: View) {
},
firstContentfulPaint: msToNs(view.timings.firstContentfulPaint),
firstInputDelay: msToNs(view.timings.firstInputDelay),
isActive: view.isActive,
largestContentfulPaint: msToNs(view.timings.largestContentfulPaint),
loadEvent: msToNs(view.timings.loadEvent),
loadingTime: msToNs(view.loadingTime),
Expand Down
1 change: 1 addition & 0 deletions packages/rum/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface RumViewEvent {
loadEvent?: number
loadingTime?: number
timeSpent: number
isActive: boolean
error: Count
action: Count
longTask: Count
Expand Down
1 change: 1 addition & 0 deletions packages/rum/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function createRawRumEvent(type: RumEventType, overrides?: Context): RawR
view: {
action: { count: 0 },
error: { count: 0 },
isActive: true,
loadingType: ViewLoadingType.INITIAL_LOAD,
longTask: { count: 0 },
resource: { count: 0 },
Expand Down

0 comments on commit 35580e3

Please sign in to comment.