-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathview-tracker.component.ts
More file actions
56 lines (51 loc) · 1.52 KB
/
view-tracker.component.ts
File metadata and controls
56 lines (51 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Component, Input, OnInit, OnDestroy } from '@angular/core';
import { Angulartics2 } from 'angulartics2';
import { DSpaceObject } from '../../../core/shared/dspace-object.model';
import { Subscription } from 'rxjs/internal/Subscription';
import { take } from 'rxjs/operators';
import { hasValue } from '../../../shared/empty.util';
import { ReferrerService } from '../../../core/services/referrer.service';
/**
* This component triggers a page view statistic
*/
@Component({
selector: 'ds-view-tracker',
styleUrls: ['./view-tracker.component.scss'],
templateUrl: './view-tracker.component.html',
})
export class ViewTrackerComponent implements OnInit, OnDestroy {
/**
* The DSpaceObject to track a view event about
*/
@Input() object: DSpaceObject;
/**
* The subscription on this.referrerService.getReferrer()
* @protected
*/
protected sub: Subscription;
constructor(
public angulartics2: Angulartics2,
public referrerService: ReferrerService
) {
}
ngOnInit(): void {
this.sub = this.referrerService.getReferrer()
.pipe(take(1))
.subscribe((referrer: string) => {
this.angulartics2.eventTrack.next({
action: 'page_view',
properties: {
object: this.object,
referrer
},
});
});
}
ngOnDestroy(): void {
// unsubscribe in the case that this component is destroyed before
// this.referrerService.getReferrer() has emitted
if (hasValue(this.sub)) {
this.sub.unsubscribe();
}
}
}