Skip to content

Commit

Permalink
Use window.open observable
Browse files Browse the repository at this point in the history
  • Loading branch information
nulrich committed Dec 12, 2024
1 parent 518c07a commit 10be4bd
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 23 deletions.
9 changes: 9 additions & 0 deletions packages/rum-core/src/boot/startRum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function startRumStub(
sessionManager: RumSessionManager,
location: Location,
domMutationObservable: Observable<void>,
windowOpenObservable: Observable<void>,
locationChangeObservable: Observable<LocationChange>,
pageStateHistory: PageStateHistory,
reportError: (error: RawError) => void
Expand All @@ -74,6 +75,7 @@ function startRumStub(
pageStateHistory,
locationChangeObservable,
domMutationObservable,
windowOpenObservable,
() => ({
context: {},
user: {},
Expand All @@ -86,6 +88,7 @@ function startRumStub(
configuration,
location,
domMutationObservable,
windowOpenObservable,
locationChangeObservable,
startFeatureFlagContexts(lifeCycle, createCustomerDataTracker(noop)),
pageStateHistory,
Expand Down Expand Up @@ -114,6 +117,7 @@ describe('rum session', () => {
lifeCycle = new LifeCycle()
sessionManager = createRumSessionManagerMock().setId('42')
const domMutationObservable = new Observable<void>()
const windowOpenObservable = new Observable<void>()
const { locationChangeObservable } = setupLocationObserver()

serverRumEvents = collectServerEvents(lifeCycle)
Expand All @@ -123,6 +127,7 @@ describe('rum session', () => {
sessionManager,
location,
domMutationObservable,
windowOpenObservable,
locationChangeObservable,
mockPageStateHistory(),
noop
Expand Down Expand Up @@ -165,6 +170,7 @@ describe('rum session keep alive', () => {
clock = mockClock()
sessionManager = createRumSessionManagerMock().setId('1234')
const domMutationObservable = new Observable<void>()
const windowOpenObservable = new Observable<void>()
const { locationChangeObservable } = setupLocationObserver()

serverRumEvents = collectServerEvents(lifeCycle)
Expand All @@ -174,6 +180,7 @@ describe('rum session keep alive', () => {
sessionManager,
location,
domMutationObservable,
windowOpenObservable,
locationChangeObservable,
mockPageStateHistory(),
noop
Expand Down Expand Up @@ -229,6 +236,7 @@ describe('rum events url', () => {
function setupViewUrlTest() {
const sessionManager = createRumSessionManagerMock().setId('1234')
const domMutationObservable = new Observable<void>()
const windowOpenObservable = new Observable<void>()
const locationSetupResult = setupLocationObserver('http://foo.com/')
changeLocation = locationSetupResult.changeLocation

Expand All @@ -238,6 +246,7 @@ describe('rum events url', () => {
sessionManager,
locationSetupResult.fakeLocation,
domMutationObservable,
windowOpenObservable,
locationSetupResult.locationChangeObservable,
mockPageStateHistory(),
noop
Expand Down
6 changes: 6 additions & 0 deletions packages/rum-core/src/boot/startRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ExperimentalFeature,
} from '@datadog/browser-core'
import { createDOMMutationObservable } from '../browser/domMutationObservable'
import { createWindowOpenObservable } from '../browser/windowOpenObservable'
import { startRumAssembly } from '../domain/assembly'
import { startInternalContext } from '../domain/contexts/internalContext'
import { LifeCycle, LifeCycleEventType } from '../domain/lifeCycle'
Expand Down Expand Up @@ -125,6 +126,7 @@ export function startRum(
}

const domMutationObservable = createDOMMutationObservable()
const windowOpenObservable = createWindowOpenObservable()
const locationChangeObservable = createLocationChangeObservable(configuration, location)
const pageStateHistory = startPageStateHistory(configuration)
const {
Expand All @@ -141,6 +143,7 @@ export function startRum(
pageStateHistory,
locationChangeObservable,
domMutationObservable,
windowOpenObservable,
getCommonContext,
reportError
)
Expand All @@ -160,6 +163,7 @@ export function startRum(
configuration,
location,
domMutationObservable,
windowOpenObservable,
locationChangeObservable,
featureFlagContexts,
pageStateHistory,
Expand Down Expand Up @@ -233,6 +237,7 @@ export function startRumEventCollection(
pageStateHistory: PageStateHistory,
locationChangeObservable: Observable<LocationChange>,
domMutationObservable: Observable<void>,
windowOpenObservable: Observable<void>,
getCommonContext: () => CommonContext,
reportError: (error: RawError) => void
) {
Expand All @@ -242,6 +247,7 @@ export function startRumEventCollection(
const { addAction, actionContexts } = startActionCollection(
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
pageStateHistory
)
Expand Down
9 changes: 9 additions & 0 deletions packages/rum-core/src/browser/windowOpenObservable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { instrumentMethod, Observable } from '@datadog/browser-core'

export function createWindowOpenObservable() {
const observable = new Observable<void>()
instrumentMethod(window, 'open', () => {
observable.notify()
})
return observable
}
2 changes: 2 additions & 0 deletions packages/rum-core/src/domain/action/actionCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ describe('actionCollection', () => {

beforeEach(() => {
const domMutationObservable = new Observable<void>()
const windowOpenObservable = new Observable<void>()

;({ addAction } = startActionCollection(
lifeCycle,
domMutationObservable,
windowOpenObservable,
mockRumConfiguration(),
basePageStateHistory
))
Expand Down
8 changes: 7 additions & 1 deletion packages/rum-core/src/domain/action/actionCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type AutoAction = ClickAction
export function startActionCollection(
lifeCycle: LifeCycle,
domMutationObservable: Observable<void>,
windowOpenObservable: Observable<void>,
configuration: RumConfiguration,
pageStateHistory: PageStateHistory
) {
Expand All @@ -46,7 +47,12 @@ export function startActionCollection(

let actionContexts: ActionContexts = { findActionId: noop as () => undefined }
if (configuration.trackUserInteractions) {
actionContexts = trackClickActions(lifeCycle, domMutationObservable, configuration).actionContexts
actionContexts = trackClickActions(
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration
).actionContexts
}

return {
Expand Down
3 changes: 3 additions & 0 deletions packages/rum-core/src/domain/action/trackClickActions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function eventsCollector<T>() {
describe('trackClickActions', () => {
let lifeCycle: LifeCycle
let domMutationObservable: Observable<void>
let windowOpenObservable: Observable<void>
let clock: Clock

const { events, pushEvent } = eventsCollector<ClickAction>()
Expand All @@ -58,6 +59,7 @@ describe('trackClickActions', () => {
const trackClickActionsResult = trackClickActions(
lifeCycle,
domMutationObservable,
windowOpenObservable,
mockRumConfiguration(partialConfig)
)

Expand All @@ -72,6 +74,7 @@ describe('trackClickActions', () => {
lifeCycle = new LifeCycle()
clock = mockClock()
domMutationObservable = new Observable<void>()
windowOpenObservable = new Observable<void>()

button = document.createElement('button')
button.type = 'button'
Expand Down
10 changes: 8 additions & 2 deletions packages/rum-core/src/domain/action/trackClickActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const ACTION_CONTEXT_TIME_OUT_DELAY = 5 * ONE_MINUTE // arbitrary
export function trackClickActions(
lifeCycle: LifeCycle,
domMutationObservable: Observable<void>,
windowOpenObservable: Observable<void>,
configuration: RumConfiguration
) {
const history: ClickActionIdHistory = createValueHistory({ expireDelay: ACTION_CONTEXT_TIME_OUT_DELAY })
Expand All @@ -81,12 +82,13 @@ export function trackClickActions(
hadActivityOnPointerDown: () => boolean
}>(configuration, {
onPointerDown: (pointerDownEvent) =>
processPointerDown(configuration, lifeCycle, domMutationObservable, pointerDownEvent),
processPointerDown(configuration, lifeCycle, domMutationObservable, pointerDownEvent, windowOpenObservable),
onPointerUp: ({ clickActionBase, hadActivityOnPointerDown }, startEvent, getUserActivity) => {
startClickAction(
configuration,
lifeCycle,
domMutationObservable,
windowOpenObservable,
history,
stopObservable,
appendClickToClickChain,
Expand Down Expand Up @@ -131,7 +133,8 @@ function processPointerDown(
configuration: RumConfiguration,
lifeCycle: LifeCycle,
domMutationObservable: Observable<void>,
pointerDownEvent: MouseEventOnElement
pointerDownEvent: MouseEventOnElement,
windowOpenObservable: Observable<void>
) {
const nodePrivacyLevel = configuration.enablePrivacyForActionName
? getNodePrivacyLevel(pointerDownEvent.target, configuration.defaultPrivacyLevel)
Expand All @@ -148,6 +151,7 @@ function processPointerDown(
waitPageActivityEnd(
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
(pageActivityEndEvent) => {
hadActivityOnPointerDown = pageActivityEndEvent.hadActivity
Expand All @@ -164,6 +168,7 @@ function startClickAction(
configuration: RumConfiguration,
lifeCycle: LifeCycle,
domMutationObservable: Observable<void>,
windowOpenObservable: Observable<void>,
history: ClickActionIdHistory,
stopObservable: Observable<void>,
appendClickToClickChain: (click: Click) => void,
Expand All @@ -183,6 +188,7 @@ function startClickAction(
const { stop: stopWaitPageActivityEnd } = waitPageActivityEnd(
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
(pageActivityEndEvent) => {
if (pageActivityEndEvent.hadActivity && pageActivityEndEvent.end < click.startClocks.timeStamp) {
Expand Down
2 changes: 2 additions & 0 deletions packages/rum-core/src/domain/view/setupViewTest.specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface ViewTrackingContext {

export function setupViewTest({ lifeCycle, initialLocation }: ViewTrackingContext, initialViewOptions?: ViewOptions) {
const domMutationObservable = new Observable<void>()
const windowOpenObservable = new Observable<void>()
const configuration = mockRumConfiguration()
const { locationChangeObservable, changeLocation } = setupLocationObserver(initialLocation)

Expand Down Expand Up @@ -42,6 +43,7 @@ export function setupViewTest({ lifeCycle, initialLocation }: ViewTrackingContex
location,
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
locationChangeObservable,
!configuration.trackViewsManually,
Expand Down
4 changes: 4 additions & 0 deletions packages/rum-core/src/domain/view/trackViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function trackViews(
location: Location,
lifeCycle: LifeCycle,
domMutationObservable: Observable<void>,
windowOpenObservable: Observable<void>,
configuration: RumConfiguration,
locationChangeObservable: Observable<LocationChange>,
areViewsTrackedAutomatically: boolean,
Expand All @@ -112,6 +113,7 @@ export function trackViews(
const newlyCreatedView = newView(
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
location,
loadingType,
Expand Down Expand Up @@ -188,6 +190,7 @@ export function trackViews(
function newView(
lifeCycle: LifeCycle,
domMutationObservable: Observable<void>,
windowOpenObservable: Observable<void>,
configuration: RumConfiguration,
initialLocation: Location,
loadingType: ViewLoadingType,
Expand Down Expand Up @@ -249,6 +252,7 @@ function newView(
} = trackCommonViewMetrics(
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
scheduleViewUpdate,
loadingType,
Expand Down
2 changes: 2 additions & 0 deletions packages/rum-core/src/domain/view/viewCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ describe('viewCollection', () => {
) {
getReplayStatsSpy = jasmine.createSpy()
const domMutationObservable = new Observable<void>()
const pageOpenObserable = new Observable<void>()
const locationChangeObservable = new Observable<LocationChange>()

const collectionResult = startViewCollection(
lifeCycle,
mockRumConfiguration(partialConfiguration),
location,
domMutationObservable,
pageOpenObserable,
locationChangeObservable,
mockFeatureFlagContexts(partialFeatureFlagContexts),
mockPageStateHistory({
Expand Down
2 changes: 2 additions & 0 deletions packages/rum-core/src/domain/view/viewCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function startViewCollection(
configuration: RumConfiguration,
location: Location,
domMutationObservable: Observable<void>,
pageOpenObserable: Observable<void>,
locationChangeObservable: Observable<LocationChange>,
featureFlagContexts: FeatureFlagContexts,
pageStateHistory: PageStateHistory,
Expand All @@ -34,6 +35,7 @@ export function startViewCollection(
location,
lifeCycle,
domMutationObservable,
pageOpenObserable,
configuration,
locationChangeObservable,
!configuration.trackViewsManually,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface CommonViewMetrics {
export function trackCommonViewMetrics(
lifeCycle: LifeCycle,
domMutationObservable: Observable<void>,
windowOpenObservable: Observable<void>,
configuration: RumConfiguration,
scheduleViewUpdate: () => void,
loadingType: ViewLoadingType,
Expand All @@ -30,6 +31,7 @@ export function trackCommonViewMetrics(
const { stop: stopLoadingTimeTracking, setLoadEvent } = trackLoadingTime(
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
loadingType,
viewStart,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('trackLoadingTime', () => {
const lifeCycle = new LifeCycle()
let clock: Clock
let domMutationObservable: Observable<void>
let windowOpenObservable: Observable<void>
let loadingTimeCallback: jasmine.Spy<(loadingTime: Duration) => void>
let setLoadEvent: (loadEvent: Duration) => void
let stopLoadingTimeTracking: () => void
Expand All @@ -29,6 +30,7 @@ describe('trackLoadingTime', () => {
const loadingTimeTracking = trackLoadingTime(
lifeCycle,
domMutationObservable,
windowOpenObservable,
mockRumConfiguration(),
loadType,
clocksOrigin(),
Expand All @@ -41,6 +43,7 @@ describe('trackLoadingTime', () => {
beforeEach(() => {
clock = mockClock()
domMutationObservable = new Observable()
windowOpenObservable = new Observable()
loadingTimeCallback = jasmine.createSpy<(loadingTime: Duration) => void>()
})

Expand Down
Loading

0 comments on commit 10be4bd

Please sign in to comment.