UNPKG

4.66 kBJavaScriptView Raw
1"use strict";
2// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3// SPDX-License-Identifier: Apache-2.0
4Object.defineProperty(exports, "__esModule", { value: true });
5var tslib_1 = require("tslib");
6var dom_utils_1 = require("../vendor/dom-utils");
7var core_1 = require("@aws-amplify/core");
8var logger = new core_1.ConsoleLogger('EventTracker');
9var defaultOpts = {
10 enable: false,
11 events: ['click'],
12 selectorPrefix: 'data-amplify-analytics-',
13 provider: 'AWSPinpoint',
14};
15var EventTracker = /** @class */ (function () {
16 function EventTracker(tracker, opts) {
17 if (!core_1.browserOrNode().isBrowser || !window.addEventListener) {
18 logger.debug('not in the supported web environment');
19 return;
20 }
21 this._config = Object.assign({}, defaultOpts, opts);
22 this._tracker = tracker;
23 this._delegates = {};
24 this._trackFunc = this._trackFunc.bind(this);
25 logger.debug('initialize pageview tracker with opts', this._config);
26 this.configure(this._config);
27 }
28 EventTracker.prototype.configure = function (opts) {
29 var _this = this;
30 Object.assign(this._config, opts);
31 if (!this._config.enable) {
32 Object.keys(this._delegates).forEach(function (key) {
33 if (typeof _this._delegates[key].destroy === 'function')
34 _this._delegates[key].destroy();
35 });
36 this._delegates = {};
37 }
38 else if (this._config.enable &&
39 Object.keys(this._delegates).length === 0) {
40 var selector_1 = '[' + this._config.selectorPrefix + 'on]';
41 this._config.events.forEach(function (evt) {
42 _this._delegates[evt] = dom_utils_1.delegate(document, evt, selector_1, _this._trackFunc, { composed: true, useCapture: true });
43 });
44 }
45 return this._config;
46 };
47 EventTracker.prototype._trackFunc = function (event, element) {
48 return tslib_1.__awaiter(this, void 0, void 0, function () {
49 var customAttrs, events, eventName, attrs, defaultAttrs, _a, attributes;
50 return tslib_1.__generator(this, function (_b) {
51 switch (_b.label) {
52 case 0:
53 customAttrs = {};
54 events = element
55 .getAttribute(this._config.selectorPrefix + 'on')
56 .split(/\s*,\s*/);
57 eventName = element.getAttribute(this._config.selectorPrefix + 'name');
58 attrs = element.getAttribute(this._config.selectorPrefix + 'attrs');
59 if (attrs) {
60 attrs.split(/\s*,\s*/).forEach(function (attr) {
61 var tmp = attr.trim().split(/\s*:\s*/);
62 customAttrs[tmp[0]] = tmp[1];
63 });
64 }
65 if (!(typeof this._config.attributes === 'function')) return [3 /*break*/, 2];
66 return [4 /*yield*/, this._config.attributes()];
67 case 1:
68 _a = _b.sent();
69 return [3 /*break*/, 3];
70 case 2:
71 _a = this._config.attributes;
72 _b.label = 3;
73 case 3:
74 defaultAttrs = _a;
75 attributes = Object.assign({
76 type: event.type,
77 target: event.target.localName + " with id " + event.target.id,
78 }, defaultAttrs, customAttrs);
79 logger.debug('events needed to be recorded', events);
80 logger.debug('attributes needed to be attached', customAttrs);
81 if (events.indexOf(event.type) < 0) {
82 logger.debug("event " + event.type + " is not selected to be recorded");
83 return [2 /*return*/];
84 }
85 this._tracker({
86 name: eventName || 'event',
87 attributes: attributes,
88 }, this._config.provider).catch(function (e) {
89 logger.debug("Failed to record the " + event.type + " event', " + e);
90 });
91 return [2 /*return*/];
92 }
93 });
94 });
95 };
96 return EventTracker;
97}());
98exports.EventTracker = EventTracker;
99//# sourceMappingURL=EventTracker.js.map
\No newline at end of file