Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* refactor: moves datadog rum methods to a service * cleanup: remove console warning * refactor(datadog-rum): moves service init to app-init service and checks if datadog has been initialized before adding or removing a user session * refactor: further refactoring of datadog rum init
- Loading branch information
Showing
9 changed files
with
132 additions
and
49 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { DatadogRumService } from './datadog-rum.service'; | ||
|
||
describe('DatadogRumService', () => { | ||
let service: DatadogRumService; | ||
const mockdatadogRumService = jasmine.createSpyObj('datadogRumService', ['initializeRum', 'setActiveUser', 'removeActiveUser']); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
{ provide: DatadogRumService, useValue: mockdatadogRumService } | ||
] | ||
}); | ||
service = TestBed.inject(DatadogRumService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
describe('setActiveUser', () => { | ||
it('should set the active user', () => { | ||
const identifier = 'test'; | ||
const identifierType = 'email'; | ||
mockdatadogRumService.setActiveUser.and.callThrough(); | ||
service.setActiveUser(identifier, identifierType); | ||
expect(service.setActiveUser).toHaveBeenCalledOnceWith(identifier, identifierType); | ||
}); | ||
}); | ||
|
||
describe('removeActiveUser', () => { | ||
it('should remove the active user', () => { | ||
mockdatadogRumService.removeActiveUser.and.callThrough(); | ||
service.removeActiveUser(); | ||
expect(service.removeActiveUser).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,50 @@ | ||
import { Inject, Injectable } from '@angular/core'; | ||
import { datadogRum, RumFetchResourceEventDomainContext } from '@datadog/browser-rum'; | ||
import { DspInstrumentationToken } from '../declarations/dsp-api-tokens'; | ||
import { DspInstrumentationConfig } from '../declarations/dsp-instrumentation-config'; | ||
|
||
const { version: appVersion } = require('../../../../package.json'); | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class DatadogRumService { | ||
|
||
constructor( | ||
@Inject(DspInstrumentationToken) private _dspInstrumentationConfig: DspInstrumentationConfig | ||
) { | ||
if (this._dspInstrumentationConfig.dataDog.enabled) { | ||
datadogRum.init({ | ||
applicationId: this._dspInstrumentationConfig.dataDog.applicationId, | ||
clientToken: this._dspInstrumentationConfig.dataDog.clientToken, | ||
site: this._dspInstrumentationConfig.dataDog.site, | ||
service: this._dspInstrumentationConfig.dataDog.service, | ||
env: this._dspInstrumentationConfig.environment, | ||
version: appVersion, | ||
sampleRate: 100, | ||
trackInteractions: true, | ||
beforeSend: (event, context) => { | ||
// collect a RUM resource's response headers | ||
if (event.type === 'resource' && event.resource.type === 'xhr') { | ||
event.context = { ...event.context, responseHeaders: (context as RumFetchResourceEventDomainContext).response.body }; | ||
} | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
setActiveUser(identifier: any, identifierType: 'iri' | 'email' | 'username'): void { | ||
if(datadogRum.getInternalContext().application_id) { | ||
datadogRum.setUser({ | ||
id: identifier, | ||
identifierType: identifierType | ||
}); | ||
} | ||
} | ||
|
||
removeActiveUser(): void { | ||
if(datadogRum.getInternalContext().application_id) { | ||
datadogRum.removeUser(); | ||
} | ||
} | ||
} |