Skip to content
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d32f6ea
Add initial Salesforce support.
BeltranBulbarellaDD May 26, 2026
c02e9bf
Guard session
BeltranBulbarellaDD May 26, 2026
2fcb2ee
Linting
BeltranBulbarellaDD May 26, 2026
0d952b5
Add Resources and APM support
BeltranBulbarellaDD May 27, 2026
d6d525e
Make getGlobalLocationHref mockable, patch plain usage of location
BeltranBulbarellaDD May 27, 2026
363201b
use wire
BeltranBulbarellaDD May 29, 2026
2655dc5
Merge branch 'main' into beltran.bulbarella/salesforce_0
BeltranBulbarellaDD May 29, 2026
6a2eb32
Refactor for comments
BeltranBulbarellaDD May 29, 2026
059c321
Merge branch 'main' into beltran.bulbarella/salesforce_0
BeltranBulbarellaDD May 29, 2026
f7581c2
Fix bundle
BeltranBulbarellaDD May 29, 2026
783c96b
Use globalObject in turn of window
BeltranBulbarellaDD May 29, 2026
a8474b6
Add isCSPEventSupported function
BeltranBulbarellaDD May 29, 2026
59232c7
Create isEventSupported and reuse it
BeltranBulbarellaDD May 29, 2026
4f0711a
linter
BeltranBulbarellaDD May 29, 2026
721c1a2
Remove config parameter from isEventSupported
BeltranBulbarellaDD May 29, 2026
41a66d7
Remove views plugin and try catch debug
BeltranBulbarellaDD May 29, 2026
045bd3c
linter
BeltranBulbarellaDD May 29, 2026
3293b46
Fix compat
BeltranBulbarellaDD May 29, 2026
1f4c882
Revert changes to instrumentMethod.ts
BeltranBulbarellaDD Jun 3, 2026
a52f0d5
Move view tracking to bundle, rename bundle
BeltranBulbarellaDD Jun 3, 2026
a56d75f
Merge branch 'main' into beltran.bulbarella/salesforce_0
BeltranBulbarellaDD Jun 3, 2026
ff6a526
Move SF packages, add README
BeltranBulbarellaDD Jun 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint-local-rules/disallowSideEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const pathsWithSideEffect = new Set([
`${packagesRoot}/logs/src/entries/main.ts`,
`${packagesRoot}/rum/src/entries/main.ts`,
`${packagesRoot}/rum-slim/src/entries/main.ts`,
`${packagesRoot}/rum-slim/src/entries/salesforce.ts`,
`${packagesRoot}/debugger/src/entries/main.ts`,
])

Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/browser/addEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const enum DOM_EVENT {
SECURITY_POLICY_VIOLATION = 'securitypolicyviolation',
SELECTION_CHANGE = 'selectionchange',
STORAGE = 'storage',
UNHANDLED_REJECTION = 'unhandledrejection',
}

interface AddEventListenerOptions {
Expand Down Expand Up @@ -146,3 +147,17 @@ export function addEventListeners<Target extends EventTarget, EventName extends
stop,
}
}

export function isEventSupported<Target extends EventTarget, EventName extends keyof EventMapFor<Target> & string>(
configuration: { allowUntrustedEvents?: boolean | undefined },
eventTarget: Target,
eventName: EventName,
listener: (event: EventMapFor<Target>[EventName] & { type: EventName }) => void
) {
try {
addEventListener(configuration, eventTarget, eventName, listener).stop()
return true
} catch {
return false
}
}
12 changes: 11 additions & 1 deletion packages/core/src/browser/cookieAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { Observable } from '../tools/observable'
import { mockable } from '../tools/mockable'
import { display } from '../tools/display'
import { generateUUID } from '../tools/utils/stringUtils'
import { noop } from '../tools/utils/functionUtils'
import type { Configuration } from '../domain/configuration'
import { addTelemetryDebug } from '../domain/telemetry'
import { globalObject } from '../tools/globalObject'
import { addEventListener, DOM_EVENT } from './addEventListener'
import { addEventListener, DOM_EVENT, isEventSupported } from './addEventListener'
import { getCookies, setCookie } from './cookie'
import type { CookieOptions } from './cookie'

Expand Down Expand Up @@ -162,3 +163,12 @@ export function createDocumentCookieAccess(
observable,
}
}

// Salesforce LWS does not support the change event of CookieStore objects. https://developer.salesforce.com/tools/lws-distortion-viewer
export function isCookieStoreSupported(configuration: Configuration): boolean {
const cookieStore = globalObject.cookieStore
if (!cookieStore) {
return false
}
return isEventSupported(configuration, cookieStore, DOM_EVENT.CHANGE, noop)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function isCookieStoreSupported(configuration: Configuration): boolean {
const cookieStore = globalObject.cookieStore
if (!cookieStore) {
return false
}
return isEventSupported(configuration, cookieStore, DOM_EVENT.CHANGE, noop)
}
export function isCookieStoreSupported(configuration: Configuration): boolean {
return globalObject.cookieStore && isEventSupported(configuration, cookieStore, DOM_EVENT.CHANGE, noop)
}

7 changes: 7 additions & 0 deletions packages/core/src/domain/error/trackRuntimeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { clocksNow } from '../../tools/utils/timeUtils'
import type { StackTrace } from '../../tools/stackTrace/computeStackTrace'
import { computeStackTraceFromOnErrorMessage } from '../../tools/stackTrace/computeStackTrace'
import { globalObject } from '../../tools/globalObject'
import { DOM_EVENT, isEventSupported } from '../../browser/addEventListener'
import { noop } from '../../tools/utils/functionUtils'
import { computeRawError, isError } from './error'
import type { RawError } from './error.types'
import { ErrorHandling, ErrorSource, NonErrorPrefix } from './error.types'
Expand Down Expand Up @@ -44,6 +46,11 @@ export function instrumentOnError(callback: UnhandledErrorCallback) {
}

export function instrumentUnhandledRejection(callback: UnhandledErrorCallback) {
// Salesforce LWS does not support the unhandledrejection event. https://developer.salesforce.com/tools/lws-distortion-viewer
if (!isEventSupported({}, window, DOM_EVENT.UNHANDLED_REJECTION, noop)) {
return { stop: noop }
}

return instrumentMethod(globalObject, 'onunhandledrejection', ({ parameters: [e] }) => {
callback(e.reason || 'Empty reason')
})
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/domain/report/reportObservable.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { toStackTraceString } from '../../tools/stackTrace/handlingStack'
import { monitor } from '../../tools/monitor'
import { mergeObservables, Observable } from '../../tools/observable'
import { addEventListener, DOM_EVENT } from '../../browser/addEventListener'
import { addEventListener, DOM_EVENT, isEventSupported } from '../../browser/addEventListener'
import { safeTruncate } from '../../tools/utils/stringUtils'
import type { Configuration } from '../configuration'
import type { RawError } from '../error/error.types'
import { ErrorHandling, ErrorSource } from '../error/error.types'
import { clocksNow } from '../../tools/utils/timeUtils'
import { noop } from '../../tools/utils/functionUtils'
import type { ReportType, InterventionReport, DeprecationReport } from './browser.types'

export const RawReportType = {
Expand Down Expand Up @@ -60,6 +61,11 @@ function createReportObservable(reportTypes: ReportType[]) {

function createCspViolationReportObservable(configuration: Configuration) {
return new Observable<RawReportError>((observable) => {
// Salesforce does not allow to add a securitypolicyviolation event listener. https://developer.salesforce.com/tools/lws-distortion-viewer
if (!isEventSupported(configuration, document, DOM_EVENT.SECURITY_POLICY_VIOLATION, noop)) {
return
}

const { stop } = addEventListener(configuration, document, DOM_EVENT.SECURITY_POLICY_VIOLATION, (event) => {
observable.notify(buildRawReportErrorFromCspViolation(event))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
areCookiesAuthorized,
createCookieStoreAccess,
createDocumentCookieAccess,
isCookieStoreSupported,
} from '../../../browser/cookieAccess'
import { globalObject } from '../../../tools/globalObject'
import { CookieApi, LEGACY_SESSION_STORE_KEY, SESSION_STORE_KEY } from './sessionStoreStrategy'
import type {
SessionStoreStrategy,
Expand All @@ -35,7 +35,7 @@ export async function selectCookieStrategy(
}

if (
mockable(globalObject.cookieStore) &&
isCookieStoreSupported(configuration) &&
(await areCookiesAuthorized(createCookieStoreAccess, cookieOptions, configuration))
) {
return { type: SessionPersistence.COOKIE, cookieOptions, cookieApi: CookieApi.COOKIE_STORE }
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export {
export { NonErrorPrefix } from './domain/error/error.types'
export type { Context, ContextArray, ContextValue } from './tools/serialisation/context'
export { getCookie, getInitCookie, setCookie, deleteCookie, resetInitCookies } from './browser/cookie'
export { isCookieStoreSupported } from './browser/cookieAccess'
export type {
CookieStore,
WeakRef,
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/tools/instrumentMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { setTimeout } from './timer'
import { callMonitored } from './monitor'
import { noop } from './utils/functionUtils'
import { createHandlingStack } from './stackTrace/handlingStack'
import { display } from './display'

/**
* Object passed to the callback of an instrumented method call. See `instrumentMethod` for more
Expand Down Expand Up @@ -117,14 +118,26 @@ export function instrumentMethod<TARGET extends { [key: string]: any }, METHOD e
return result
}

targetPrototype[method] = instrumentation as TARGET[METHOD]
// Salesforce makes History.prototype.pushState and History.prototype.replaceState readonly. https://help.salesforce.com/s/articleView?id=release-notes.rn_lws_distortions_added.htm&release=238&type=5

try {
targetPrototype[method] = instrumentation as TARGET[METHOD]
} catch (error) {
display.error(error)
return { stop: noop }
}

return {
stop: () => {
stopped = true
// If the instrumentation has been removed by a third party, keep the last one
if (targetPrototype[method] === instrumentation) {
targetPrototype[method] = original
try {
targetPrototype[method] = original
} catch (error) {
display.error(error)
// Restore can be rejected by sandboxed runtimes; the instrumentation is already stopped.
}
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion packages/rum-core/src/browser/cookieObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
DOM_EVENT,
getCookie,
globalObject,
isCookieStoreSupported,
} from '@datadog/browser-core'

export type CookieObservable = ReturnType<typeof createCookieObservable>

export function createCookieObservable(configuration: Configuration, cookieName: string) {
const detectCookieChangeStrategy = globalObject.cookieStore
const detectCookieChangeStrategy = isCookieStoreSupported(configuration)
? listenToCookieStoreChange(configuration)
: watchCookieFallback

Expand Down
11 changes: 9 additions & 2 deletions packages/rum-core/src/browser/locationChangeObservable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { addEventListener, DOM_EVENT, instrumentMethod, Observable, shallowClone } from '@datadog/browser-core'
import {
addEventListener,
DOM_EVENT,
globalObject,
instrumentMethod,
Observable,
shallowClone,
} from '@datadog/browser-core'
import type { RumConfiguration } from '../domain/configuration'

export interface LocationChange {
Expand All @@ -7,7 +14,7 @@ export interface LocationChange {
}

export function createLocationChangeObservable(configuration: RumConfiguration) {
let currentLocation = shallowClone(location)
let currentLocation = shallowClone(globalObject.location)

return new Observable<LocationChange>((observable) => {
const { stop: stopHistoryTracking } = trackHistory(configuration, onLocationChange)
Expand Down
6 changes: 4 additions & 2 deletions packages/rum-core/src/domain/contexts/urlContexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
createValueHistory,
HookNames,
DISCARDED,
mockable,
buildUrl,
mockable,
globalObject,
} from '@datadog/browser-core'
import type { LocationChange } from '../../browser/locationChangeObservable'
import type { LifeCycle } from '../lifeCycle'
Expand Down Expand Up @@ -41,8 +42,9 @@ export function startUrlContexts(
let previousViewUrl: string | undefined

lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_CREATED, ({ startClocks, url }) => {
const locationHref = mockable(location).href
const locationHref = mockable(globalObject.location).href
const viewUrl = url !== undefined ? buildUrl(url, locationHref).href : locationHref

urlContextHistory.add(
buildUrlContext({
url: viewUrl,
Expand Down
1 change: 1 addition & 0 deletions packages/rum-slim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"scripts": {
"build": "node ../../scripts/build/build-package.ts --modules --bundle datadog-rum-slim.js",
"build:bundle": "node ../../scripts/build/build-package.ts --bundle datadog-rum-slim.js",
"build:salesforce": "node ../../scripts/build/build-package.ts --bundle datadog-rum-salesforce.js --entry ./src/entries/salesforce.ts",
"prepack": "yarn build"
},
"dependencies": {
Expand Down
73 changes: 73 additions & 0 deletions packages/rum-slim/src/domain/salesforce/salesforceViewsPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { RumPlugin, RumPublicApi, ViewOptions } from '@datadog/browser-rum-core'

export interface SalesforceViewChange {
pageReference?: unknown
view?: ViewOptions
}

export interface SalesforceViewsPlugin {
plugin: RumPlugin
onPageReferenceChange: (viewChange: SalesforceViewChange) => void
}

export function createSalesforceViewsPlugin(): SalesforceViewsPlugin {
let publicApi: Pick<RumPublicApi, 'startView'> | undefined
let pendingViewChange: SalesforceViewChange | undefined
let lastPageReferenceKey: string | undefined

function onPageReferenceChange(viewChange: SalesforceViewChange) {
const pageReferenceKey = getPageReferenceKey(viewChange.pageReference)
if (pageReferenceKey && pageReferenceKey === lastPageReferenceKey) {
return
}

if (pageReferenceKey) {
lastPageReferenceKey = pageReferenceKey
}

if (!publicApi) {
pendingViewChange = viewChange
return
}

startView(viewChange)
}

function startView(viewChange: SalesforceViewChange) {
if (viewChange.view) {
publicApi?.startView(viewChange.view)
}
}

return {
plugin: {
name: 'salesforce',
onInit({ initConfiguration, publicApi: rumPublicApi }) {
initConfiguration.trackViewsManually = true
publicApi = rumPublicApi

if (pendingViewChange) {
startView(pendingViewChange)
pendingViewChange = undefined
}
},
getConfigurationTelemetry() {
return { views: true }
},
},
onPageReferenceChange,
}
}

function getPageReferenceKey(pageReference: unknown) {
if (pageReference === undefined) {
return undefined
}

try {
return JSON.stringify(pageReference)
} catch {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
return String(pageReference)
}
}
82 changes: 82 additions & 0 deletions packages/rum-slim/src/entries/salesforce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { defineGlobal, globalObject } from '@datadog/browser-core'
import type { RumPublicApi } from '@datadog/browser-rum-core'
import { makeRumPublicApi } from '@datadog/browser-rum-core'
import { makeProfilerApiStub } from '../boot/stubProfilerApi'
import { makeRecorderApiStub } from '../boot/stubRecorderApi'
import { createSalesforceViewsPlugin } from '../domain/salesforce/salesforceViewsPlugin'
import type { SalesforceViewsPlugin } from '../domain/salesforce/salesforceViewsPlugin'

export type {
User,
Account,
TraceContextInjection,
SessionPersistence,
TrackingConsent,
MatchOption,
ProxyFn,
Site,
Context,
ContextValue,
ContextArray,
RumInternalContext,
} from '@datadog/browser-core'

/**
* @deprecated Use {@link DatadogRum} instead
*/
export type RumGlobal = DatadogRum

export interface DatadogRum extends RumPublicApi {
createSalesforceViewsPlugin: () => SalesforceViewsPlugin
}

export type {
RumInitConfiguration,
RumBeforeSend,
ViewOptions,
StartRecordingOptions,
AddDurationVitalOptions,
DurationVitalOptions,
FeatureOperationOptions,
FailureReason,
ActionOptions,
ResourceOptions,
ResourceStopOptions,
TracingOption,
RumPlugin,
OnRumStartOptions,
PropagatorType,
FeatureFlagsForEvents,
MatchHeader,
CommonProperties,
RumEvent,
RumActionEvent,
RumErrorEvent,
RumLongTaskEvent,
RumResourceEvent,
RumViewEvent,
RumVitalEvent,
RumEventDomainContext,
RumViewEventDomainContext,
RumErrorEventDomainContext,
RumActionEventDomainContext,
RumVitalEventDomainContext,
RumResourceEventDomainContext,
RumLongTaskEventDomainContext,
} from '@datadog/browser-rum-core'
export { DEFAULT_TRACKED_RESOURCE_HEADERS } from '@datadog/browser-rum-core'
export { DefaultPrivacyLevel } from '@datadog/browser-core'
export { createSalesforceViewsPlugin }
export type { SalesforceViewChange, SalesforceViewsPlugin } from '../domain/salesforce/salesforceViewsPlugin'

export const datadogRum: DatadogRum = Object.assign(
makeRumPublicApi(makeRecorderApiStub(), makeProfilerApiStub(), {
sdkName: 'rum-slim',
}),
{ createSalesforceViewsPlugin }
)

interface BrowserWindow {
DD_RUM?: RumPublicApi
}
defineGlobal(globalObject as BrowserWindow, 'DD_RUM', datadogRum)
Loading
Loading