UNPKG

7.08 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");
6// the session tracker for web
7var core_1 = require("@aws-amplify/core");
8var logger = new core_1.ConsoleLogger('SessionTracker');
9var defaultOpts = {
10 enable: false,
11 provider: 'AWSPinpoint',
12};
13var initialEventSent = false;
14var SessionTracker = /** @class */ (function () {
15 function SessionTracker(tracker, opts) {
16 this._config = Object.assign({}, defaultOpts, opts);
17 this._tracker = tracker;
18 this._hasEnabled = false;
19 this._trackFunc = this._trackFunc.bind(this);
20 this._trackBeforeUnload = this._trackBeforeUnload.bind(this);
21 this.configure(this._config);
22 }
23 SessionTracker.prototype._envCheck = function () {
24 if (!core_1.browserOrNode().isBrowser) {
25 return false;
26 }
27 if (!document || !document.addEventListener) {
28 logger.debug('not in the supported web environment');
29 return false;
30 }
31 if (typeof document.hidden !== 'undefined') {
32 this._hidden = 'hidden';
33 this._visibilityChange = 'visibilitychange';
34 }
35 else if (typeof document['msHidden'] !== 'undefined') {
36 this._hidden = 'msHidden';
37 this._visibilityChange = 'msvisibilitychange';
38 }
39 else if (typeof document['webkitHidden'] !== 'undefined') {
40 this._hidden = 'webkitHidden';
41 this._visibilityChange = 'webkitvisibilitychange';
42 }
43 else {
44 logger.debug('not in the supported web environment');
45 return false;
46 }
47 return true;
48 };
49 SessionTracker.prototype._trackFunc = function () {
50 return tslib_1.__awaiter(this, void 0, void 0, function () {
51 var customAttrs, _a, attributes;
52 return tslib_1.__generator(this, function (_b) {
53 switch (_b.label) {
54 case 0:
55 if (!(typeof this._config.attributes === 'function')) return [3 /*break*/, 2];
56 return [4 /*yield*/, this._config.attributes()];
57 case 1:
58 _a = _b.sent();
59 return [3 /*break*/, 3];
60 case 2:
61 _a = this._config.attributes;
62 _b.label = 3;
63 case 3:
64 customAttrs = _a;
65 attributes = Object.assign({}, customAttrs);
66 if (document.visibilityState === this._hidden) {
67 this._tracker({
68 name: '_session.stop',
69 attributes: attributes,
70 }, this._config.provider).catch(function (e) {
71 logger.debug('record session stop event failed.', e);
72 });
73 }
74 else {
75 this._tracker({
76 name: '_session.start',
77 attributes: attributes,
78 }, this._config.provider).catch(function (e) {
79 logger.debug('record session start event failed.', e);
80 });
81 }
82 return [2 /*return*/];
83 }
84 });
85 });
86 };
87 SessionTracker.prototype._trackBeforeUnload = function (event) {
88 // before unload callback cannot be async => https://github.com/aws-amplify/amplify-js/issues/2088
89 var _this = this;
90 var customAttrs = typeof this._config.attributes === 'function'
91 ? Promise.resolve(this._config.attributes())
92 : Promise.resolve(this._config.attributes);
93 customAttrs.then(function (custom) {
94 var attributes = Object.assign({}, custom);
95 _this._tracker({
96 name: '_session.stop',
97 attributes: attributes,
98 immediate: true,
99 }, _this._config.provider).catch(function (e) {
100 logger.debug('record session stop event failed.', e);
101 });
102 });
103 };
104 // to keep configure a synchronized function
105 SessionTracker.prototype._sendInitialEvent = function () {
106 return tslib_1.__awaiter(this, void 0, void 0, function () {
107 var customAttrs, _a, attributes;
108 return tslib_1.__generator(this, function (_b) {
109 switch (_b.label) {
110 case 0:
111 if (initialEventSent) {
112 logger.debug('the start session has been sent when the page is loaded');
113 return [2 /*return*/];
114 }
115 else {
116 initialEventSent = true;
117 }
118 if (!(typeof this._config.attributes === 'function')) return [3 /*break*/, 2];
119 return [4 /*yield*/, this._config.attributes()];
120 case 1:
121 _a = _b.sent();
122 return [3 /*break*/, 3];
123 case 2:
124 _a = this._config.attributes;
125 _b.label = 3;
126 case 3:
127 customAttrs = _a;
128 attributes = Object.assign({}, customAttrs);
129 this._tracker({
130 name: '_session.start',
131 attributes: attributes,
132 }, this._config.provider).catch(function (e) {
133 logger.debug('record session start event failed.', e);
134 });
135 return [2 /*return*/];
136 }
137 });
138 });
139 };
140 SessionTracker.prototype.configure = function (opts) {
141 if (!this._envCheck()) {
142 return this._config;
143 }
144 Object.assign(this._config, opts);
145 if (this._config.enable && !this._hasEnabled) {
146 // send a start session as soon as it's enabled
147 this._sendInitialEvent();
148 // listen on events
149 document.addEventListener(this._visibilityChange, this._trackFunc, false);
150 window.addEventListener('beforeunload', this._trackBeforeUnload, false);
151 this._hasEnabled = true;
152 }
153 else if (!this._config.enable && this._hasEnabled) {
154 document.removeEventListener(this._visibilityChange, this._trackFunc, false);
155 window.removeEventListener('beforeunload', this._trackBeforeUnload, false);
156 this._hasEnabled = false;
157 }
158 return this._config;
159 };
160 return SessionTracker;
161}());
162exports.SessionTracker = SessionTracker;
163//# sourceMappingURL=SessionTracker.js.map
\No newline at end of file