-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathobservability-events.service.ts
More file actions
116 lines (102 loc) · 3.42 KB
/
Copy pathobservability-events.service.ts
File metadata and controls
116 lines (102 loc) · 3.42 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Inject, Injectable } from '@angular/core'
import { WINDOW } from 'src/app/cdk/window'
export type JourneyType =
| 'orcid_registration'
| 'orcid_update_emails'
| 'orcid_with_notifications'
| 'orcid_without_notifications'
@Injectable({
providedIn: 'root',
})
export class CustomEventService {
// Store the start times of each journey
private journeys: { [key: string]: { startTime: number; attributes: any } } =
{}
constructor(@Inject(WINDOW) private window: Window) {}
/**
* Starts a new user journey.
* @param journeyType The type of the journey (e.g., 'orcid_registration', 'orcid_update_emails').
* @param attributes Additional attributes to store with the journey
*/
startJourney(journeyType: JourneyType, attributes: any = {}): void {
// Record the start time and initial attributes
this.journeys[journeyType] = {
startTime: Date.now(),
attributes,
}
if (runtimeEnvironment.debugger) {
console.debug(
`[RUM][journey:${journeyType}] : start`,
attributes
)
}
}
/**
* Records an event within the journey.
* @param journeyType The type of the journey (e.g., 'orcid_registration').
* @param eventName The name of the event to track (e.g., 'page_loaded', 'form_submitted').
* @param additionalAttributes Any additional attributes related to the event.
*/
recordEvent(
journeyType: JourneyType,
eventName: string,
additionalAttributes: any = {}
): void {
if (!this.journeys[journeyType]) {
console.error(`Journey "${journeyType}" not started.`)
return
}
// Calculate time since the start of the journey
const elapsedTime = Date.now() - this.journeys[journeyType].startTime
// Combine the journey attributes with additional attributes
const eventAttributes = {
...this.journeys[journeyType].attributes,
...additionalAttributes,
eventName,
elapsedTime,
}
if (typeof (this.window as any).newrelic?.addPageAction === 'function') {
;(this.window as any).newrelic.addPageAction(journeyType, eventAttributes)
}
// Send the custom event to New Relic
if (runtimeEnvironment.debugger) {
console.debug(
`[RUM][journey:${journeyType}] : event ${eventName}`,
eventAttributes
)
}
}
/**
* Finishes the user journey.
* @param journeyType The type of the journey (e.g., 'orcid_registration').
* @param additionalAttributes Any final attributes or metadata to log at the end of the journey.
*/
finishJourney(journeyType: string, additionalAttributes: any = {}): void {
if (!this.journeys[journeyType]) {
console.error(`Journey "${journeyType}" not started.`)
return
}
// Calculate time since the start of the journey
const elapsedTime = Date.now() - this.journeys[journeyType].startTime
// Final event attributes
const finalAttributes = {
...this.journeys[journeyType].attributes,
...additionalAttributes,
eventName: 'journey_finished',
elapsedTime,
}
// Send the final custom event to New Relic
if (typeof (this.window as any).newrelic?.addPageAction === 'function') {
;(this.window as any).newrelic?.addPageAction(
journeyType,
finalAttributes
)
}
// Clean up the journey data
delete this.journeys[journeyType]
console.debug(
`[RUM][journey:${journeyType}] : finished`,
finalAttributes
)
}
}