1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
14 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
15 | return new (P || (P = Promise))(function (resolve, reject) {
|
16 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
17 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
18 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
19 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
20 | });
|
21 | };
|
22 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
23 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
24 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
25 | function verb(n) { return function (v) { return step([n, v]); }; }
|
26 | function step(op) {
|
27 | if (f) throw new TypeError("Generator is already executing.");
|
28 | while (_) try {
|
29 | 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;
|
30 | if (y = 0, t) op = [op[0] & 2, t.value];
|
31 | switch (op[0]) {
|
32 | case 0: case 1: t = op; break;
|
33 | case 4: _.label++; return { value: op[1], done: false };
|
34 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
35 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
36 | default:
|
37 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
38 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
39 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
40 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
41 | if (t[2]) _.ops.pop();
|
42 | _.trys.pop(); continue;
|
43 | }
|
44 | op = body.call(thisArg, _);
|
45 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
46 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
47 | }
|
48 | };
|
49 |
|
50 | import { ConsoleLogger as Logger } from '@aws-amplify/core';
|
51 | import { AppState } from 'react-native';
|
52 | var logger = new Logger('SessionTracker');
|
53 | var defaultOpts = {
|
54 | enable: false,
|
55 | provider: 'AWSPinpoint',
|
56 | };
|
57 | var initialEventSent = false;
|
58 | var SessionTracker = (function () {
|
59 | function SessionTracker(tracker, opts) {
|
60 | this._config = Object.assign({}, defaultOpts, opts);
|
61 | this._tracker = tracker;
|
62 | this._hasEnabled = false;
|
63 | this._trackFunc = this._trackFunc.bind(this);
|
64 | this._currentState = AppState.currentState;
|
65 | this.configure(this._config);
|
66 | }
|
67 | SessionTracker.prototype._envCheck = function () {
|
68 | if (!AppState) {
|
69 | logger.debug('not in the supported react native environment');
|
70 | return false;
|
71 | }
|
72 | return true;
|
73 | };
|
74 | SessionTracker.prototype._trackFunc = function (nextAppState) {
|
75 | return __awaiter(this, void 0, void 0, function () {
|
76 | var customAttrs, _a, attributes;
|
77 | return __generator(this, function (_b) {
|
78 | switch (_b.label) {
|
79 | case 0:
|
80 | if (!(typeof this._config.attributes === 'function')) return [3 , 2];
|
81 | return [4 , this._config.attributes()];
|
82 | case 1:
|
83 | _a = _b.sent();
|
84 | return [3 , 3];
|
85 | case 2:
|
86 | _a = this._config.attributes;
|
87 | _b.label = 3;
|
88 | case 3:
|
89 | customAttrs = _a;
|
90 | attributes = Object.assign({}, customAttrs);
|
91 | if (this._currentState.match(/inactive|background/) &&
|
92 | nextAppState === 'active') {
|
93 | logger.debug('App has come to the foreground, recording start session');
|
94 | this._tracker({
|
95 | name: '_session.start',
|
96 | attributes: attributes,
|
97 | immediate: false,
|
98 | }, this._config.provider).catch(function (e) {
|
99 | logger.debug('record session start event failed.', e);
|
100 | });
|
101 | }
|
102 | if (this._currentState.match(/active/) &&
|
103 | nextAppState.match(/inactive|background/)) {
|
104 | logger.debug('App has come to inactive/background, recording stop session');
|
105 | this._tracker({
|
106 | name: '_session.stop',
|
107 | attributes: attributes,
|
108 | immediate: false,
|
109 | }, this._config.provider).catch(function (e) {
|
110 | logger.debug('record session stop event failed.', e);
|
111 | });
|
112 | }
|
113 | this._currentState = nextAppState;
|
114 | return [2 ];
|
115 | }
|
116 | });
|
117 | });
|
118 | };
|
119 |
|
120 | SessionTracker.prototype._sendInitialEvent = function () {
|
121 | return __awaiter(this, void 0, void 0, function () {
|
122 | var customAttrs, _a, attributes;
|
123 | return __generator(this, function (_b) {
|
124 | switch (_b.label) {
|
125 | case 0:
|
126 | if (initialEventSent) {
|
127 | logger.debug('the start session has been sent when the page is loaded');
|
128 | return [2 ];
|
129 | }
|
130 | else {
|
131 | initialEventSent = true;
|
132 | }
|
133 | if (!(typeof this._config.attributes === 'function')) return [3 , 2];
|
134 | return [4 , this._config.attributes()];
|
135 | case 1:
|
136 | _a = _b.sent();
|
137 | return [3 , 3];
|
138 | case 2:
|
139 | _a = this._config.attributes;
|
140 | _b.label = 3;
|
141 | case 3:
|
142 | customAttrs = _a;
|
143 | attributes = Object.assign({}, customAttrs);
|
144 | this._tracker({
|
145 | name: '_session.start',
|
146 | attributes: attributes,
|
147 | immediate: false,
|
148 | }, this._config.provider).catch(function (e) {
|
149 | logger.debug('record session start event failed.', e);
|
150 | });
|
151 | return [2 ];
|
152 | }
|
153 | });
|
154 | });
|
155 | };
|
156 | SessionTracker.prototype.configure = function (opts) {
|
157 | if (!this._envCheck()) {
|
158 | return this._config;
|
159 | }
|
160 | Object.assign(this._config, opts);
|
161 | if (this._config.enable && !this._hasEnabled) {
|
162 |
|
163 | this._sendInitialEvent();
|
164 |
|
165 | AppState.addEventListener('change', this._trackFunc, false);
|
166 | this._hasEnabled = true;
|
167 | }
|
168 | else if (!this._config.enable && this._hasEnabled) {
|
169 | AppState.removeEventListener('change', this._trackFunc, false);
|
170 | this._hasEnabled = false;
|
171 | }
|
172 | return this._config;
|
173 | };
|
174 | return SessionTracker;
|
175 | }());
|
176 | export { SessionTracker };
|
177 |
|
178 |
|
179 |
|
180 | export default SessionTracker;
|
181 |
|
\ | No newline at end of file |