/
AWSPinpointProvider.ts
815 lines (722 loc) · 21.2 KB
/
AWSPinpointProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import {
ConsoleLogger as Logger,
ClientDevice,
Platform,
Credentials,
Signer,
JS,
Hub,
getAmplifyUserAgent,
} from '@aws-amplify/core';
import {
PinpointClient,
PutEventsCommand,
PutEventsCommandInput,
UpdateEndpointCommand,
GetUserEndpointsCommand,
} from '@aws-sdk/client-pinpoint';
import { EventsBatch } from '@aws-sdk/client-pinpoint/models';
import Cache from '@aws-amplify/cache';
import {
AnalyticsProvider,
PromiseHandlers,
EndpointBuffer,
EventParams,
EventObject,
EndpointFailureData,
} from '../types';
import { v1 as uuid } from 'uuid';
import EventsBuffer from './EventBuffer';
const AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' &&
typeof Symbol.for === 'function'
? Symbol.for('amplify_default')
: '@@amplify_default') as Symbol;
const dispatchAnalyticsEvent = (event, data) => {
Hub.dispatch('analytics', { event, data }, 'Analytics', AMPLIFY_SYMBOL);
};
const logger = new Logger('AWSPinpointProvider');
const RETRYABLE_CODES = [429, 500];
const ACCEPTED_CODES = [202];
const FORBIDDEN_CODE = 403;
const BAD_REQUEST_CODE = 400;
const MOBILE_SERVICE_NAME = 'mobiletargeting';
const EXPIRED_TOKEN_CODE = 'ExpiredTokenException';
const UPDATE_ENDPOINT = '_update_endpoint';
const SESSION_START = '_session.start';
const SESSION_STOP = '_session.stop';
const BEACON_SUPPORTED =
typeof navigator !== 'undefined' &&
navigator &&
typeof navigator.sendBeacon === 'function';
// events buffer
const BUFFER_SIZE = 1000;
const FLUSH_SIZE = 100;
const FLUSH_INTERVAL = 5 * 1000; // 5s
const RESEND_LIMIT = 5;
// params: { event: {name: , .... }, timeStamp, config, resendLimits }
export class AWSPinpointProvider implements AnalyticsProvider {
static category = 'Analytics';
static providerName = 'AWSPinpoint';
private _config;
private pinpointClient;
private _sessionId;
private _sessionStartTimestamp;
private _buffer: EventsBuffer;
private _endpointBuffer: EndpointBuffer;
private _clientInfo;
private _endpointGenerating = true;
private _endpointUpdateInProgress = false;
constructor(config?) {
this._buffer = null;
this._endpointBuffer = [];
this._config = config ? config : {};
this._config.bufferSize = this._config.bufferSize || BUFFER_SIZE;
this._config.flushSize = this._config.flushSize || FLUSH_SIZE;
this._config.flushInterval = this._config.flushInterval || FLUSH_INTERVAL;
this._config.resendLimit = this._config.resendLimit || RESEND_LIMIT;
this._clientInfo = ClientDevice.clientInfo();
}
/**
* get the category of the plugin
*/
getCategory(): string {
return AWSPinpointProvider.category;
}
/**
* get provider name of the plugin
*/
getProviderName(): string {
return AWSPinpointProvider.providerName;
}
/**
* configure the plugin
* @param {Object} config - configuration
*/
public configure(config): object {
logger.debug('configure Analytics', config);
const conf = config || {};
this._config = Object.assign({}, this._config, conf);
if (this._config.appId && !this._config.disabled) {
if (!this._config.endpointId) {
const cacheKey = this.getProviderName() + '_' + this._config.appId;
this._getEndpointId(cacheKey)
.then(endpointId => {
logger.debug('setting endpoint id from the cache', endpointId);
this._config.endpointId = endpointId;
dispatchAnalyticsEvent('pinpointProvider_configured', null);
})
.catch(err => {
logger.debug('Failed to generate endpointId', err);
});
} else {
dispatchAnalyticsEvent('pinpointProvider_configured', null);
}
} else {
this._flushBuffer();
}
return this._config;
}
/**
* record an event
* @param {Object} params - the params of an event
*/
public async record(params: EventParams, handlers: PromiseHandlers) {
logger.debug('_public record', params);
const credentials = await this._getCredentials();
if (!credentials || !this._config.appId || !this._config.region) {
logger.debug(
'cannot send events without credentials, applicationId or region'
);
return handlers.reject(
new Error('No credentials, applicationId or region')
);
}
this._initClients(credentials);
const timestamp = new Date().getTime();
// attach the session and eventId
this._generateSession(params);
params.event.eventId = uuid();
Object.assign(params, { timestamp, config: this._config });
if (params.event.immediate) {
return this._send(params, handlers);
} else {
this._putToBuffer(params, handlers);
}
}
private async _sendEndpointUpdate(endpointObject: EventObject) {
if (this._endpointUpdateInProgress) {
this._endpointBuffer.push(endpointObject);
return;
}
this._endpointUpdateInProgress = true;
await this._updateEndpoint(endpointObject);
const next = this._endpointBuffer.shift();
this._endpointUpdateInProgress = false;
next && this._sendEndpointUpdate(next);
}
/**
* @private
* @param params - params for event recording
* Put events into buffer
*/
private _putToBuffer(params, handlers) {
if (params.event.name === UPDATE_ENDPOINT) {
this._sendEndpointUpdate({ params, handlers });
return;
}
this._buffer && this._buffer.push({ params, handlers });
}
private _generateSession(params) {
this._sessionId = this._sessionId || uuid();
const { event } = params;
switch (event.name) {
case SESSION_START:
// refresh the session id and session start time
this._sessionStartTimestamp = new Date().getTime();
this._sessionId = uuid();
event.session = {
Id: this._sessionId,
StartTimestamp: new Date(this._sessionStartTimestamp).toISOString(),
};
break;
case SESSION_STOP:
const stopTimestamp = new Date().getTime();
this._sessionStartTimestamp =
this._sessionStartTimestamp || new Date().getTime();
this._sessionId = this._sessionId || uuid();
event.session = {
Id: this._sessionId,
Duration: stopTimestamp - this._sessionStartTimestamp,
StartTimestamp: new Date(this._sessionStartTimestamp).toISOString(),
StopTimestamp: new Date(stopTimestamp).toISOString(),
};
this._sessionId = undefined;
this._sessionStartTimestamp = undefined;
break;
default:
this._sessionStartTimestamp =
this._sessionStartTimestamp || new Date().getTime();
this._sessionId = this._sessionId || uuid();
event.session = {
Id: this._sessionId,
StartTimestamp: new Date(this._sessionStartTimestamp).toISOString(),
};
}
}
private async _send(params, handlers) {
const { event } = params;
switch (event.name) {
case UPDATE_ENDPOINT:
return this._updateEndpoint({ params, handlers });
case SESSION_STOP:
return this._pinpointSendStopSession(params, handlers);
default:
return this._pinpointPutEvents(params, handlers);
}
}
private _generateBatchItemContext(params) {
const { event, timestamp, config } = params;
const { name, attributes, metrics, eventId, session } = event;
const { appId, endpointId } = config;
const endpointContext = {};
const eventParams: PutEventsCommandInput = {
ApplicationId: appId,
EventsRequest: {
BatchItem: {},
},
};
const endpointObj: EventsBatch = {} as EventsBatch;
endpointObj.Endpoint = endpointContext;
endpointObj.Events = {
[eventId]: {
EventType: name,
Timestamp: new Date(timestamp).toISOString(),
Attributes: attributes,
Metrics: metrics,
Session: session,
},
};
eventParams.EventsRequest.BatchItem[endpointId] = endpointObj;
return eventParams;
}
private async _pinpointPutEvents(params, handlers) {
const {
event: { eventId },
config: { endpointId },
} = params;
const eventParams = this._generateBatchItemContext(params);
const command: PutEventsCommand = new PutEventsCommand(eventParams);
try {
const data = await this.pinpointClient.send(command);
const {
EventsResponse: {
Results: {
[endpointId]: {
EventsItemResponse: {
[eventId]: { StatusCode, Message },
},
},
},
},
} = data;
if (ACCEPTED_CODES.includes(StatusCode)) {
logger.debug('record event success. ', data);
return handlers.resolve(data);
} else {
if (RETRYABLE_CODES.includes(StatusCode)) {
this._retry(params, handlers);
} else {
logger.error(
`Event ${eventId} is not accepted, the error is ${Message}`
);
return handlers.reject(data);
}
}
} catch (err) {
this._eventError(err);
return handlers.reject(err);
}
}
private _pinpointSendStopSession(params, handlers): Promise<string> {
if (!BEACON_SUPPORTED) {
this._pinpointPutEvents(params, handlers);
return;
}
const eventParams = this._generateBatchItemContext(params);
const { region } = this._config;
const { ApplicationId, EventsRequest } = eventParams;
const accessInfo = {
secret_key: this._config.credentials.secretAccessKey,
access_key: this._config.credentials.accessKeyId,
session_token: this._config.credentials.sessionToken,
};
const url = `https://pinpoint.${region}.amazonaws.com/v1/apps/${ApplicationId}/events/legacy`;
const body = JSON.stringify(EventsRequest);
const method = 'POST';
const request = {
url,
body,
method,
};
const serviceInfo = { region, service: MOBILE_SERVICE_NAME };
const requestUrl: string = Signer.signUrl(
request,
accessInfo,
serviceInfo,
null
);
const success: boolean = navigator.sendBeacon(requestUrl, body);
if (success) {
return handlers.resolve('sendBeacon success');
}
return handlers.reject('sendBeacon failure');
}
private _retry(params, handlers) {
const {
config: { resendLimit },
} = params;
// For backward compatibility
params.resendLimit =
typeof params.resendLimit === 'number' ? params.resendLimit : resendLimit;
if (params.resendLimit-- > 0) {
logger.debug(
`resending event ${params.eventName} with ${params.resendLimit} retry times left`
);
this._pinpointPutEvents(params, handlers);
} else {
logger.debug(`retry times used up for event ${params.eventName}`);
}
}
private async _updateEndpoint(endpointObject: EventObject) {
const { params, handlers } = endpointObject;
const { config, event } = params;
const { appId, endpointId } = config;
const request = this._endpointRequest(
config,
JS.transferKeyToLowerCase(
event,
[],
['attributes', 'userAttributes', 'Attributes', 'UserAttributes']
)
);
const update_params = {
ApplicationId: appId,
EndpointId: endpointId,
EndpointRequest: request,
};
try {
const command: UpdateEndpointCommand = new UpdateEndpointCommand(
update_params
);
const data = await this.pinpointClient.send(command);
logger.debug('updateEndpoint success', data);
this._endpointGenerating = false;
this._resumeBuffer();
handlers.resolve(data);
return;
} catch (err) {
const failureData: EndpointFailureData = {
err,
update_params,
endpointObject,
};
return this._handleEndpointUpdateFailure(failureData);
}
}
private async _handleEndpointUpdateFailure(failureData: EndpointFailureData) {
const { err, endpointObject } = failureData;
const statusCode = err.$metadata && err.$metadata.httpStatusCode;
logger.error('updateEndpoint failed', err);
switch (statusCode) {
case BAD_REQUEST_CODE:
return this._handleEndpointUpdateBadRequest(failureData);
case FORBIDDEN_CODE:
return this._handleEndpointUpdateForbidden(failureData);
default:
if (RETRYABLE_CODES.includes(statusCode)) {
// Server error. Attempt exponential retry
const exponential = true;
return this._retryEndpointUpdate(endpointObject, exponential);
}
endpointObject.handlers.reject(err);
}
}
private async _handleEndpointUpdateBadRequest(
failureData: EndpointFailureData
) {
const { err, update_params, endpointObject } = failureData;
const { message } = err;
const { ApplicationId, EndpointRequest } = update_params;
if (
!String(message).startsWith('Exceeded maximum endpoint per user count')
) {
return endpointObject.handlers.reject(err);
}
try {
await this._removeUnusedEndpoints(
ApplicationId,
EndpointRequest.User.UserId
);
logger.debug('Removed unused endpoints successfully');
this._retryEndpointUpdate(endpointObject);
} catch (err) {
logger.warn(`Failed to remove unused endpoints with error: ${err}`);
logger.warn(
`Please ensure you have updated your Pinpoint IAM Policy ` +
`with the Action: "mobiletargeting:GetUserEndpoints" ` +
`in order to get endpoints info of the user`
);
return endpointObject.handlers.reject(err);
}
}
private _handleEndpointUpdateForbidden(failureData: EndpointFailureData) {
const { err, endpointObject } = failureData;
const { code, retryable } = err;
if (code !== EXPIRED_TOKEN_CODE && !retryable) {
return endpointObject.handlers.reject(err);
}
this._retryEndpointUpdate(endpointObject);
}
private _retryEndpointUpdate(
endpointObject: EventObject,
exponential: boolean = false
) {
logger.debug('_retryEndpointUpdate', endpointObject);
const { params } = endpointObject;
// TODO: implement retry with exp back off once exp function is available
const {
config: { resendLimit },
} = params;
params.resendLimit =
typeof params.resendLimit === 'number' ? params.resendLimit : resendLimit;
if (params.resendLimit-- > 0) {
logger.debug(
`resending endpoint update ${params.event.eventId} with ${params.resendLimit} retry attempts remaining`
);
// insert at the front of endpointBuffer
this._endpointBuffer.length
? this._endpointBuffer.unshift(endpointObject)
: this._updateEndpoint(endpointObject);
return;
}
logger.warn(
`resending endpoint update ${params.event.eventId} failed after ${params.config.resendLimit} attempts`
);
if (this._endpointGenerating) {
logger.error('Initial endpoint update failed. ');
}
}
private async _removeUnusedEndpoints(appId, userId) {
try {
// TODO: re-write with Promise (during refactor pt. 2)
const command: GetUserEndpointsCommand = new GetUserEndpointsCommand({
ApplicationId: appId,
UserId: userId,
});
const data = await this.pinpointClient.send(command);
const endpoints = data.EndpointsResponse.Item;
logger.debug(
`get endpoints associated with the userId: ${userId} with data`,
endpoints
);
let endpointToBeDeleted = endpoints[0];
for (let i = 1; i < endpoints.length; i++) {
const timeStamp1 = Date.parse(endpointToBeDeleted['EffectiveDate']);
const timeStamp2 = Date.parse(endpoints[i]['EffectiveDate']);
// delete the one with invalid effective date
if (isNaN(timeStamp1)) break;
if (isNaN(timeStamp2)) {
endpointToBeDeleted = endpoints[i];
break;
}
if (timeStamp2 < timeStamp1) {
endpointToBeDeleted = endpoints[i];
}
}
// update the endpoint's user id with an empty string
const update_params = {
ApplicationId: appId,
EndpointId: endpointToBeDeleted['Id'],
EndpointRequest: {
User: {
UserId: '',
},
},
};
try {
const updateEndPointcommand: UpdateEndpointCommand = new UpdateEndpointCommand(
update_params
);
const updateEndPointData = await this.pinpointClient.send(
updateEndPointcommand
);
logger.debug(
'The old endpoint is updated with an empty string for user id'
);
return updateEndPointData;
} catch (err) {
logger.debug('Failed to update the endpoint', err);
throw err;
}
} catch (err) {
logger.debug(
`Failed to get endpoints associated with the userId: ${userId} with error`,
err
);
throw err;
}
}
/**
* @private
* @param config
* Init the clients
*/
private async _initClients(credentials) {
logger.debug('init clients');
if (
this.pinpointClient &&
this._config.credentials &&
this._config.credentials.sessionToken === credentials.sessionToken &&
this._config.credentials.identityId === credentials.identityId
) {
logger.debug('no change for aws credentials, directly return from init');
return;
}
const identityId = this._config.credentials
? this._config.credentials.identityId
: null;
this._config.credentials = credentials;
const { region } = this._config;
logger.debug('init clients with credentials', credentials);
this.pinpointClient = new PinpointClient({
region,
credentials,
customUserAgent: getAmplifyUserAgent(),
});
if (this._bufferExists() && identityId === credentials.identityId) {
// if the identity has remained the same, pass the updated client to the buffer
this._updateBufferClient();
} else {
// otherwise flush the buffer and instantiate a new one
// this will cause the old buffer to send any remaining events
// with the old credentials and then stop looping and shortly thereafter get picked up by GC
this._initBuffer();
}
this._customizePinpointClientReq();
}
private _bufferExists() {
return this._buffer && this._buffer instanceof EventsBuffer;
}
private _initBuffer() {
if (this._bufferExists()) {
this._flushBuffer();
}
this._buffer = new EventsBuffer(this.pinpointClient, this._config);
// if the first endpoint update hasn't yet resolved pause the buffer to
// prevent race conditions. It will be resumed as soon as that request succeeds
if (this._endpointGenerating) {
this._buffer.pause();
}
}
private _updateBufferClient() {
if (this._bufferExists()) {
this._buffer.updateClient(this.pinpointClient);
}
}
private _flushBuffer() {
if (this._bufferExists()) {
this._buffer.flush();
this._buffer = null;
}
}
private _resumeBuffer() {
if (this._bufferExists()) {
this._buffer.resume();
}
}
private _customizePinpointClientReq() {
// TODO FIXME: Find a middleware to do this with AWS V3 SDK
// if (Platform.isReactNative) {
// this.pinpointClient.customizeRequests(request => {
// request.on('build', req => {
// req.httpRequest.headers['user-agent'] = Platform.userAgent;
// });
// });
// }
}
private async _getEndpointId(cacheKey) {
// try to get from cache
let endpointId = await Cache.getItem(cacheKey);
logger.debug(
'endpointId from cache',
endpointId,
'type',
typeof endpointId
);
if (!endpointId) {
endpointId = uuid();
Cache.setItem(cacheKey, endpointId);
}
return endpointId;
}
/**
* EndPoint request
* @return {Object} - The request of updating endpoint
*/
private _endpointRequest(config, event) {
const { credentials } = config;
const clientInfo = this._clientInfo || {};
const clientContext = config.clientContext || {};
// for now we have three different ways for default endpoint configurations
// clientInfo
// clientContext (deprecated)
// config.endpoint
const defaultEndpointConfig = config.endpoint || {};
const demographicByClientInfo = {
appVersion: clientInfo.appVersion,
make: clientInfo.make,
model: clientInfo.model,
modelVersion: clientInfo.version,
platform: clientInfo.platform,
};
// for backward compatibility
const {
clientId,
appTitle,
appVersionName,
appVersionCode,
appPackageName,
...demographicByClientContext
} = clientContext;
const channelType = event.address
? clientInfo.platform === 'android'
? 'GCM'
: 'APNS'
: undefined;
const tmp = {
channelType,
requestId: uuid(),
effectiveDate: new Date().toISOString(),
...defaultEndpointConfig,
...event,
attributes: {
...defaultEndpointConfig.attributes,
...event.attributes,
},
demographic: {
...demographicByClientInfo,
...demographicByClientContext,
...defaultEndpointConfig.demographic,
...event.demographic,
},
location: {
...defaultEndpointConfig.location,
...event.location,
},
metrics: {
...defaultEndpointConfig.metrics,
...event.metrics,
},
user: {
userId:
event.userId ||
defaultEndpointConfig.userId ||
credentials.identityId,
userAttributes: {
...defaultEndpointConfig.userAttributes,
...event.userAttributes,
},
},
};
// eliminate unnecessary params
const {
userId,
userAttributes,
name,
session,
eventId,
immediate,
...ret
} = tmp;
return JS.transferKeyToUpperCase(
ret,
[],
['metrics', 'userAttributes', 'attributes']
);
}
private _eventError(err: any) {
logger.error('record event failed.', err);
logger.warn(
`Please ensure you have updated your Pinpoint IAM Policy ` +
`with the Action: "mobiletargeting:PutEvents" ` +
`in order to record events`
);
}
private async _getCredentials() {
try {
const credentials = await Credentials.get();
if (!credentials) return null;
logger.debug('set credentials for analytics', credentials);
return Credentials.shear(credentials);
} catch (err) {
logger.debug('ensure credentials error', err);
return null;
}
}
}
/**
* @deprecated use named import
*/
export default AWSPinpointProvider;