1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
15 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
16 | return new (P || (P = Promise))(function (resolve, reject) {
|
17 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
18 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
19 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
20 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
21 | });
|
22 | };
|
23 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
24 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
25 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
26 | function verb(n) { return function (v) { return step([n, v]); }; }
|
27 | function step(op) {
|
28 | if (f) throw new TypeError("Generator is already executing.");
|
29 | while (_) try {
|
30 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
31 | if (y = 0, t) op = [op[0] & 2, t.value];
|
32 | switch (op[0]) {
|
33 | case 0: case 1: t = op; break;
|
34 | case 4: _.label++; return { value: op[1], done: false };
|
35 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
36 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
37 | default:
|
38 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
39 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
40 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
41 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
42 | if (t[2]) _.ops.pop();
|
43 | _.trys.pop(); continue;
|
44 | }
|
45 | op = body.call(thisArg, _);
|
46 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
47 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
48 | }
|
49 | };
|
50 | Object.defineProperty(exports, "__esModule", { value: true });
|
51 | var dom_utils_1 = require("../vendor/dom-utils");
|
52 | var core_1 = require("@aws-amplify/core");
|
53 | var logger = new core_1.ConsoleLogger('EventTracker');
|
54 | var defaultOpts = {
|
55 | enable: false,
|
56 | events: ['click'],
|
57 | selectorPrefix: 'data-amplify-analytics-',
|
58 | provider: 'AWSPinpoint',
|
59 | };
|
60 | var EventTracker = (function () {
|
61 | function EventTracker(tracker, opts) {
|
62 | if (!core_1.JS.browserOrNode().isBrowser || !window.addEventListener) {
|
63 | logger.debug('not in the supported web environment');
|
64 | return;
|
65 | }
|
66 | this._config = Object.assign({}, defaultOpts, opts);
|
67 | this._tracker = tracker;
|
68 | this._delegates = {};
|
69 | this._trackFunc = this._trackFunc.bind(this);
|
70 | logger.debug('initialize pageview tracker with opts', this._config);
|
71 | this.configure(this._config);
|
72 | }
|
73 | EventTracker.prototype.configure = function (opts) {
|
74 | var _this = this;
|
75 | Object.assign(this._config, opts);
|
76 | if (!this._config.enable) {
|
77 | Object.keys(this._delegates).forEach(function (key) {
|
78 | if (typeof _this._delegates[key].destroy === 'function')
|
79 | _this._delegates[key].destroy();
|
80 | });
|
81 | this._delegates = {};
|
82 | }
|
83 | else if (this._config.enable &&
|
84 | Object.keys(this._delegates).length === 0) {
|
85 | var selector_1 = '[' + this._config.selectorPrefix + 'on]';
|
86 | this._config.events.forEach(function (evt) {
|
87 | _this._delegates[evt] = dom_utils_1.delegate(document, evt, selector_1, _this._trackFunc, { composed: true, useCapture: true });
|
88 | });
|
89 | }
|
90 | return this._config;
|
91 | };
|
92 | EventTracker.prototype._trackFunc = function (event, element) {
|
93 | return __awaiter(this, void 0, void 0, function () {
|
94 | var customAttrs, events, eventName, attrs, defaultAttrs, _a, attributes;
|
95 | return __generator(this, function (_b) {
|
96 | switch (_b.label) {
|
97 | case 0:
|
98 | customAttrs = {};
|
99 | events = element
|
100 | .getAttribute(this._config.selectorPrefix + 'on')
|
101 | .split(/\s*,\s*/);
|
102 | eventName = element.getAttribute(this._config.selectorPrefix + 'name');
|
103 | attrs = element.getAttribute(this._config.selectorPrefix + 'attrs');
|
104 | if (attrs) {
|
105 | attrs.split(/\s*,\s*/).forEach(function (attr) {
|
106 | var tmp = attr.trim().split(/\s*:\s*/);
|
107 | customAttrs[tmp[0]] = tmp[1];
|
108 | });
|
109 | }
|
110 | if (!(typeof this._config.attributes === 'function')) return [3 , 2];
|
111 | return [4 , this._config.attributes()];
|
112 | case 1:
|
113 | _a = _b.sent();
|
114 | return [3 , 3];
|
115 | case 2:
|
116 | _a = this._config.attributes;
|
117 | _b.label = 3;
|
118 | case 3:
|
119 | defaultAttrs = _a;
|
120 | attributes = Object.assign({
|
121 | type: event.type,
|
122 | target: event.target.localName + " with id " + event.target.id,
|
123 | }, defaultAttrs, customAttrs);
|
124 | logger.debug('events needed to be recorded', events);
|
125 | logger.debug('attributes needed to be attached', customAttrs);
|
126 | if (events.indexOf(event.type) < 0) {
|
127 | logger.debug("event " + event.type + " is not selected to be recorded");
|
128 | return [2 ];
|
129 | }
|
130 | this._tracker({
|
131 | name: eventName || 'event',
|
132 | attributes: attributes,
|
133 | }, this._config.provider).catch(function (e) {
|
134 | logger.debug("Failed to record the " + event.type + " event', " + e);
|
135 | });
|
136 | return [2 ];
|
137 | }
|
138 | });
|
139 | });
|
140 | };
|
141 | return EventTracker;
|
142 | }());
|
143 | exports.EventTracker = EventTracker;
|
144 |
|
145 |
|
146 |
|
147 | exports.default = EventTracker;
|
148 |
|
\ | No newline at end of file |