-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathAdobeTagManager.js
113 lines (106 loc) · 2.84 KB
/
AdobeTagManager.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Performs the tracking calls to Adobe Tag Manager.
* @module AdobeTagManager
* @class
* @internal
*/
class AdobeTagManager {
constructor(options = {}) {
this.options = options;
this.name = "Adobe Tag Manager";
}
/**
*
* @method pageView
* @param {String} eventName
* @param {Object} params
* @returns {Promise}
* @internal
*/
pageView(...args) {
return this.track(...args);
}
/**
*
* @method track
* @param {String} eventName
* @param {Object} params
* @returns {Promise}
* @internal
*/
track(eventName, params) {
return new Promise((resolve, reject) => {
this._load()
.then(satellite => {
this._satellite = this._satellite || satellite;
this._track(eventName, params);
resolve({
eventName,
params
});
})
.catch(error => {
console.error("Omniture: Failed to load seed file", error);
reject(error);
});
});
}
/**
*
* @method _track
* @param {String} eventName
* @param {Object} params
* @protected
*/
_track(eventName, params) {
this._satellite.data.customVars = {};
this._satellite.setVar(params);
this._satellite.track(eventName);
}
/**
*
* @method _load
* @protected
*/
_load() {
return (
this._promise ||
(this._promise = new Promise((resolve, reject) => {
if (window._satellite) {
resolve(window._satellite);
} else {
const script = document.createElement("script");
script.onload = () => {
this._addPageBottom();
resolve(window._satellite);
};
script.onerror = error => {
reject(error);
};
script.src = this.options.seedFile;
document.head.appendChild(script);
}
}))
);
}
/**
*
* @method _addPageBottom
* @protected
*/
_addPageBottom() {
const body = document.body;
const script = document.createElement("script");
// Lets add to page so Adobe consultant knows we've added the pageBottom() call.
const scriptContent = `
"use strict";
var _satellite = window._satellite;
if (_satellite) {
_satellite.pageBottom();
}
`;
script.text = scriptContent;
return body.appendChild(script);
}
}
export default AdobeTagManager;