forked from angulartics/angulartics2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtm.ts
123 lines (109 loc) · 3.7 KB
/
gtm.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { Injectable } from '@angular/core';
import { Angulartics2 } from '../../angulartics2-core';
import { GoogleTagManagerSettings } from '../../angulartics2-config';
declare var dataLayer: any;
export class GoogleTagManagerDefaults implements GoogleTagManagerSettings {
userId = null;
}
@Injectable({ providedIn: 'root' })
export class Angulartics2GoogleTagManager {
constructor(protected angulartics2: Angulartics2) {
// The dataLayer needs to be initialized
if (typeof dataLayer !== 'undefined' && dataLayer) {
dataLayer = (window as any).dataLayer = (window as any).dataLayer || [];
}
const defaults = new GoogleTagManagerDefaults();
// Set the default settings for this module
this.angulartics2.settings.gtm = { ...defaults, ...this.angulartics2.settings.gtm };
this.angulartics2.setUsername.subscribe((x: string) => this.setUsername(x));
}
startTracking() {
this.angulartics2.pageTrack
.pipe(this.angulartics2.filterDeveloperMode())
.subscribe(x => this.pageTrack(x.path));
this.angulartics2.eventTrack
.pipe(this.angulartics2.filterDeveloperMode())
.subscribe(x => this.eventTrack(x.action, x.properties));
this.angulartics2.exceptionTrack
.pipe(this.angulartics2.filterDeveloperMode())
.subscribe((x: any) => this.exceptionTrack(x));
}
pageTrack(path: string) {
this.pushLayer({
event: 'Page View',
'content-name': path,
userId: this.angulartics2.settings.gtm.userId,
});
}
/**
* Send Data Layer
*
* @layer data layer object
*/
pushLayer(layer: any) {
if (typeof dataLayer !== 'undefined' && dataLayer) {
dataLayer.push(layer);
}
}
/**
* Send interactions to the dataLayer, i.e. for event tracking in Google Analytics
*
* @param action associated with the event
*/
eventTrack(action: string, properties: any) {
// TODO: make interface
// @param {string} properties.category
// @param {string} [properties.label]
// @param {number} [properties.value]
// @param {boolean} [properties.noninteraction]
// Set a default GTM category
properties = properties || {};
this.pushLayer({
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,
});
}
/**
* Exception Track Event in GTM
*
*/
exceptionTrack(properties: any) {
// TODO: make interface
// @param {Object} properties
// @param {string} properties.appId
// @param {string} properties.appName
// @param {string} properties.appVersion
// @param {string} [properties.description]
// @param {boolean} [properties.fatal]
if (!properties || !properties.appId || !properties.appName || !properties.appVersion) {
console.error('Must be setted appId, appName and appVersion.');
return;
}
if (properties.fatal === undefined) {
console.log('No "fatal" provided, sending with fatal=true');
properties.exFatal = true;
}
properties.exDescription = properties.event ? properties.event.stack : properties.description;
this.eventTrack(
`Exception thrown for ${properties.appName} <${properties.appId}@${properties.appVersion}>`,
{
category: 'Exception',
label: properties.exDescription,
},
);
}
/**
* Set userId for use with Universal Analytics User ID feature
*
* @param userId used to identify user cross-device in Google Analytics
*/
setUsername(userId: string) {
this.angulartics2.settings.gtm.userId = userId;
}
}