1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports["default"] = void 0;
|
9 |
|
10 | require("core-js/modules/es.object.to-string.js");
|
11 |
|
12 | require("core-js/modules/es.promise.js");
|
13 |
|
14 | require("core-js/modules/es.object.keys.js");
|
15 |
|
16 | require("core-js/modules/es.array.filter.js");
|
17 |
|
18 | require("core-js/modules/es.function.name.js");
|
19 |
|
20 | require("core-js/modules/es.array.slice.js");
|
21 |
|
22 | var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
23 |
|
24 | var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));
|
25 |
|
26 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
27 |
|
28 | var _util = require("./util");
|
29 |
|
30 | var AvAnalytics = function AvAnalytics(plugins) {
|
31 | var _this = this;
|
32 |
|
33 | var promise = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Promise;
|
34 | var pageTracking = arguments.length > 2 ? arguments[2] : undefined;
|
35 | var autoTrack = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
|
36 | var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
37 | (0, _classCallCheck2["default"])(this, AvAnalytics);
|
38 |
|
39 | this.startAutoTrack = function () {
|
40 | document.body.addEventListener('click', _this.handleEvent, true);
|
41 | document.body.addEventListener('focus', _this.handleEvent, true);
|
42 | document.body.addEventListener('blur', _this.handleEvent, true);
|
43 | };
|
44 |
|
45 | this.stopAutoTrack = function () {
|
46 | document.body.removeEventListener('click', _this.handleEvent, true);
|
47 | document.body.removeEventListener('focus', _this.handleEvent, true);
|
48 | document.body.removeEventListener('blur', _this.handleEvent, true);
|
49 | };
|
50 |
|
51 | this.handleEvent = function (event) {
|
52 | if (_this.invalidEvent(event)) {
|
53 | return;
|
54 | }
|
55 |
|
56 | var target = event.target || event.srcElement;
|
57 | var path = (0, _util.getComposedPath)(event.target);
|
58 | var analyticAttrs = {};
|
59 |
|
60 | if (_this.recursive) {
|
61 |
|
62 | var _iterator = (0, _createForOfIteratorHelper2["default"])(path.reverse()),
|
63 | _step;
|
64 |
|
65 | try {
|
66 | for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
67 | var pth = _step.value;
|
68 |
|
69 | var attrs = _this.getAnalyticAttrs(pth);
|
70 |
|
71 | analyticAttrs = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, analyticAttrs), attrs);
|
72 |
|
73 | if (Object.keys(attrs).length > 0) {
|
74 | analyticAttrs.elemId = pth.getAttribute('id') || pth.getAttribute('name') || undefined;
|
75 | }
|
76 | }
|
77 | } catch (err) {
|
78 | _iterator.e(err);
|
79 | } finally {
|
80 | _iterator.f();
|
81 | }
|
82 | } else {
|
83 | analyticAttrs = _this.getAnalyticAttrs(target);
|
84 | }
|
85 |
|
86 | var actions = analyticAttrs ? _this.eventModifiers.filter(function (mod) {
|
87 | return analyticAttrs[mod] === event.type;
|
88 | }) : [];
|
89 |
|
90 | if (Object.keys(analyticAttrs).length === 0 || _this.recursive && actions.length === 0 || actions.length === 0) {
|
91 | return;
|
92 | }
|
93 |
|
94 | analyticAttrs.action = analyticAttrs.action || event.type;
|
95 | analyticAttrs.event = event.type;
|
96 | analyticAttrs.elemId = analyticAttrs.elemId || target.getAttribute('id') || target.getAttribute('name') || undefined;
|
97 |
|
98 | if (analyticAttrs.elemId === undefined) {
|
99 | delete analyticAttrs.elemId;
|
100 | }
|
101 |
|
102 |
|
103 | var _iterator2 = (0, _createForOfIteratorHelper2["default"])(actions),
|
104 | _step2;
|
105 |
|
106 | try {
|
107 | for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
108 | var key = _step2.value;
|
109 |
|
110 | if (key !== 'action' && key !== 'event') {
|
111 | delete analyticAttrs[key];
|
112 | }
|
113 | }
|
114 | } catch (err) {
|
115 | _iterator2.e(err);
|
116 | } finally {
|
117 | _iterator2.f();
|
118 | }
|
119 |
|
120 | _this.trackEvent(analyticAttrs);
|
121 | };
|
122 |
|
123 | this.invalidEvent = function (event) {
|
124 | return (0, _util.isModifiedEvent)(event) || event.type === 'click' && !(0, _util.isLeftClickEvent)(event) || !(0, _util.isValidEventTypeOnTarget)(event);
|
125 | };
|
126 |
|
127 | this.getAnalyticAttrs = function (elem) {
|
128 | if (!elem.attributes) {
|
129 | return {};
|
130 | }
|
131 |
|
132 | var attrs = elem.attributes;
|
133 | var analyticAttrs = {};
|
134 |
|
135 | if (elem.nodeType === 1) {
|
136 | for (var i = attrs.length - 1; i >= 0; i--) {
|
137 | var name = attrs[i].name;
|
138 |
|
139 | if (name.indexOf("".concat(_this.attributePrefix, "-")) === 0) {
|
140 | var camelName = (0, _util.camelCase)(name.slice(_this.attributePrefix.length + 1));
|
141 | analyticAttrs[camelName] = elem.getAttribute(name);
|
142 | }
|
143 | }
|
144 | }
|
145 |
|
146 | return analyticAttrs;
|
147 | };
|
148 |
|
149 | this.startPageTracking = function () {
|
150 | if (!_this.pageListener) {
|
151 | _this.pageListener = _this.trackPageView;
|
152 | window.addEventListener('hashchange', _this.pageListener, false);
|
153 | }
|
154 | };
|
155 |
|
156 | this.stopPageTracking = function () {
|
157 | if (_this.pageListener) {
|
158 | window.removeEventListener('hashchange', _this.pageListener, false);
|
159 | delete _this.pageListener;
|
160 | }
|
161 | };
|
162 |
|
163 | this.init = function () {
|
164 | _this.setPageTracking();
|
165 |
|
166 | var _iterator3 = (0, _createForOfIteratorHelper2["default"])(_this.plugins),
|
167 | _step3;
|
168 |
|
169 | try {
|
170 | for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
171 | var plugin = _step3.value;
|
172 |
|
173 | if ((0, _util.isPluginEnabled)(plugin) && typeof plugin.init === 'function') {
|
174 | plugin.init();
|
175 | }
|
176 | }
|
177 | } catch (err) {
|
178 | _iterator3.e(err);
|
179 | } finally {
|
180 | _iterator3.f();
|
181 | }
|
182 | };
|
183 |
|
184 | this.setPageTracking = function (value) {
|
185 |
|
186 | if (value != undefined) {
|
187 | _this.pageTracking = !!value;
|
188 | }
|
189 |
|
190 | var canPageTrack = typeof _this.startPageTracking === 'function' && typeof _this.stopPageTracking === 'function';
|
191 |
|
192 | if (canPageTrack && _this.pageTracking !== _this.isPageTracking) {
|
193 | if (_this.pageTracking) {
|
194 | _this.startPageTracking();
|
195 | } else {
|
196 | _this.stopPageTracking();
|
197 | }
|
198 |
|
199 | _this.isPageTracking = _this.pageTracking;
|
200 | }
|
201 | };
|
202 |
|
203 | this.trackEvent = function (properties) {
|
204 | var promises = [];
|
205 | properties.url = properties.url || window.location.href || 'N/A';
|
206 |
|
207 | var _iterator4 = (0, _createForOfIteratorHelper2["default"])(_this.plugins),
|
208 | _step4;
|
209 |
|
210 | try {
|
211 | for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
212 | var plugin = _step4.value;
|
213 | var props = (0, _objectSpread2["default"])({}, properties);
|
214 |
|
215 | if ((0, _util.isPluginEnabled)(plugin) && typeof plugin.trackEvent === 'function') {
|
216 | promises.push(plugin.trackEvent(props));
|
217 | }
|
218 | }
|
219 | } catch (err) {
|
220 | _iterator4.e(err);
|
221 | } finally {
|
222 | _iterator4.f();
|
223 | }
|
224 |
|
225 | return _this.Promise.all(promises);
|
226 | };
|
227 |
|
228 | this.trackPageView = function (url) {
|
229 |
|
230 | if (typeof url === 'object') {
|
231 | url = url.newURL;
|
232 | }
|
233 |
|
234 | url = url || window.location.href;
|
235 | var promises = [];
|
236 |
|
237 | var _iterator5 = (0, _createForOfIteratorHelper2["default"])(_this.plugins),
|
238 | _step5;
|
239 |
|
240 | try {
|
241 | for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
242 | var plugin = _step5.value;
|
243 |
|
244 | if ((0, _util.isPluginEnabled)(plugin) && typeof plugin.trackPageView === 'function') {
|
245 | promises.push(plugin.trackPageView(url));
|
246 | }
|
247 | }
|
248 | } catch (err) {
|
249 | _iterator5.e(err);
|
250 | } finally {
|
251 | _iterator5.f();
|
252 | }
|
253 |
|
254 | return _this.Promise.all(promises);
|
255 | };
|
256 |
|
257 |
|
258 |
|
259 | if (!plugins || !promise) {
|
260 | throw new Error('[plugins], and [promise] must be defined');
|
261 | }
|
262 |
|
263 | this.plugins = Array.isArray(plugins) ? plugins : [plugins];
|
264 | this.pageTracking = !!pageTracking;
|
265 |
|
266 | if (options.eventModifiers) {
|
267 | this.eventModifiers = Array.isArray(options.eventModifiers) ? options.eventModifiers : [options.eventModifiers];
|
268 | } else {
|
269 | this.eventModifiers = ['action'];
|
270 | }
|
271 |
|
272 | this.Promise = promise;
|
273 | this.recursive = !!options.recursive;
|
274 | this.attributePrefix = options.attributePrefix || 'data-analytics';
|
275 | this.isPageTracking = false;
|
276 | this.hasInit = false;
|
277 |
|
278 | if (autoTrack) {
|
279 | this.startAutoTrack();
|
280 | }
|
281 | };
|
282 |
|
283 | exports["default"] = AvAnalytics; |
\ | No newline at end of file |