forked from 4Science/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in task/dspace-cris-2023_02_x/DSC-1570-fix-rum (pull request D…
…Space#1748) Fix several issue with SSR Approved-by: Andrea Barbasso
- Loading branch information
Showing
11 changed files
with
120 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { environment } from '../../../environments/environment'; | ||
import { datadogRum } from '@datadog/browser-rum'; | ||
import { CookieConsents, KlaroService } from '../cookies/klaro.service'; | ||
import { BehaviorSubject, Observable } from 'rxjs'; | ||
import { createSelector, Store } from '@ngrx/store'; | ||
import { setDatadogRumStatusAction } from './datadog-rum.actions'; | ||
import { DatadogRumState } from './datadog-rum.reducer'; | ||
import { distinctUntilChanged, take } from 'rxjs/operators'; | ||
import { coreSelector } from '../../core/core.selectors'; | ||
import { CoreState } from '../../core/core-state.model'; | ||
import { DatadogRumService } from './datadog-rum.service'; | ||
|
||
@Injectable() | ||
export class BrowserDatadogRumService extends DatadogRumService { | ||
|
||
consentsUpdates$: BehaviorSubject<CookieConsents>; | ||
datadogRumStateSelector = createSelector(coreSelector, (state: CoreState) => state.datadogRum); | ||
|
||
constructor( | ||
private klaroService: KlaroService, | ||
private store: Store | ||
) { | ||
super(); | ||
} | ||
|
||
initDatadogRum() { | ||
this.klaroService.watchConsentUpdates(); | ||
this.consentsUpdates$ = this.klaroService.consentsUpdates$; | ||
this.consentsUpdates$.subscribe(savedPreferences => { | ||
this.getDatadogRumState().subscribe((state) => { | ||
if (savedPreferences?.datadog && | ||
environment.datadogRum?.clientToken && environment.datadogRum?.applicationId && | ||
environment.datadogRum?.service && environment.datadogRum?.env) { | ||
if (!state.isInitialized) { | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isInitialized: true, | ||
isRunning: true | ||
})); | ||
datadogRum.init(environment.datadogRum); | ||
} else if (!state.isRunning) { | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isRunning: true | ||
})); | ||
datadogRum.startSessionReplayRecording(); | ||
} | ||
} else { | ||
datadogRum.stopSessionReplayRecording(); | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isRunning: false | ||
})); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
|
||
getDatadogRumState(): Observable<DatadogRumState> { | ||
return this.store | ||
.select(this.datadogRumStateSelector) | ||
.pipe( | ||
distinctUntilChanged(), | ||
take(1), | ||
); | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,67 +1,11 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { environment } from '../../../environments/environment'; | ||
import { datadogRum } from '@datadog/browser-rum'; | ||
import { CookieConsents, KlaroService } from '../cookies/klaro.service'; | ||
import { BehaviorSubject, Observable } from 'rxjs'; | ||
import { createSelector, Store } from '@ngrx/store'; | ||
import { setDatadogRumStatusAction } from './datadog-rum.actions'; | ||
import { DatadogRumState } from './datadog-rum.reducer'; | ||
import { distinctUntilChanged, take } from 'rxjs/operators'; | ||
import { coreSelector } from '../../core/core.selectors'; | ||
import { CoreState } from '../../core/core-state.model'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class DatadogRumService { | ||
@Injectable() | ||
export abstract class DatadogRumService { | ||
|
||
consentsUpdates$: BehaviorSubject<CookieConsents>; | ||
datadogRumStateSelector = createSelector(coreSelector, (state: CoreState) => state.datadogRum); | ||
|
||
constructor( | ||
private klaroService: KlaroService, | ||
private store: Store | ||
) { | ||
} | ||
|
||
initDatadogRum() { | ||
this.klaroService.watchConsentUpdates(); | ||
this.consentsUpdates$ = this.klaroService.consentsUpdates$; | ||
this.consentsUpdates$.subscribe(savedPreferences => { | ||
this.getDatadogRumState().subscribe((state) => { | ||
if (savedPreferences?.datadog && | ||
environment.datadogRum?.clientToken && environment.datadogRum?.applicationId && | ||
environment.datadogRum?.service && environment.datadogRum?.env) { | ||
if (!state.isInitialized) { | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isInitialized: true, | ||
isRunning: true | ||
})); | ||
datadogRum.init(environment.datadogRum); | ||
} else if (!state.isRunning) { | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isRunning: true | ||
})); | ||
datadogRum.startSessionReplayRecording(); | ||
} | ||
} else { | ||
datadogRum.stopSessionReplayRecording(); | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isRunning: false | ||
})); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
|
||
getDatadogRumState(): Observable<DatadogRumState> { | ||
return this.store | ||
.select(this.datadogRumStateSelector) | ||
.pipe( | ||
distinctUntilChanged(), | ||
take(1), | ||
); | ||
} | ||
/** | ||
* Initializes the service | ||
*/ | ||
abstract initDatadogRum(); | ||
} | ||
|
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,11 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { DatadogRumService } from './datadog-rum.service'; | ||
|
||
@Injectable() | ||
export class ServerDatadogRumService extends DatadogRumService { | ||
|
||
initDatadogRum() { | ||
return; | ||
} | ||
} | ||
|
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