-
Notifications
You must be signed in to change notification settings - Fork 45
/
GoogleTagManager.js
99 lines (98 loc) · 2.39 KB
/
GoogleTagManager.js
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
import analytics from "analytics.js";
/**
* Performs the tracking calls to Google Tag Manager.
* Utilizing Segment IO Metrics Integration.
* Note: Not currently working (containerId: GTM-P79HDJ).
*
* @module GoogleTagManager
* @class
* @internal
*/
class GoogleTagManager {
constructor(options = {}) {
this.name = "Google Tag Manager";
this._loaded = false;
this.options = options;
}
/**
*
* @method pageView
* @param {String} eventName
* @param {Object} params
* @returns {Promise}
* @internal
*/
pageView(...args) {
return this.track(...args);
}
user(user) {
return new Promise(resolve => {
// reject(new Error("dummy error"));
resolve({
user
});
});
}
/**
*
* @method track
* @param {String} eventName
* @param {Object} params
* @returns {Promise}
* @internal
*/
track(eventName, params) {
return new Promise((resolve, reject) => {
this._load()
.then(() => {
this._track(eventName, params);
resolve({
eventName,
params
});
})
.catch(error => {
console.error("GTM: Failed to initialize", error);
reject(error);
});
});
}
/**
*
* @method _track
* @param {String} eventName
* @param {Object} params
* @protected
*/
_track(eventName, params) {
if (eventName === "pageView") {
analytics.page(params.category, params);
return;
}
analytics.track(eventName, params);
}
/**
*
* @method _load
* @protected
*/
_load() {
return (
this._promise ||
(this._promise = new Promise(resolve => {
if (this._loaded) {
resolve();
} else {
analytics.once("ready", () => {
this._loaded = true;
resolve();
});
analytics.initialize({
"Google Tag Manager": this.options
});
}
}))
);
}
}
export default GoogleTagManager;