1- import { Component , Input , OnInit } from '@angular/core' ;
1+ import { Component , Input , OnInit , OnDestroy } from '@angular/core' ;
22import { Angulartics2 } from 'angulartics2' ;
33import { DSpaceObject } from '../../../core/shared/dspace-object.model' ;
4+ import { Subscription } from 'rxjs/internal/Subscription' ;
5+ import { take } from 'rxjs/operators' ;
6+ import { hasValue } from '../../../shared/empty.util' ;
7+ import { ReferrerService } from '../../../core/services/referrer.service' ;
48
59/**
610 * This component triggers a page view statistic
@@ -10,18 +14,43 @@ import { DSpaceObject } from '../../../core/shared/dspace-object.model';
1014 styleUrls : [ './view-tracker.component.scss' ] ,
1115 templateUrl : './view-tracker.component.html' ,
1216} )
13- export class ViewTrackerComponent implements OnInit {
17+ export class ViewTrackerComponent implements OnInit , OnDestroy {
18+ /**
19+ * The DSpaceObject to track a view event about
20+ */
1421 @Input ( ) object : DSpaceObject ;
1522
23+ /**
24+ * The subscription on this.referrerService.getReferrer()
25+ * @protected
26+ */
27+ protected sub : Subscription ;
28+
1629 constructor (
17- public angulartics2 : Angulartics2
30+ public angulartics2 : Angulartics2 ,
31+ public referrerService : ReferrerService
1832 ) {
1933 }
2034
2135 ngOnInit ( ) : void {
22- this . angulartics2 . eventTrack . next ( {
23- action : 'pageView' ,
24- properties : { object : this . object } ,
25- } ) ;
36+ this . sub = this . referrerService . getReferrer ( )
37+ . pipe ( take ( 1 ) )
38+ . subscribe ( ( referrer : string ) => {
39+ this . angulartics2 . eventTrack . next ( {
40+ action : 'pageView' ,
41+ properties : {
42+ object : this . object ,
43+ referrer
44+ } ,
45+ } ) ;
46+ } ) ;
47+ }
48+
49+ ngOnDestroy ( ) : void {
50+ // unsubscribe in the case that this component is destroyed before
51+ // this.referrerService.getReferrer() has emitted
52+ if ( hasValue ( this . sub ) ) {
53+ this . sub . unsubscribe ( ) ;
54+ }
2655 }
2756}
0 commit comments