forked from SnakeByteDevelopment/angulartics2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx-analytics-gtm.ts
110 lines (92 loc) · 3.34 KB
/
ngx-analytics-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
import { Injectable } from '@angular/core';
import { NgxAnalytics, GoogleTagManagerSettings } from 'ngx-analytics';
declare var dataLayer: any;
export class GoogleTagManagerDefaults implements GoogleTagManagerSettings {
userId = null;
}
@Injectable()
export class NgxAnalyticsGoogleTagManager {
constructor(
protected ngxAnalytics: NgxAnalytics,
) {
// The dataLayer needs to be initialized
if (typeof dataLayer !== 'undefined' && dataLayer) {
dataLayer = (<any>window).dataLayer = (<any>window).dataLayer || [];
}
const defaults = new GoogleTagManagerDefaults;
// Set the default settings for this module
this.ngxAnalytics.settings.gtm = { ...defaults, ...this.ngxAnalytics.settings.gtm };
this.ngxAnalytics.pageTrack.subscribe((x) => this.pageTrack(x.path));
this.ngxAnalytics.eventTrack.subscribe((x) => this.eventTrack(x.action, x.properties));
this.ngxAnalytics.exceptionTrack.subscribe((x: any) => this.exceptionTrack(x));
this.ngxAnalytics.setUsername.subscribe((x: string) => this.setUsername(x));
}
pageTrack(path: string) {
if (typeof dataLayer !== 'undefined' && dataLayer) {
dataLayer.push({
'event': 'Page View',
'content-name': path,
'userId': this.ngxAnalytics.settings.gtm.userId
});
}
}
/**
* Send interactions to the dataLayer, i.e. for event tracking in Google Analytics
*
* @param action associated with the event
* @param properties
* @param {string} properties.category
* @param {string} [properties.label]
* @param {number} [properties.value]
* @param {boolean} [properties.noninteraction]
*/
eventTrack(action: string, properties: any) {
// Set a default GTM category
properties = properties || {};
if (typeof dataLayer !== 'undefined' && dataLayer) {
dataLayer.push({
event: properties.event || 'interaction',
target: properties.category || 'Event',
action: action,
label: properties.label,
value: properties.value,
interactionType: properties.noninteraction,
userId: this.ngxAnalytics.settings.gtm.userId,
...properties.gtmCustom
});
}
}
/**
* Exception Track Event in GTM
*
* @param {Object} properties
* @param {string} properties.appId
* @param {string} properties.appName
* @param {string} properties.appVersion
* @param {string} [properties.description]
* @param {boolean} [properties.fatal]
*/
exceptionTrack(properties: any) {
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.ngxAnalytics.settings.gtm.userId = userId;
}
}