-
Notifications
You must be signed in to change notification settings - Fork 173
⚗️ Add Angular Router integration #4315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5816b10
✨ Add @datadog/browser-rum-angular package with Angular Router integr…
rgaignault 7db6a62
✅ Add Angular test app and E2E tests for router integration
rgaignault ddff855
🔧 Downgrade test app to Angular 16 and fix E2E scenario
rgaignault 0887365
👌 v15 support + simplification
amortemousque d9e1544
align with React implementation
amortemousque 2d54319
Merge remote-tracking branch 'origin/main' into romanG/angular-integr…
rgaignault fa754ce
fix test
amortemousque 0967db7
Merge branch 'main' into romanG/angular-integration
amortemousque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,6 @@ | ||
| * | ||
| !/cjs/**/* | ||
| !/esm/**/* | ||
| !/src/**/* | ||
| /src/**/*.spec.ts | ||
| /src/**/*.specHelper.ts |
This file contains hidden or 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,39 @@ | ||
| { | ||
| "name": "@datadog/browser-rum-angular", | ||
| "private": true, | ||
| "license": "Apache-2.0", | ||
| "main": "cjs/entries/main.js", | ||
| "module": "esm/entries/main.js", | ||
| "types": "cjs/entries/main.d.ts", | ||
| "scripts": { | ||
| "build": "node ../../scripts/build/build-package.ts --modules", | ||
| "prepack": "npm run build" | ||
| }, | ||
| "dependencies": { | ||
| "@datadog/browser-core": "6.31.0", | ||
| "@datadog/browser-rum-core": "6.31.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "@angular/core": ">=15 <=21", | ||
| "@angular/router": ">=15 <=21", | ||
| "rxjs": ">=7" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/DataDog/browser-sdk.git", | ||
| "directory": "packages/rum-angular" | ||
| }, | ||
| "volta": { | ||
| "extends": "../../package.json" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "devDependencies": { | ||
| "@angular/common": "19.2.5", | ||
| "@angular/compiler": "19.2.5", | ||
| "@angular/core": "19.2.5", | ||
| "@angular/router": "19.2.5", | ||
| "rxjs": "7.8.2" | ||
| } | ||
| } | ||
This file contains hidden or 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,98 @@ | ||
| import type { RumInitConfiguration, RumPublicApi } from '@datadog/browser-rum-core' | ||
| import { registerCleanupTask } from '../../../core/test' | ||
| import { angularPlugin, onRumInit, onRumStart, resetAngularPlugin } from './angularPlugin' | ||
|
|
||
| const PUBLIC_API = {} as RumPublicApi | ||
| const INIT_CONFIGURATION = {} as RumInitConfiguration | ||
|
|
||
| describe('angularPlugin', () => { | ||
| beforeEach(() => { | ||
| registerCleanupTask(() => { | ||
| resetAngularPlugin() | ||
| }) | ||
| }) | ||
|
|
||
| it('returns a plugin object', () => { | ||
| const plugin = angularPlugin() | ||
| expect(plugin).toEqual( | ||
| jasmine.objectContaining({ | ||
| name: 'angular', | ||
| onInit: jasmine.any(Function), | ||
| onRumStart: jasmine.any(Function), | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('calls callbacks registered with onRumInit during onInit', () => { | ||
| const callbackSpy = jasmine.createSpy() | ||
| const pluginConfiguration = {} | ||
| onRumInit(callbackSpy) | ||
|
|
||
| expect(callbackSpy).not.toHaveBeenCalled() | ||
|
|
||
| angularPlugin(pluginConfiguration).onInit!({ | ||
| publicApi: PUBLIC_API, | ||
| initConfiguration: INIT_CONFIGURATION, | ||
| }) | ||
|
|
||
| expect(callbackSpy).toHaveBeenCalledTimes(1) | ||
| expect(callbackSpy.calls.mostRecent().args[0]).toBe(pluginConfiguration) | ||
| expect(callbackSpy.calls.mostRecent().args[1]).toBe(PUBLIC_API) | ||
| }) | ||
|
|
||
| it('calls callbacks immediately if onInit was already invoked', () => { | ||
| const callbackSpy = jasmine.createSpy() | ||
| const pluginConfiguration = {} | ||
| angularPlugin(pluginConfiguration).onInit!({ | ||
| publicApi: PUBLIC_API, | ||
| initConfiguration: INIT_CONFIGURATION, | ||
| }) | ||
|
|
||
| onRumInit(callbackSpy) | ||
|
|
||
| expect(callbackSpy).toHaveBeenCalledTimes(1) | ||
| expect(callbackSpy.calls.mostRecent().args[0]).toBe(pluginConfiguration) | ||
| expect(callbackSpy.calls.mostRecent().args[1]).toBe(PUBLIC_API) | ||
| }) | ||
|
|
||
| it('enforce manual view tracking when router is enabled', () => { | ||
| const initConfiguration = { ...INIT_CONFIGURATION } | ||
| angularPlugin({ router: true }).onInit!({ publicApi: PUBLIC_API, initConfiguration }) | ||
|
|
||
| expect(initConfiguration.trackViewsManually).toBe(true) | ||
| }) | ||
|
|
||
| it('does not enforce manual view tracking when router is disabled', () => { | ||
| const initConfiguration = { ...INIT_CONFIGURATION } | ||
| angularPlugin({ router: false }).onInit!({ publicApi: PUBLIC_API, initConfiguration }) | ||
|
|
||
| expect(initConfiguration.trackViewsManually).toBeUndefined() | ||
| }) | ||
|
|
||
| it('returns the configuration telemetry', () => { | ||
| const pluginConfiguration = { router: true } | ||
| const plugin = angularPlugin(pluginConfiguration) | ||
|
|
||
| expect(plugin.getConfigurationTelemetry!()).toEqual({ router: true }) | ||
| }) | ||
|
|
||
| it('calls onRumStart subscribers during onRumStart', () => { | ||
| const callbackSpy = jasmine.createSpy() | ||
| const addErrorSpy = jasmine.createSpy() | ||
| onRumStart(callbackSpy) | ||
|
|
||
| angularPlugin().onRumStart!({ addError: addErrorSpy }) | ||
|
|
||
| expect(callbackSpy).toHaveBeenCalledWith(addErrorSpy) | ||
| }) | ||
|
|
||
| it('calls onRumStart subscribers immediately if already started', () => { | ||
| const addErrorSpy = jasmine.createSpy() | ||
| angularPlugin().onRumStart!({ addError: addErrorSpy }) | ||
|
|
||
| const callbackSpy = jasmine.createSpy() | ||
| onRumStart(callbackSpy) | ||
|
|
||
| expect(callbackSpy).toHaveBeenCalledWith(addErrorSpy) | ||
| }) | ||
| }) |
This file contains hidden or 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,93 @@ | ||
| import type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core' | ||
|
|
||
| type InitSubscriber = (configuration: AngularPluginConfiguration, rumPublicApi: RumPublicApi) => void | ||
| type StartSubscriber = (addError: StartRumResult['addError']) => void | ||
|
|
||
| let globalPublicApi: RumPublicApi | undefined | ||
| let globalConfiguration: AngularPluginConfiguration | undefined | ||
| let globalAddError: StartRumResult['addError'] | undefined | ||
|
|
||
| const onRumInitSubscribers: InitSubscriber[] = [] | ||
| const onRumStartSubscribers: StartSubscriber[] = [] | ||
|
|
||
| /** | ||
| * Angular plugin configuration. | ||
| * | ||
| * @category Main | ||
| */ | ||
| export interface AngularPluginConfiguration { | ||
| /** | ||
| * Enable Angular Router integration. Make sure to use `provideDatadogRouter()` in your | ||
| * application providers. | ||
| */ | ||
| router?: boolean | ||
| } | ||
|
|
||
| /** | ||
| * Angular plugin constructor. | ||
| * | ||
| * @category Main | ||
| * @example | ||
| * ```ts | ||
| * import { datadogRum } from '@datadog/browser-rum' | ||
| * import { angularPlugin } from '@datadog/browser-rum-angular' | ||
| * | ||
| * datadogRum.init({ | ||
| * applicationId: '<DATADOG_APPLICATION_ID>', | ||
| * clientToken: '<DATADOG_CLIENT_TOKEN>', | ||
| * site: '<DATADOG_SITE>', | ||
| * plugins: [angularPlugin({ router: true })], | ||
| * // ... | ||
| * }) | ||
| * ``` | ||
| */ | ||
| export function angularPlugin(configuration: AngularPluginConfiguration = {}): RumPlugin { | ||
| return { | ||
| name: 'angular', | ||
| onInit({ publicApi, initConfiguration }) { | ||
| globalPublicApi = publicApi | ||
| globalConfiguration = configuration | ||
| for (const subscriber of onRumInitSubscribers) { | ||
| subscriber(globalConfiguration, globalPublicApi) | ||
| } | ||
| if (configuration.router) { | ||
| initConfiguration.trackViewsManually = true | ||
| } | ||
| }, | ||
| onRumStart({ addError }) { | ||
| globalAddError = addError | ||
| if (addError) { | ||
| for (const subscriber of onRumStartSubscribers) { | ||
| subscriber(addError) | ||
| } | ||
| } | ||
| }, | ||
| getConfigurationTelemetry() { | ||
| return { router: !!configuration.router } | ||
| }, | ||
| } satisfies RumPlugin | ||
| } | ||
|
|
||
| export function onRumInit(callback: InitSubscriber) { | ||
| if (globalConfiguration && globalPublicApi) { | ||
| callback(globalConfiguration, globalPublicApi) | ||
| } else { | ||
| onRumInitSubscribers.push(callback) | ||
| } | ||
| } | ||
|
|
||
| export function onRumStart(callback: StartSubscriber) { | ||
| if (globalAddError) { | ||
| callback(globalAddError) | ||
| } else { | ||
| onRumStartSubscribers.push(callback) | ||
| } | ||
| } | ||
|
|
||
| export function resetAngularPlugin() { | ||
| globalPublicApi = undefined | ||
| globalConfiguration = undefined | ||
| globalAddError = undefined | ||
| onRumInitSubscribers.length = 0 | ||
| onRumStartSubscribers.length = 0 | ||
| } |
50 changes: 50 additions & 0 deletions
50
packages/rum-angular/src/domain/angularRouter/provideDatadogRouter.ts
This file contains hidden or 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,50 @@ | ||
| import type { EnvironmentProviders } from '@angular/core' | ||
| import { ENVIRONMENT_INITIALIZER, inject, makeEnvironmentProviders } from '@angular/core' | ||
| import { GuardsCheckEnd, Router } from '@angular/router' | ||
| import { filter } from 'rxjs' | ||
| import { startAngularView } from './startAngularView' | ||
|
|
||
| /** | ||
| * Angular provider that subscribes to Router events and starts a new RUM view | ||
| * on each GuardsCheckEnd, using the matched route template as the view name. | ||
| * | ||
| * GuardsCheckEnd fires after guards pass but before resolvers run, so data | ||
| * fetches from resolvers are correctly attributed to the new view. | ||
| * | ||
| * @category Main | ||
| * @example | ||
| * ```ts | ||
| * import { bootstrapApplication } from '@angular/platform-browser' | ||
| * import { provideRouter } from '@angular/router' | ||
| * import { provideDatadogRouter } from '@datadog/browser-rum-angular' | ||
| * | ||
| * bootstrapApplication(AppComponent, { | ||
| * providers: [ | ||
| * provideRouter(routes), | ||
| * provideDatadogRouter(), | ||
| * ], | ||
| * }) | ||
| * ``` | ||
| */ | ||
| export function provideDatadogRouter(): EnvironmentProviders { | ||
| return makeEnvironmentProviders([ | ||
| { | ||
| // Needed for Angular v15 support (provideEnvironmentInitializer requires v16+) | ||
| provide: ENVIRONMENT_INITIALIZER, | ||
| multi: true, | ||
| useFactory: () => { | ||
| const router = inject(Router) | ||
|
|
||
| return () => { | ||
| // No unsubscribe needed as its for the full app lifecycle and because DestroyRef requires v16+ | ||
|
amortemousque marked this conversation as resolved.
|
||
| router.events | ||
| .pipe(filter((event): event is GuardsCheckEnd => event instanceof GuardsCheckEnd)) | ||
| .subscribe((event) => { | ||
| const root = event.state.root | ||
| startAngularView(root, event.urlAfterRedirects) | ||
| }) | ||
| } | ||
| }, | ||
| }, | ||
| ]) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.