UNPKG

7.63 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
6 * the License. A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0/
9 *
10 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
12 * and limitations under the License.
13 */
14var __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};
23var __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};
50Object.defineProperty(exports, "__esModule", { value: true });
51var dom_utils_1 = require("../vendor/dom-utils");
52var core_1 = require("@aws-amplify/core");
53var logger = new core_1.ConsoleLogger('EventTracker');
54var defaultOpts = {
55 enable: false,
56 events: ['click'],
57 selectorPrefix: 'data-amplify-analytics-',
58 provider: 'AWSPinpoint',
59};
60var EventTracker = /** @class */ (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 /*break*/, 2];
111 return [4 /*yield*/, this._config.attributes()];
112 case 1:
113 _a = _b.sent();
114 return [3 /*break*/, 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 /*return*/];
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 /*return*/];
137 }
138 });
139 });
140 };
141 return EventTracker;
142}());
143exports.EventTracker = EventTracker;
144/**
145 * @deprecated use named import
146 */
147exports.default = EventTracker;
148//# sourceMappingURL=EventTracker.js.map
\No newline at end of file