Skip to content

Commit

Permalink
👷 [RUM-6562] Enable and rename update view name API (#3099)
Browse files Browse the repository at this point in the history
* change update-view-name api naming / remove feature flag

* delete as any/eslint disable comment

* change update to set for some test/description

* chores update
  • Loading branch information
RomanGaignault authored Nov 4, 2024
1 parent 4deadef commit 2016e15
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 82 deletions.
1 change: 0 additions & 1 deletion packages/core/src/tools/experimentalFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { objectHasValue } from './utils/objectUtils'
export enum ExperimentalFeature {
WRITABLE_RESOURCE_GRAPHQL = 'writable_resource_graphql',
REMOTE_CONFIGURATION = 'remote_configuration',
UPDATE_VIEW_NAME = 'update_view_name',
LONG_ANIMATION_FRAME = 'long_animation_frame',
}

Expand Down
16 changes: 8 additions & 8 deletions packages/rum-core/src/boot/preStartRum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,16 @@ describe('preStartRum', () => {
let strategy: Strategy
let startViewSpy: jasmine.Spy<StartRumResult['startView']>
let addTimingSpy: jasmine.Spy<StartRumResult['addTiming']>
let updateViewNameSpy: jasmine.Spy<StartRumResult['updateViewName']>
let setViewNameSpy: jasmine.Spy<StartRumResult['setViewName']>

beforeEach(() => {
startViewSpy = jasmine.createSpy('startView')
addTimingSpy = jasmine.createSpy('addTiming')
updateViewNameSpy = jasmine.createSpy('updateViewName')
setViewNameSpy = jasmine.createSpy('setViewName')
doStartRumSpy.and.returnValue({
startView: startViewSpy,
addTiming: addTimingSpy,
updateViewName: updateViewNameSpy,
setViewName: setViewNameSpy,
} as unknown as StartRumResult)
strategy = createPreStartStrategy(
{},
Expand Down Expand Up @@ -741,14 +741,14 @@ describe('preStartRum', () => {
expect(setViewContextPropertySpy).toHaveBeenCalledOnceWith('foo', 'bar')
})

it('updateViewName', () => {
const updateViewNameSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ updateViewName: updateViewNameSpy } as unknown as StartRumResult)
it('setViewName', () => {
const setViewNameSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ setViewName: setViewNameSpy } as unknown as StartRumResult)

const name = 'foo'
strategy.updateViewName(name)
strategy.setViewName(name)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(updateViewNameSpy).toHaveBeenCalledOnceWith(name)
expect(setViewNameSpy).toHaveBeenCalledOnceWith(name)
})

it('addFeatureFlagEvaluation', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/src/boot/preStartRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ export function createPreStartStrategy(
}
},

updateViewName(name) {
bufferApiCalls.add((startRumResult) => startRumResult.updateViewName(name))
setViewName(name) {
bufferApiCalls.add((startRumResult) => startRumResult.setViewName(name))
},

setViewContext(context) {
Expand Down
27 changes: 9 additions & 18 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { RelativeTime, Context, DeflateWorker, CustomerDataTrackerManager, TimeStamp } from '@datadog/browser-core'
import {
ExperimentalFeature,
ONE_SECOND,
display,
DefaultPrivacyLevel,
Expand All @@ -9,7 +8,7 @@ import {
timeStampToClocks,
} from '@datadog/browser-core'
import type { Clock } from '@datadog/browser-core/test'
import { mockClock, mockExperimentalFeatures, registerCleanupTask } from '@datadog/browser-core/test'
import { mockClock, registerCleanupTask } from '@datadog/browser-core/test'
import { noopRecorderApi } from '../../test'
import { ActionType, VitalType } from '../rawRumEvent.types'
import type { DurationVitalReference } from '../domain/vital/vitalCollection'
Expand All @@ -25,7 +24,7 @@ const noopStartRum = (): ReturnType<StartRum> => ({
startView: () => undefined,
setViewContext: () => undefined,
setViewContextProperty: () => undefined,
updateViewName: () => undefined,
setViewName: () => undefined,
getInternalContext: () => undefined,
lifeCycle: {} as any,
viewHistory: {} as any,
Expand Down Expand Up @@ -836,34 +835,26 @@ describe('rum public api', () => {
expect(rumPublicApi.version).toBe('test')
})

describe('updateViewName', () => {
let updateViewNameSpy: jasmine.Spy<ReturnType<StartRum>['updateViewName']>
describe('setViewName', () => {
let setViewNameSpy: jasmine.Spy<ReturnType<StartRum>['setViewName']>
let rumPublicApi: RumPublicApi

beforeEach(() => {
updateViewNameSpy = jasmine.createSpy()
setViewNameSpy = jasmine.createSpy()
rumPublicApi = makeRumPublicApi(
() => ({
...noopStartRum(),
updateViewName: updateViewNameSpy,
setViewName: setViewNameSpy,
}),
noopRecorderApi
)
})

it('should not expose update view name api when ff is disabled', () => {
const rumPublicApi = makeRumPublicApi(noopStartRum, noopRecorderApi)
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
expect((rumPublicApi as any).updateViewName).toBeUndefined()
})

it('should update the view name', () => {
mockExperimentalFeatures([ExperimentalFeature.UPDATE_VIEW_NAME])
it('should set the view name', () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
;(rumPublicApi as any).updateViewName('foo')
rumPublicApi.setViewName('foo')

expect(updateViewNameSpy).toHaveBeenCalledWith('foo')
expect(setViewNameSpy).toHaveBeenCalledWith('foo')
})
})

Expand Down
31 changes: 14 additions & 17 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import type {
} from '@datadog/browser-core'
import {
addTelemetryUsage,
isExperimentalFeatureEnabled,
ExperimentalFeature,
CustomerDataType,
assign,
createContextManager,
Expand Down Expand Up @@ -76,6 +74,15 @@ export interface RumPublicApi extends PublicApi {
*/
setTrackingConsent: (trackingConsent: TrackingConsent) => void

/**
* Set View Name.
*
* Enable to manually change the name of the current view.
* @param name name of the view
* See [Override default RUM view names](https://docs.datadoghq.com/real_user_monitoring/browser/advanced_configuration/#override-default-rum-view-names) for further information.
*/
setViewName: (name: string) => void

/**
* Set View Context.
*
Expand Down Expand Up @@ -342,7 +349,7 @@ export interface Strategy {
stopSession: StartRumResult['stopSession']
addTiming: StartRumResult['addTiming']
startView: StartRumResult['startView']
updateViewName: StartRumResult['updateViewName']
setViewName: StartRumResult['setViewName']
setViewContext: StartRumResult['setViewContext']
setViewContextProperty: StartRumResult['setViewContextProperty']
addAction: StartRumResult['addAction']
Expand Down Expand Up @@ -427,27 +434,17 @@ export function makeRumPublicApi(
const rumPublicApi: RumPublicApi = makePublicApi<RumPublicApi>({
init: monitor((initConfiguration) => {
strategy.init(initConfiguration, rumPublicApi)

// Add experimental features here
if (isExperimentalFeatureEnabled(ExperimentalFeature.UPDATE_VIEW_NAME)) {
/**
* Update View Name.
*
* Enable to manually change the name of the current view.
* @param name name of the view
* See [Override default RUM view names](https://docs.datadoghq.com/real_user_monitoring/browser/advanced_configuration/#override-default-rum-view-names) for further information.
*/
;(rumPublicApi as any).updateViewName = monitor((name: string) => {
strategy.updateViewName(name)
})
}
}),

setTrackingConsent: monitor((trackingConsent) => {
trackingConsentState.update(trackingConsent)
addTelemetryUsage({ feature: 'set-tracking-consent', tracking_consent: trackingConsent })
}),

setViewName: monitor((name: string) => {
strategy.setViewName(name)
}),

setViewContext: monitor((context: Context) => {
strategy.setViewContext(context)
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/src/boot/startRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function startRum(
const {
addTiming,
startView,
updateViewName,
setViewName,
setViewContext,
setViewContextProperty,
stop: stopViewCollection,
Expand Down Expand Up @@ -201,7 +201,7 @@ export function startRum(
startView,
setViewContext,
setViewContextProperty,
updateViewName,
setViewName,
lifeCycle,
viewHistory,
session,
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/src/domain/view/setupViewTest.specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function setupViewTest({ lifeCycle, initialLocation }: ViewTrackingContex
} = spyOnViews<ViewEndedEvent>()
lifeCycle.subscribe(LifeCycleEventType.VIEW_ENDED, viewEndHandler)

const { stop, startView, updateViewName, setViewContext, setViewContextProperty, addTiming } = trackViews(
const { stop, startView, setViewName, setViewContext, setViewContextProperty, addTiming } = trackViews(
location,
lifeCycle,
domMutationObservable,
Expand All @@ -53,7 +53,7 @@ export function setupViewTest({ lifeCycle, initialLocation }: ViewTrackingContex
setViewContext,
setViewContextProperty,
changeLocation,
updateViewName,
setViewName,
addTiming,
getViewUpdate,
getViewUpdateCount,
Expand Down
29 changes: 8 additions & 21 deletions packages/rum-core/src/domain/view/trackViews.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import {
display,
relativeToClocks,
relativeNow,
ExperimentalFeature,
resetExperimentalFeatures,
} from '@datadog/browser-core'

import type { Clock } from '@datadog/browser-core/test'
import { mockClock, mockExperimentalFeatures, registerCleanupTask } from '@datadog/browser-core/test'
import { mockClock, registerCleanupTask } from '@datadog/browser-core/test'
import { createPerformanceEntry, mockPerformanceObserver } from '../../../test'
import { RumEventType, ViewLoadingType } from '../../rawRumEvent.types'
import type { RumEvent } from '../../rumEvent.types'
Expand Down Expand Up @@ -935,37 +934,25 @@ describe('view event count', () => {
})
})

describe('update view name', () => {
it('should update an undefined view name if the experimental feature is enabled', () => {
mockExperimentalFeatures([ExperimentalFeature.UPDATE_VIEW_NAME])
describe('set view name', () => {
it('should set an undefined view name', () => {
viewTest = setupViewTest({ lifeCycle })

const { getViewUpdate, startView, updateViewName } = viewTest
const { getViewUpdate, startView, setViewName } = viewTest

startView()
updateViewName('foo')
setViewName('foo')
expect(getViewUpdate(3).name).toEqual('foo')
})

it('should update a defined view name if the experimental feature is enabled', () => {
mockExperimentalFeatures([ExperimentalFeature.UPDATE_VIEW_NAME])
it('should set a defined view name', () => {
viewTest = setupViewTest({ lifeCycle })

const { getViewUpdate, startView, updateViewName } = viewTest
const { getViewUpdate, startView, setViewName } = viewTest

startView({ name: 'initial view name' })
updateViewName('foo')
setViewName('foo')
expect(getViewUpdate(3).name).toEqual('foo')
})

it('should not update a defined view name if the experimental feature is not enabled', () => {
viewTest = setupViewTest({ lifeCycle })

const { getViewUpdate, startView, updateViewName } = viewTest

startView({ name: 'initial view name' })
updateViewName('foo')
expect(getViewUpdate(2).name).toEqual('initial view name')
})
})
})
11 changes: 3 additions & 8 deletions packages/rum-core/src/domain/view/trackViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import {
clearInterval,
setTimeout,
Observable,
isExperimentalFeatureEnabled,
ExperimentalFeature,
createContextManager,
} from '@datadog/browser-core'
import type { ViewCustomTimings } from '../../rawRumEvent.types'
Expand Down Expand Up @@ -173,8 +171,8 @@ export function trackViews(
setViewContextProperty: (key: string, value: ContextValue) => {
currentView.contextManager.setContextProperty(key, value)
},
updateViewName: (name: string) => {
currentView.updateViewName(name)
setViewName: (name: string) => {
currentView.setViewName(name)
},

stop: () => {
Expand Down Expand Up @@ -335,10 +333,7 @@ function newView(
customTimings[sanitizeTiming(name)] = relativeTime
scheduleViewUpdate()
},
updateViewName(updatedName: string) {
if (!isExperimentalFeatureEnabled(ExperimentalFeature.UPDATE_VIEW_NAME)) {
return
}
setViewName(updatedName: string) {
name = updatedName
triggerViewUpdate()
},
Expand Down
5 changes: 2 additions & 3 deletions test/e2e/scenario/rum/init.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('API calls and events around init', () => {
})

createTest('should be associated to corresponding views when views are manually tracked')
.withRum({ trackViewsManually: true, enableExperimentalFeatures: ['update_view_name'] })
.withRum({ trackViewsManually: true })
.withRumSlim()
.withRumInit((configuration) => {
window.DD_RUM!.addError('before init')
Expand All @@ -89,8 +89,7 @@ describe('API calls and events around init', () => {
window.DD_RUM!.addError('after manual view')
window.DD_RUM!.addAction('after manual view')
window.DD_RUM!.addTiming('after manual view')
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
;(window.DD_RUM as any).updateViewName('after manual view')
window.DD_RUM!.setViewName('after manual view')
}, 40)
})
.run(async ({ intakeRegistry }) => {
Expand Down

0 comments on commit 2016e15

Please sign in to comment.