1 | import { ɵɵdefineInjectable, ɵɵinject, Injectable } from '@angular/core';
|
2 | import { Angulartics2 } from 'angulartics2';
|
3 |
|
4 | class GoogleTagManagerDefaults {
|
5 | constructor() {
|
6 | this.userId = null;
|
7 | }
|
8 | }
|
9 | class Angulartics2GoogleTagManager {
|
10 | constructor(angulartics2) {
|
11 | this.angulartics2 = angulartics2;
|
12 |
|
13 | if (typeof dataLayer !== 'undefined' && dataLayer) {
|
14 | dataLayer = window.dataLayer = window.dataLayer || [];
|
15 | }
|
16 | const defaults = new GoogleTagManagerDefaults();
|
17 |
|
18 | this.angulartics2.settings.gtm = Object.assign(Object.assign({}, defaults), this.angulartics2.settings.gtm);
|
19 | this.angulartics2.setUsername
|
20 | .subscribe((x) => this.setUsername(x));
|
21 | }
|
22 | startTracking() {
|
23 | this.angulartics2.pageTrack
|
24 | .pipe(this.angulartics2.filterDeveloperMode())
|
25 | .subscribe((x) => this.pageTrack(x.path));
|
26 | this.angulartics2.eventTrack
|
27 | .pipe(this.angulartics2.filterDeveloperMode())
|
28 | .subscribe((x) => this.eventTrack(x.action, x.properties));
|
29 | this.angulartics2.exceptionTrack
|
30 | .pipe(this.angulartics2.filterDeveloperMode())
|
31 | .subscribe((x) => this.exceptionTrack(x));
|
32 | }
|
33 | pageTrack(path) {
|
34 | this.pushLayer({
|
35 | event: 'Page View',
|
36 | 'content-name': path,
|
37 | userId: this.angulartics2.settings.gtm.userId
|
38 | });
|
39 | }
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 | pushLayer(layer) {
|
46 | if (typeof dataLayer !== 'undefined' && dataLayer) {
|
47 | dataLayer.push(layer);
|
48 | }
|
49 | }
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 | eventTrack(action, properties) {
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | properties = properties || {};
|
63 | this.pushLayer(Object.assign({ event: properties.event || 'interaction', target: properties.category || 'Event', action, label: properties.label, value: properties.value, interactionType: properties.noninteraction, userId: this.angulartics2.settings.gtm.userId }, properties.gtmCustom));
|
64 | }
|
65 | |
66 |
|
67 |
|
68 |
|
69 | exceptionTrack(properties) {
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | if (!properties || !properties.appId || !properties.appName || !properties.appVersion) {
|
78 | console.error('Must be setted appId, appName and appVersion.');
|
79 | return;
|
80 | }
|
81 | if (properties.fatal === undefined) {
|
82 | console.log('No "fatal" provided, sending with fatal=true');
|
83 | properties.exFatal = true;
|
84 | }
|
85 | properties.exDescription = properties.event ? properties.event.stack : properties.description;
|
86 | this.eventTrack(`Exception thrown for ${properties.appName} <${properties.appId}@${properties.appVersion}>`, {
|
87 | category: 'Exception',
|
88 | label: properties.exDescription,
|
89 | });
|
90 | }
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 | setUsername(userId) {
|
97 | this.angulartics2.settings.gtm.userId = userId;
|
98 | }
|
99 | }
|
100 | Angulartics2GoogleTagManager.ɵprov = ɵɵdefineInjectable({ factory: function Angulartics2GoogleTagManager_Factory() { return new Angulartics2GoogleTagManager(ɵɵinject(Angulartics2)); }, token: Angulartics2GoogleTagManager, providedIn: "root" });
|
101 | Angulartics2GoogleTagManager.decorators = [
|
102 | { type: Injectable, args: [{ providedIn: 'root' },] }
|
103 | ];
|
104 | Angulartics2GoogleTagManager.ctorParameters = () => [
|
105 | { type: Angulartics2 }
|
106 | ];
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | export { Angulartics2GoogleTagManager, GoogleTagManagerDefaults };
|
113 |
|