UNPKG

16.6 kBJavaScriptView Raw
1/*
2 * Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5 * the License. A copy of the License is located at
6 *
7 * http://aws.amazon.com/apache2.0/
8 *
9 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11 * and limitations under the License.
12 */
13var __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};
22var __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};
49import { ConsoleLogger as Logger, Credentials, JS, getAmplifyUserAgent, } from '@aws-amplify/core';
50import { PersonalizeEventsClient, PutEventsCommand, } from '@aws-sdk/client-personalize-events';
51import { SessionInfoManager, MediaAutoTrack, } from './AmazonPersonalizeHelper';
52import get from 'lodash/get';
53import isEmpty from 'lodash/isEmpty';
54import isEqual from 'lodash/isEqual';
55var logger = new Logger('AmazonPersonalizeProvider');
56// events buffer
57var FLUSH_SIZE = 5;
58var FLUSH_SIZE_THRESHHOLD = 10;
59var FLUSH_INTERVAL = 5 * 1000; // 5s
60var IDENTIFY_EVENT = 'Identify';
61var AmazonPersonalizeProvider = /** @class */ (function () {
62 function AmazonPersonalizeProvider(config) {
63 this._buffer = [];
64 this._config = config ? config : {};
65 this._config.flushSize =
66 this._config.flushSize > 0 &&
67 this._config.flushSize <= FLUSH_SIZE_THRESHHOLD
68 ? this._config.flushSize
69 : FLUSH_SIZE;
70 this._config.flushInterval = this._config.flushInterval || FLUSH_INTERVAL;
71 this._sessionManager = new SessionInfoManager();
72 if (!isEmpty(this._config.trackingId)) {
73 this._sessionInfo = this._sessionManager.retrieveSessionInfo(this._config.trackingId);
74 }
75 this._isBrowser = JS.browserOrNode().isBrowser;
76 // flush event buffer
77 this._setupTimer();
78 }
79 AmazonPersonalizeProvider.prototype._setupTimer = function () {
80 if (this._timer) {
81 clearInterval(this._timer);
82 }
83 var flushInterval = this._config.flushInterval;
84 var that = this;
85 this._timer = setInterval(function () {
86 that._sendFromBuffer();
87 }, flushInterval);
88 };
89 /**
90 * Record event
91 * @param eventType - type of the event action. e.g. "Click"
92 * @param properties - properties of the event
93 * @return Promise
94 */
95 AmazonPersonalizeProvider.prototype.record = function (params) {
96 return __awaiter(this, void 0, void 0, function () {
97 var credentials, _a, eventType, properties, requestParams, isLoaded;
98 return __generator(this, function (_b) {
99 switch (_b.label) {
100 case 0: return [4 /*yield*/, this._getCredentials()];
101 case 1:
102 credentials = _b.sent();
103 if (!credentials)
104 return [2 /*return*/, Promise.resolve(false)];
105 Object.assign(params, {
106 config: this._config,
107 credentials: credentials,
108 sentAt: new Date(),
109 });
110 _a = params.event, eventType = _a.eventType, properties = _a.properties;
111 if (eventType === IDENTIFY_EVENT) {
112 this._sessionManager.updateSessionInfo(properties && properties.userId ? properties.userId : '', this._sessionInfo);
113 return [2 /*return*/];
114 }
115 else if (!isEmpty(params.event.userId)) {
116 this._sessionManager.updateSessionInfo(params.event.userId, this._sessionInfo);
117 }
118 requestParams = this.generateRequestParams(params, this._sessionInfo);
119 if (!(eventType === 'MediaAutoTrack')) return [3 /*break*/, 7];
120 if (!this._isBrowser) return [3 /*break*/, 5];
121 if (!!isEmpty(get(requestParams, 'eventData.properties.domElementId', null))) return [3 /*break*/, 3];
122 return [4 /*yield*/, this.isElementFullyLoaded(this.loadElement, requestParams.eventData.properties['domElementId'], 500, 5)];
123 case 2:
124 isLoaded = _b.sent();
125 if (isLoaded) {
126 new MediaAutoTrack(requestParams, this);
127 }
128 else {
129 logger.debug('Cannot find the media element.');
130 }
131 return [3 /*break*/, 4];
132 case 3:
133 logger.debug("Missing domElementId field in 'properties' for MediaAutoTrack event type.");
134 _b.label = 4;
135 case 4: return [3 /*break*/, 6];
136 case 5:
137 logger.debug('MediaAutoTrack only for browser');
138 _b.label = 6;
139 case 6: return [2 /*return*/];
140 case 7: return [2 /*return*/, this.putToBuffer(requestParams)];
141 }
142 });
143 });
144 };
145 AmazonPersonalizeProvider.prototype.loadElement = function (domId) {
146 return new Promise(function (resolve, reject) {
147 if (document.getElementById(domId) &&
148 document.getElementById(domId).clientHeight) {
149 return resolve(true);
150 }
151 else {
152 return reject(true);
153 }
154 });
155 };
156 AmazonPersonalizeProvider.prototype.isElementFullyLoaded = function (operation, params, delay, times) {
157 var _this = this;
158 var wait = function (ms) { return new Promise(function (r) { return setTimeout(r, ms); }); };
159 return new Promise(function (resolve, reject) {
160 return operation(params)
161 .then(resolve)
162 .catch(function (reason) {
163 if (times - 1 > 0) {
164 return wait(delay)
165 .then(_this.isElementFullyLoaded.bind(null, operation, params, delay, times - 1))
166 .then(resolve)
167 .catch(reject);
168 }
169 return reject(reason);
170 });
171 });
172 };
173 /**
174 * get the category of the plugin
175 */
176 AmazonPersonalizeProvider.prototype.getCategory = function () {
177 return 'Analytics';
178 };
179 /**
180 * get provider name of the plugin
181 */
182 AmazonPersonalizeProvider.prototype.getProviderName = function () {
183 return 'AmazonPersonalize';
184 };
185 /**
186 * configure the plugin
187 * @param {Object} config - configuration
188 */
189 AmazonPersonalizeProvider.prototype.configure = function (config) {
190 logger.debug('configure Analytics', config);
191 var conf = config ? config : {};
192 this._config = Object.assign({}, this._config, conf);
193 if (!isEmpty(this._config.trackingId)) {
194 this._sessionInfo = this._sessionManager.retrieveSessionInfo(this._config.trackingId);
195 }
196 this._setupTimer();
197 return this._config;
198 };
199 /**
200 * Generate the requestParams from customer input params and sessionInfo
201 * @private
202 * @param eventData - customer input for event data
203 * @param api - api name
204 * @return RequestParams - wrapper object with all information required for make request
205 */
206 AmazonPersonalizeProvider.prototype.generateRequestParams = function (params, sessionInfo) {
207 var requestParams = {};
208 var _a = params.event, eventType = _a.eventType, properties = _a.properties;
209 requestParams.eventData = { eventType: eventType, properties: properties };
210 requestParams.sessionInfo = sessionInfo;
211 requestParams.sentAt = params.sentAt;
212 requestParams.credentials = params.credentials;
213 requestParams.config = params.config;
214 return requestParams;
215 };
216 /**
217 * record an event
218 * @param {Object} params - the params of an event
219 */
220 AmazonPersonalizeProvider.prototype._sendEvents = function (group) {
221 var groupLen = group.length;
222 if (groupLen === 0) {
223 logger.debug('events array is empty, directly return');
224 return;
225 }
226 var _a = group[0], config = _a.config, credentials = _a.credentials, sessionInfo = _a.sessionInfo;
227 var initClients = this._init(config, credentials);
228 if (!initClients)
229 return false;
230 if (groupLen > 0) {
231 var events = [];
232 for (var i = 0; i < groupLen; i += 1) {
233 var params = group.shift();
234 var eventPayload = this._generateSingleRecordPayload(params, sessionInfo);
235 events.push(eventPayload);
236 }
237 var payload_1 = {};
238 payload_1.trackingId = sessionInfo.trackingId;
239 payload_1.sessionId = sessionInfo.sessionId;
240 payload_1.userId = sessionInfo.userId;
241 payload_1.eventList = [];
242 events.forEach(function (event) {
243 // @ts-ignore
244 payload_1.eventList.push(event);
245 });
246 var command = new PutEventsCommand(payload_1);
247 this._personalize.send(command, function (err) {
248 if (err)
249 logger.debug('Failed to call putEvents in Personalize', err);
250 else
251 logger.debug('Put events');
252 });
253 }
254 };
255 /**
256 * Put event into buffer
257 * @private
258 * @param params - params for the event recording
259 */
260 AmazonPersonalizeProvider.prototype.putToBuffer = function (params) {
261 if (this._buffer.length < this._config.flushSize) {
262 this._buffer.push(params);
263 }
264 else {
265 this._buffer.push(params);
266 this._sendFromBuffer();
267 }
268 return Promise.resolve(true);
269 };
270 /**
271 * flush the buffer and batch sending the request
272 * @private
273 * @param eventsParams - the buffer for cache the payload
274 */
275 AmazonPersonalizeProvider.prototype._sendFromBuffer = function () {
276 var _this = this;
277 var size = this._buffer.length;
278 if (size <= 0)
279 return;
280 var eventsGroups = [];
281 var preCred = null;
282 var group = [];
283 for (var i = 0; i < size; i += 1) {
284 var currRequestParams = this._buffer.shift();
285 var cred = currRequestParams.credentials;
286 var sessionInfo = currRequestParams.sessionInfo;
287 if (i === 0) {
288 group.push(currRequestParams);
289 preCred = cred;
290 }
291 else {
292 if (isEqual(sessionInfo, this._sessionInfo) &&
293 cred.sessionToken === preCred.sessionToken &&
294 cred.identityId === preCred.identityId) {
295 logger.debug('no change for cred, put event in the same group');
296 group.push(currRequestParams);
297 }
298 else {
299 eventsGroups.push(group);
300 group = [];
301 group.push(currRequestParams);
302 preCred = cred;
303 this._sessionInfo = sessionInfo;
304 }
305 }
306 }
307 eventsGroups.push(group);
308 eventsGroups.map(function (group) {
309 _this._sendEvents(group);
310 });
311 };
312 /**
313 * Generate the record payload for single event
314 * @private
315 * @param params - RequestParams
316 */
317 AmazonPersonalizeProvider.prototype._generateSingleRecordPayload = function (params, sessionInfo) {
318 var eventData = params.eventData, sentAt = params.sentAt;
319 var trackPayload = {};
320 trackPayload.sentAt = sentAt;
321 trackPayload.properties =
322 eventData.properties && JSON.stringify(eventData.properties);
323 trackPayload.eventId =
324 this._sessionManager.getTimerKey() + sessionInfo.sessionId;
325 trackPayload.eventType = eventData.eventType;
326 return trackPayload;
327 };
328 /**
329 * Initialize the personalize client
330 * @private
331 * @param params - RequestParams
332 */
333 AmazonPersonalizeProvider.prototype._init = function (config, credentials) {
334 logger.debug('init clients');
335 if (this._personalize &&
336 this._config.credentials &&
337 this._config.credentials.sessionToken === credentials.sessionToken &&
338 this._config.credentials.identityId === credentials.identityId) {
339 logger.debug('no change for analytics config, directly return from init');
340 return true;
341 }
342 this._config.credentials = credentials;
343 var region = config.region;
344 logger.debug('initialize personalize with credentials', credentials);
345 this._personalize = new PersonalizeEventsClient({
346 region: region,
347 credentials: credentials,
348 customUserAgent: getAmplifyUserAgent(),
349 });
350 return true;
351 };
352 /**
353 * check if current credentials exists
354 * @private
355 */
356 AmazonPersonalizeProvider.prototype._getCredentials = function () {
357 var that = this;
358 return Credentials.get()
359 .then(function (credentials) {
360 if (!credentials)
361 return null;
362 logger.debug('set credentials for analytics', that._config.credentials);
363 return Credentials.shear(credentials);
364 })
365 .catch(function (err) {
366 logger.debug('ensure credentials error', err);
367 return null;
368 });
369 };
370 return AmazonPersonalizeProvider;
371}());
372export { AmazonPersonalizeProvider };
373/**
374 * @deprecated use named import
375 */
376export default AmazonPersonalizeProvider;
377//# sourceMappingURL=AmazonPersonalizeProvider.js.map
\No newline at end of file