|
| 1 | +import { buildUrl, instrumentMethod, noop, setTimeout } from '@datadog/browser-core' |
| 2 | +import type { RumPublicApi, ViewOptions } from '@datadog/browser-rum-core' |
| 3 | + |
| 4 | +export interface SalesforceLocation { |
| 5 | + pathname?: string |
| 6 | + href?: string |
| 7 | +} |
| 8 | + |
| 9 | +interface StartSalesforceViewNameTrackingOptions { |
| 10 | + getRumPublicApi: () => Pick<RumPublicApi, 'setViewName' | 'startView'> | undefined |
| 11 | + getLocation?: () => SalesforceLocation | undefined |
| 12 | +} |
| 13 | + |
| 14 | +interface SalesforceView { |
| 15 | + key: string |
| 16 | + url?: string |
| 17 | +} |
| 18 | + |
| 19 | +export function startSalesforceViewNameTracking(options: StartSalesforceViewNameTrackingOptions) { |
| 20 | + const getLocation = options.getLocation ?? getNavigationLocation |
| 21 | + const initialView = resolveCurrentView(getLocation()) |
| 22 | + let lastViewKey = initialView?.key |
| 23 | + |
| 24 | + if (initialView) { |
| 25 | + setCurrentViewName(initialView) |
| 26 | + } |
| 27 | + |
| 28 | + const { stop: stopInstrumentingPushState } = instrumentMethod( |
| 29 | + getHistoryInstrumentationTarget('pushState'), |
| 30 | + 'pushState', |
| 31 | + ({ onPostCall }) => { |
| 32 | + onPostCall(scheduleSetCurrentViewName) |
| 33 | + } |
| 34 | + ) |
| 35 | + const { stop: stopInstrumentingReplaceState } = instrumentMethod( |
| 36 | + getHistoryInstrumentationTarget('replaceState'), |
| 37 | + 'replaceState', |
| 38 | + ({ onPostCall }) => { |
| 39 | + onPostCall(scheduleSetCurrentViewName) |
| 40 | + } |
| 41 | + ) |
| 42 | + |
| 43 | + window.addEventListener('popstate', scheduleSetCurrentViewName) |
| 44 | + window.addEventListener('hashchange', scheduleSetCurrentViewName) |
| 45 | + window.addEventListener('click', scheduleLocationCheckAfterClick, true) |
| 46 | + |
| 47 | + function scheduleLocationCheckAfterClick() { |
| 48 | + setTimeout(trackCurrentView, 0) |
| 49 | + setTimeout(trackCurrentView, 100) |
| 50 | + setTimeout(trackCurrentView, 500) |
| 51 | + } |
| 52 | + |
| 53 | + function scheduleSetCurrentViewName() { |
| 54 | + setTimeout(trackCurrentView, 0) |
| 55 | + } |
| 56 | + |
| 57 | + function trackCurrentView() { |
| 58 | + const currentView = resolveCurrentView(getLocation()) |
| 59 | + |
| 60 | + if (!currentView) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + if (!lastViewKey || currentView.key === lastViewKey) { |
| 65 | + setCurrentViewName(currentView) |
| 66 | + lastViewKey = currentView.key |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + options.getRumPublicApi()?.startView(toViewOptions(currentView)) |
| 71 | + lastViewKey = currentView.key |
| 72 | + } |
| 73 | + |
| 74 | + function setCurrentViewName(view: SalesforceView) { |
| 75 | + options.getRumPublicApi()?.setViewName(view.key) |
| 76 | + } |
| 77 | + |
| 78 | + return { |
| 79 | + stop() { |
| 80 | + stopInstrumentingPushState() |
| 81 | + stopInstrumentingReplaceState() |
| 82 | + window.removeEventListener('popstate', scheduleSetCurrentViewName) |
| 83 | + window.removeEventListener('hashchange', scheduleSetCurrentViewName) |
| 84 | + window.removeEventListener('click', scheduleLocationCheckAfterClick, true) |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function getNavigationLocation(): SalesforceLocation | undefined { |
| 90 | + try { |
| 91 | + return { |
| 92 | + href: window.location.href, |
| 93 | + pathname: window.location.pathname, |
| 94 | + } |
| 95 | + } catch { |
| 96 | + return undefined |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function resolveCurrentView(location: SalesforceLocation | undefined): SalesforceView | undefined { |
| 101 | + if (!location) { |
| 102 | + return undefined |
| 103 | + } |
| 104 | + |
| 105 | + const url = normalizeLocationHref(location.href) |
| 106 | + const key = normalizePathname(location.pathname) ?? getPathnameFromHref(url) |
| 107 | + |
| 108 | + if (!key) { |
| 109 | + return undefined |
| 110 | + } |
| 111 | + |
| 112 | + return { |
| 113 | + key, |
| 114 | + url, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +function toViewOptions(view: SalesforceView): ViewOptions { |
| 119 | + return view.url ? { name: view.key, url: view.url } : { name: view.key } |
| 120 | +} |
| 121 | + |
| 122 | +function getPathnameFromHref(href: string | undefined) { |
| 123 | + if (!href) { |
| 124 | + return undefined |
| 125 | + } |
| 126 | + |
| 127 | + try { |
| 128 | + return normalizePathname(buildUrl(href).pathname) |
| 129 | + } catch { |
| 130 | + return undefined |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +function normalizeLocationHref(href: unknown) { |
| 135 | + if (typeof href !== 'string' || !href.trim()) { |
| 136 | + return undefined |
| 137 | + } |
| 138 | + |
| 139 | + try { |
| 140 | + return buildUrl(href).href |
| 141 | + } catch { |
| 142 | + return undefined |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +function normalizePathname(pathname: unknown) { |
| 147 | + if (typeof pathname !== 'string' || !pathname.trim()) { |
| 148 | + return undefined |
| 149 | + } |
| 150 | + |
| 151 | + let normalizedPathname = pathname.trim() |
| 152 | + |
| 153 | + if (!normalizedPathname.startsWith('/')) { |
| 154 | + normalizedPathname = `/${normalizedPathname}` |
| 155 | + } |
| 156 | + |
| 157 | + if (normalizedPathname.length > 1) { |
| 158 | + normalizedPathname = normalizedPathname.replace(/\/+$/, '') |
| 159 | + } |
| 160 | + |
| 161 | + return normalizedPathname || '/' |
| 162 | +} |
| 163 | + |
| 164 | +function getHistoryInstrumentationTarget(methodName: 'pushState' | 'replaceState') { |
| 165 | + if (typeof History === 'undefined') { |
| 166 | + return { [methodName]: noop } |
| 167 | + } |
| 168 | + |
| 169 | + return Object.prototype.hasOwnProperty.call(history, methodName) ? history : History.prototype |
| 170 | +} |
0 commit comments