-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathdatadogInit.js
More file actions
62 lines (53 loc) · 1.66 KB
/
Copy pathdatadogInit.js
File metadata and controls
62 lines (53 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { LightningElement, wire } from 'lwc'
import { NavigationMixin, CurrentPageReference } from 'lightning/navigation'
import datadogRumSlim from '@salesforce/resourceUrl/datadog_rum_slim'
import { loadScript } from 'lightning/platformResourceLoader'
let datadogInitialization
let lastStartedUrl
const defaultInitConfiguration = {
applicationId: 'xxx',
clientToken: 'xxx',
site: 'datadoghq.com',
}
export default class DatadogInit extends NavigationMixin(LightningElement) {
connectedCallback() {
this.initialize()
}
@wire(CurrentPageReference)
handleCurrentPageReference(pageReference) {
if (!pageReference) {
return
}
this.initialize()
if (window.DD_RUM) {
this.startViewForPageReference(pageReference)
}
}
startViewForPageReference(pageReference) {
const urlPromise = this[NavigationMixin.GenerateUrl](pageReference)
urlPromise.then((url) => {
if (url === lastStartedUrl) {
return
}
lastStartedUrl = url
const absoluteUrl = new URL(url, window.location.origin).href
window.DD_RUM.startView({ name: url, url: absoluteUrl })
})
}
initialize() {
if (!datadogInitialization) {
datadogInitialization = this.loadDatadogRum()
}
}
loadDatadogRum() {
return loadScript(this, datadogRumSlim).then(() => {
window.DD_RUM.setGlobalContext(window.RUM_CONTEXT)
window.DD_RUM.init({ ...defaultInitConfiguration, ...window.RUM_CONFIGURATION })
lastStartedUrl = window.location.pathname + window.location.search + window.location.hash
window.DD_RUM.startView({
name: lastStartedUrl,
url: window.location.href,
})
})
}
}