-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
663ff80
commit bca1392
Showing
5 changed files
with
136 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/app/core/observability-events/observability-events.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { Inject, Injectable } from '@angular/core' | ||
import { WINDOW } from 'src/app/cdk/window' | ||
import { environment } from 'src/environments/environment' | ||
|
||
export type journeyType = 'orcid_registration' | 'orcid_update_emails' | ||
@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 (environment.debugger) { | ||
console.debug( | ||
`-> Journey "${journeyType}" started at ${this.journeys[journeyType].startTime}` | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* 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: string, | ||
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, | ||
} | ||
;(this.window as any).newrelic.addPageAction(journeyType, eventAttributes) | ||
|
||
// Send the custom event to New Relic | ||
|
||
if (environment.debugger) { | ||
console.debug( | ||
`-> Event "${eventName}" recorded for journey "${journeyType}" with elapsed time ${elapsedTime}ms` | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* 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 | ||
;(this.window as any).addPageAction(journeyType, finalAttributes) | ||
|
||
// Clean up the journey data | ||
delete this.journeys[journeyType] | ||
|
||
console.log( | ||
`Journey "${journeyType}" finished with elapsed time ${elapsedTime}ms` | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters