@aws-amplify/analytics 7.0.1-api-v6-models.9351bcf.0 → 7.0.1-api-v6-models.891fe0d.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/lib/apis/disable.js +1 -1
  2. package/lib/apis/enable.js +1 -1
  3. package/lib/errors/AnalyticsError.js +7 -11
  4. package/lib/errors/assertValidationError.js +6 -6
  5. package/lib/errors/validation.js +10 -11
  6. package/lib/providers/kinesis/apis/flushEvents.js +20 -23
  7. package/lib/providers/kinesis/apis/record.js +24 -26
  8. package/lib/providers/kinesis/utils/getEventBuffer.js +39 -70
  9. package/lib/providers/kinesis/utils/resolveConfig.js +14 -13
  10. package/lib/providers/kinesis-firehose/apis/flushEvents.js +19 -22
  11. package/lib/providers/kinesis-firehose/apis/record.js +22 -24
  12. package/lib/providers/kinesis-firehose/utils/getEventBuffer.js +39 -68
  13. package/lib/providers/kinesis-firehose/utils/resolveConfig.js +14 -13
  14. package/lib/providers/personalize/apis/flushEvents.js +18 -21
  15. package/lib/providers/personalize/apis/record.js +55 -71
  16. package/lib/providers/personalize/utils/autoTrackMedia.js +96 -98
  17. package/lib/providers/personalize/utils/cachedSession.js +30 -42
  18. package/lib/providers/personalize/utils/getEventBuffer.js +35 -62
  19. package/lib/providers/personalize/utils/resolveConfig.js +14 -13
  20. package/lib/providers/pinpoint/apis/configureAutoTrack.js +6 -6
  21. package/lib/providers/pinpoint/apis/flushEvents.js +10 -13
  22. package/lib/providers/pinpoint/apis/identifyUser.js +18 -30
  23. package/lib/providers/pinpoint/apis/record.js +15 -16
  24. package/lib/providers/pinpoint/utils/resolveConfig.js +5 -6
  25. package/lib/providers/pinpoint/utils/resolveCredentials.js +7 -15
  26. package/lib/setupTests.js +1 -1
  27. package/lib/trackers/EventTracker.js +39 -45
  28. package/lib/trackers/PageViewTracker.js +41 -45
  29. package/lib/trackers/SessionTracker.js +25 -31
  30. package/lib/trackers/SessionTracker.native.js +25 -31
  31. package/lib/tsconfig.tsbuildinfo +1 -1
  32. package/lib/utils/eventBuffer/EventBuffer.js +34 -62
  33. package/lib/utils/groupBy.js +4 -7
  34. package/lib/utils/resolveCredentials.js +7 -15
  35. package/lib/utils/statusHelpers.js +4 -4
  36. package/lib/utils/trackerConfigHelpers.js +2 -2
  37. package/lib/utils/trackerConfigHelpers.native.js +2 -2
  38. package/lib/utils/trackerHelpers.js +5 -5
  39. package/lib/utils/userAgent.js +3 -3
  40. package/lib-esm/apis/disable.js +1 -1
  41. package/lib-esm/apis/enable.js +1 -1
  42. package/lib-esm/errors/AnalyticsError.js +6 -11
  43. package/lib-esm/errors/assertValidationError.js +4 -4
  44. package/lib-esm/errors/validation.js +10 -11
  45. package/lib-esm/providers/kinesis/apis/flushEvents.js +15 -18
  46. package/lib-esm/providers/kinesis/apis/record.js +18 -20
  47. package/lib-esm/providers/kinesis/utils/constants.js +1 -1
  48. package/lib-esm/providers/kinesis/utils/getEventBuffer.js +37 -68
  49. package/lib-esm/providers/kinesis/utils/resolveConfig.js +11 -10
  50. package/lib-esm/providers/kinesis-firehose/apis/flushEvents.js +15 -18
  51. package/lib-esm/providers/kinesis-firehose/apis/record.js +17 -19
  52. package/lib-esm/providers/kinesis-firehose/utils/constants.js +1 -1
  53. package/lib-esm/providers/kinesis-firehose/utils/getEventBuffer.js +37 -66
  54. package/lib-esm/providers/kinesis-firehose/utils/resolveConfig.js +11 -10
  55. package/lib-esm/providers/personalize/apis/flushEvents.js +14 -17
  56. package/lib-esm/providers/personalize/apis/record.js +50 -66
  57. package/lib-esm/providers/personalize/utils/autoTrackMedia.js +94 -96
  58. package/lib-esm/providers/personalize/utils/cachedSession.js +28 -40
  59. package/lib-esm/providers/personalize/utils/constants.js +4 -4
  60. package/lib-esm/providers/personalize/utils/getEventBuffer.js +33 -60
  61. package/lib-esm/providers/personalize/utils/resolveConfig.js +11 -10
  62. package/lib-esm/providers/pinpoint/apis/configureAutoTrack.js +4 -4
  63. package/lib-esm/providers/pinpoint/apis/flushEvents.js +5 -8
  64. package/lib-esm/providers/pinpoint/apis/identifyUser.js +14 -26
  65. package/lib-esm/providers/pinpoint/apis/record.js +9 -10
  66. package/lib-esm/providers/pinpoint/utils/resolveConfig.js +3 -4
  67. package/lib-esm/providers/pinpoint/utils/resolveCredentials.js +5 -13
  68. package/lib-esm/setupTests.js +1 -1
  69. package/lib-esm/trackers/EventTracker.js +37 -44
  70. package/lib-esm/trackers/PageViewTracker.js +39 -44
  71. package/lib-esm/trackers/SessionTracker.js +22 -29
  72. package/lib-esm/trackers/SessionTracker.native.js +22 -29
  73. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  74. package/lib-esm/utils/eventBuffer/EventBuffer.js +33 -62
  75. package/lib-esm/utils/groupBy.js +4 -7
  76. package/lib-esm/utils/resolveCredentials.js +5 -13
  77. package/lib-esm/utils/statusHelpers.js +4 -4
  78. package/lib-esm/utils/trackerConfigHelpers.js +1 -1
  79. package/lib-esm/utils/trackerConfigHelpers.native.js +1 -1
  80. package/lib-esm/utils/trackerHelpers.js +4 -4
  81. package/lib-esm/utils/userAgent.js +2 -2
  82. package/package.json +6 -9
  83. package/src/providers/kinesis/utils/getEventBuffer.ts +6 -7
  84. package/src/providers/kinesis-firehose/utils/getEventBuffer.ts +6 -7
  85. package/src/providers/personalize/utils/getEventBuffer.ts +6 -7
  86. package/src/trackers/SessionTracker.native.ts +2 -3
  87. package/src/trackers/SessionTracker.ts +2 -3
@@ -1,20 +1,21 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { __assign } from "tslib";
4
3
  import { Amplify } from '@aws-amplify/core';
5
4
  import { AnalyticsValidationErrorCode, assertValidationError, } from '../../../errors';
6
5
  import { DEFAULT_KINESIS_CONFIG } from './constants';
7
- export var resolveConfig = function () {
8
- var _a;
9
- var config = (_a = Amplify.getConfig().Analytics) === null || _a === void 0 ? void 0 : _a.Kinesis;
10
- var _b = __assign(__assign({}, DEFAULT_KINESIS_CONFIG), config), region = _b.region, _c = _b.bufferSize, bufferSize = _c === void 0 ? DEFAULT_KINESIS_CONFIG.bufferSize : _c, _d = _b.flushSize, flushSize = _d === void 0 ? DEFAULT_KINESIS_CONFIG.flushSize : _d, _e = _b.flushInterval, flushInterval = _e === void 0 ? DEFAULT_KINESIS_CONFIG.flushInterval : _e, resendLimit = _b.resendLimit;
6
+ export const resolveConfig = () => {
7
+ const config = Amplify.getConfig().Analytics?.Kinesis;
8
+ const { region, bufferSize = DEFAULT_KINESIS_CONFIG.bufferSize, flushSize = DEFAULT_KINESIS_CONFIG.flushSize, flushInterval = DEFAULT_KINESIS_CONFIG.flushInterval, resendLimit, } = {
9
+ ...DEFAULT_KINESIS_CONFIG,
10
+ ...config,
11
+ };
11
12
  assertValidationError(!!region, AnalyticsValidationErrorCode.NoRegion);
12
13
  assertValidationError(flushSize < bufferSize, AnalyticsValidationErrorCode.InvalidFlushSize);
13
14
  return {
14
- region: region,
15
- bufferSize: bufferSize,
16
- flushSize: flushSize,
17
- flushInterval: flushInterval,
18
- resendLimit: resendLimit,
15
+ region,
16
+ bufferSize,
17
+ flushSize,
18
+ flushInterval,
19
+ resendLimit,
19
20
  };
20
21
  };
@@ -4,7 +4,7 @@ import { getEventBuffer, resolveConfig } from '../utils';
4
4
  import { getAnalyticsUserAgentString, resolveCredentials, } from '../../../utils';
5
5
  import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
6
6
  import { ConsoleLogger } from '@aws-amplify/core';
7
- var logger = new ConsoleLogger('KinesisFirehose');
7
+ const logger = new ConsoleLogger('KinesisFirehose');
8
8
  /**
9
9
  * Flushes all buffered Kinesis events to the service.
10
10
  *
@@ -12,22 +12,19 @@ var logger = new ConsoleLogger('KinesisFirehose');
12
12
  * This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
13
13
  * this API may not be included in the flush.
14
14
  */
15
- export var flushEvents = function () {
16
- var _a = resolveConfig(), region = _a.region, flushSize = _a.flushSize, flushInterval = _a.flushInterval, bufferSize = _a.bufferSize, resendLimit = _a.resendLimit;
15
+ export const flushEvents = () => {
16
+ const { region, flushSize, flushInterval, bufferSize, resendLimit } = resolveConfig();
17
17
  resolveCredentials()
18
- .then(function (_a) {
19
- var credentials = _a.credentials, identityId = _a.identityId;
20
- return getEventBuffer({
21
- region: region,
22
- flushSize: flushSize,
23
- flushInterval: flushInterval,
24
- bufferSize: bufferSize,
25
- credentials: credentials,
26
- identityId: identityId,
27
- resendLimit: resendLimit,
28
- userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
29
- });
30
- })
31
- .then(function (eventBuffer) { return eventBuffer.flushAll(); })
32
- .catch(function (e) { return logger.warn('Failed to flush events.', e); });
18
+ .then(({ credentials, identityId }) => getEventBuffer({
19
+ region,
20
+ flushSize,
21
+ flushInterval,
22
+ bufferSize,
23
+ credentials,
24
+ identityId,
25
+ resendLimit,
26
+ userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
27
+ }))
28
+ .then(eventBuffer => eventBuffer.flushAll())
29
+ .catch(e => logger.warn('Failed to flush events.', e));
33
30
  };
@@ -5,37 +5,35 @@ import { getAnalyticsUserAgentString, isAnalyticsEnabled, resolveCredentials, }
5
5
  import { fromUtf8 } from '@smithy/util-utf8';
6
6
  import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
7
7
  import { ConsoleLogger } from '@aws-amplify/core';
8
- var logger = new ConsoleLogger('KinesisFirehose');
9
- export var record = function (_a) {
10
- var streamName = _a.streamName, data = _a.data;
8
+ const logger = new ConsoleLogger('KinesisFirehose');
9
+ export const record = ({ streamName, data }) => {
11
10
  if (!isAnalyticsEnabled()) {
12
11
  logger.debug('Analytics is disabled, event will not be recorded.');
13
12
  return;
14
13
  }
15
- var timestamp = Date.now();
16
- var _b = resolveConfig(), region = _b.region, bufferSize = _b.bufferSize, flushSize = _b.flushSize, flushInterval = _b.flushInterval, resendLimit = _b.resendLimit;
14
+ const timestamp = Date.now();
15
+ const { region, bufferSize, flushSize, flushInterval, resendLimit } = resolveConfig();
17
16
  resolveCredentials()
18
- .then(function (_a) {
19
- var credentials = _a.credentials, identityId = _a.identityId;
20
- var buffer = getEventBuffer({
21
- region: region,
22
- credentials: credentials,
23
- identityId: identityId,
24
- bufferSize: bufferSize,
25
- flushSize: flushSize,
26
- flushInterval: flushInterval,
27
- resendLimit: resendLimit,
17
+ .then(({ credentials, identityId }) => {
18
+ const buffer = getEventBuffer({
19
+ region,
20
+ credentials,
21
+ identityId,
22
+ bufferSize,
23
+ flushSize,
24
+ flushInterval,
25
+ resendLimit,
28
26
  userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
29
27
  });
30
28
  buffer.append({
31
- region: region,
32
- streamName: streamName,
29
+ region,
30
+ streamName,
33
31
  event: ArrayBuffer.isView(data) ? data : fromUtf8(JSON.stringify(data)),
34
- timestamp: timestamp,
32
+ timestamp,
35
33
  retryCount: 0,
36
34
  });
37
35
  })
38
- .catch(function (e) {
36
+ .catch(e => {
39
37
  logger.warn('Failed to record event.', e);
40
38
  });
41
39
  };
@@ -1,6 +1,6 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- export var DEFAULT_KINESIS_FIREHOSE_CONFIG = {
3
+ export const DEFAULT_KINESIS_FIREHOSE_CONFIG = {
4
4
  bufferSize: 1000,
5
5
  flushSize: 100,
6
6
  flushInterval: 5000,
@@ -1,6 +1,5 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { __assign, __awaiter, __generator, __read, __values } from "tslib";
4
3
  import { EventBuffer, groupBy } from '../../../utils';
5
4
  import { FirehoseClient, PutRecordBatchCommand, } from '@aws-sdk/client-firehose';
6
5
  /**
@@ -11,85 +10,57 @@ import { FirehoseClient, PutRecordBatchCommand, } from '@aws-sdk/client-firehose
11
10
  * Only one active session should exist at any given moment.
12
11
  * When a new session is initiated, the previous ones should be released.
13
12
  * */
14
- var eventBufferMap = {};
15
- var cachedClients = {};
16
- var createPutRecordsBatchCommand = function (streamName, events) {
17
- return new PutRecordBatchCommand({
18
- DeliveryStreamName: streamName,
19
- Records: events.map(function (event) { return ({
20
- Data: event.event,
21
- }); }),
22
- });
13
+ const eventBufferMap = {};
14
+ const cachedClients = {};
15
+ const createPutRecordsBatchCommand = (streamName, events) => new PutRecordBatchCommand({
16
+ DeliveryStreamName: streamName,
17
+ Records: events.map(event => ({
18
+ Data: event.event,
19
+ })),
20
+ });
21
+ const submitEvents = async (events, client, resendLimit) => {
22
+ const groupedByStreamName = Object.entries(groupBy(event => event.streamName, events));
23
+ const requests = groupedByStreamName
24
+ .map(([streamName, events]) => createPutRecordsBatchCommand(streamName, events))
25
+ .map(command => client.send(command));
26
+ const responses = await Promise.allSettled(requests);
27
+ const failedEvents = responses
28
+ .map((response, i) => response.status === 'rejected' ? groupedByStreamName[i][1] : [])
29
+ .flat();
30
+ return resendLimit
31
+ ? failedEvents
32
+ .filter(event => event.retryCount < resendLimit)
33
+ .map(event => ({ ...event, retryCount: event.retryCount + 1 }))
34
+ .sort((a, b) => a.timestamp - b.timestamp)
35
+ : [];
23
36
  };
24
- var submitEvents = function (events, client, resendLimit) { return __awaiter(void 0, void 0, void 0, function () {
25
- var groupedByStreamName, requests, responses, failedEvents;
26
- return __generator(this, function (_a) {
27
- switch (_a.label) {
28
- case 0:
29
- groupedByStreamName = Object.entries(groupBy(function (event) { return event.streamName; }, events));
30
- requests = groupedByStreamName
31
- .map(function (_a) {
32
- var _b = __read(_a, 2), streamName = _b[0], events = _b[1];
33
- return createPutRecordsBatchCommand(streamName, events);
34
- })
35
- .map(function (command) { return client.send(command); });
36
- return [4 /*yield*/, Promise.allSettled(requests)];
37
- case 1:
38
- responses = _a.sent();
39
- failedEvents = responses
40
- .map(function (response, i) {
41
- return response.status === 'rejected' ? groupedByStreamName[i][1] : [];
42
- })
43
- .flat();
44
- return [2 /*return*/, resendLimit
45
- ? failedEvents
46
- .filter(function (event) { return event.retryCount < resendLimit; })
47
- .map(function (event) { return (__assign(__assign({}, event), { retryCount: event.retryCount + 1 })); })
48
- .sort(function (a, b) { return a.timestamp - b.timestamp; })
49
- : []];
50
- }
51
- });
52
- }); };
53
- export var getEventBuffer = function (_a) {
54
- var e_1, _b;
55
- var region = _a.region, credentials = _a.credentials, identityId = _a.identityId, bufferSize = _a.bufferSize, flushSize = _a.flushSize, flushInterval = _a.flushInterval, resendLimit = _a.resendLimit, userAgentValue = _a.userAgentValue;
56
- var sessionToken = credentials.sessionToken;
57
- var sessionIdentityKey = [region, sessionToken, identityId]
58
- .filter(function (id) { return !!id; })
59
- .join('-');
37
+ export const getEventBuffer = ({ region, credentials, identityId, bufferSize, flushSize, flushInterval, resendLimit, userAgentValue, }) => {
38
+ const sessionIdentityKey = [region, identityId].filter(id => !!id).join('-');
60
39
  if (!eventBufferMap[sessionIdentityKey]) {
61
- var getClient = function () {
40
+ const getClient = () => {
62
41
  if (!cachedClients[sessionIdentityKey]) {
63
42
  cachedClients[sessionIdentityKey] = new FirehoseClient({
64
- region: region,
65
- credentials: credentials,
43
+ region,
44
+ credentials,
66
45
  customUserAgent: userAgentValue,
67
46
  });
68
47
  }
69
- var firehoseClient = cachedClients[sessionIdentityKey];
70
- return function (events) { return submitEvents(events, firehoseClient, resendLimit); };
48
+ const firehoseClient = cachedClients[sessionIdentityKey];
49
+ return events => submitEvents(events, firehoseClient, resendLimit);
71
50
  };
72
51
  eventBufferMap[sessionIdentityKey] =
73
52
  new EventBuffer({
74
- bufferSize: bufferSize,
75
- flushSize: flushSize,
76
- flushInterval: flushInterval,
53
+ bufferSize,
54
+ flushSize,
55
+ flushInterval,
77
56
  }, getClient);
78
- var releaseSessionKeys = Object.keys(eventBufferMap).filter(function (key) { return key !== sessionIdentityKey; });
79
- try {
80
- for (var releaseSessionKeys_1 = __values(releaseSessionKeys), releaseSessionKeys_1_1 = releaseSessionKeys_1.next(); !releaseSessionKeys_1_1.done; releaseSessionKeys_1_1 = releaseSessionKeys_1.next()) {
81
- var releaseSessionKey = releaseSessionKeys_1_1.value;
57
+ const releaseSessionKeys = Object.keys(eventBufferMap).filter(key => key !== sessionIdentityKey);
58
+ for (const releaseSessionKey of releaseSessionKeys) {
59
+ eventBufferMap[releaseSessionKey].flushAll().finally(() => {
82
60
  eventBufferMap[releaseSessionKey].release();
83
61
  delete eventBufferMap[releaseSessionKey];
84
62
  delete cachedClients[releaseSessionKey];
85
- }
86
- }
87
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
88
- finally {
89
- try {
90
- if (releaseSessionKeys_1_1 && !releaseSessionKeys_1_1.done && (_b = releaseSessionKeys_1.return)) _b.call(releaseSessionKeys_1);
91
- }
92
- finally { if (e_1) throw e_1.error; }
63
+ });
93
64
  }
94
65
  }
95
66
  return eventBufferMap[sessionIdentityKey];
@@ -1,20 +1,21 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { __assign } from "tslib";
4
3
  import { Amplify } from '@aws-amplify/core';
5
4
  import { AnalyticsValidationErrorCode, assertValidationError, } from '../../../errors';
6
5
  import { DEFAULT_KINESIS_FIREHOSE_CONFIG } from './constants';
7
- export var resolveConfig = function () {
8
- var _a;
9
- var config = (_a = Amplify.getConfig().Analytics) === null || _a === void 0 ? void 0 : _a.KinesisFirehose;
10
- var _b = __assign(__assign({}, DEFAULT_KINESIS_FIREHOSE_CONFIG), config), region = _b.region, _c = _b.bufferSize, bufferSize = _c === void 0 ? DEFAULT_KINESIS_FIREHOSE_CONFIG.bufferSize : _c, _d = _b.flushSize, flushSize = _d === void 0 ? DEFAULT_KINESIS_FIREHOSE_CONFIG.flushSize : _d, _e = _b.flushInterval, flushInterval = _e === void 0 ? DEFAULT_KINESIS_FIREHOSE_CONFIG.flushInterval : _e, resendLimit = _b.resendLimit;
6
+ export const resolveConfig = () => {
7
+ const config = Amplify.getConfig().Analytics?.KinesisFirehose;
8
+ const { region, bufferSize = DEFAULT_KINESIS_FIREHOSE_CONFIG.bufferSize, flushSize = DEFAULT_KINESIS_FIREHOSE_CONFIG.flushSize, flushInterval = DEFAULT_KINESIS_FIREHOSE_CONFIG.flushInterval, resendLimit, } = {
9
+ ...DEFAULT_KINESIS_FIREHOSE_CONFIG,
10
+ ...config,
11
+ };
11
12
  assertValidationError(!!region, AnalyticsValidationErrorCode.NoRegion);
12
13
  assertValidationError(flushSize < bufferSize, AnalyticsValidationErrorCode.InvalidFlushSize);
13
14
  return {
14
- region: region,
15
- bufferSize: bufferSize,
16
- flushSize: flushSize,
17
- flushInterval: flushInterval,
18
- resendLimit: resendLimit,
15
+ region,
16
+ bufferSize,
17
+ flushSize,
18
+ flushInterval,
19
+ resendLimit,
19
20
  };
20
21
  };
@@ -4,7 +4,7 @@ import { getEventBuffer, resolveConfig } from '../utils';
4
4
  import { getAnalyticsUserAgentString, resolveCredentials, } from '../../../utils';
5
5
  import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
6
6
  import { ConsoleLogger } from '@aws-amplify/core';
7
- var logger = new ConsoleLogger('Personalize');
7
+ const logger = new ConsoleLogger('Personalize');
8
8
  /**
9
9
  * Flushes all buffered Personalize events to the service.
10
10
  *
@@ -12,21 +12,18 @@ var logger = new ConsoleLogger('Personalize');
12
12
  * This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
13
13
  * this API may not be included in the flush.
14
14
  */
15
- export var flushEvents = function () {
16
- var _a = resolveConfig(), region = _a.region, flushSize = _a.flushSize, bufferSize = _a.bufferSize, flushInterval = _a.flushInterval;
15
+ export const flushEvents = () => {
16
+ const { region, flushSize, bufferSize, flushInterval } = resolveConfig();
17
17
  resolveCredentials()
18
- .then(function (_a) {
19
- var credentials = _a.credentials, identityId = _a.identityId;
20
- return getEventBuffer({
21
- region: region,
22
- flushSize: flushSize,
23
- flushInterval: flushInterval,
24
- bufferSize: bufferSize,
25
- credentials: credentials,
26
- identityId: identityId,
27
- userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
28
- });
29
- })
30
- .then(function (eventBuffer) { return eventBuffer.flushAll(); })
31
- .catch(function (e) { return logger.warn('Failed to flush events', e); });
18
+ .then(({ credentials, identityId }) => getEventBuffer({
19
+ region,
20
+ flushSize,
21
+ flushInterval,
22
+ bufferSize,
23
+ credentials,
24
+ identityId,
25
+ userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
26
+ }))
27
+ .then(eventBuffer => eventBuffer.flushAll())
28
+ .catch(e => logger.warn('Failed to flush events', e));
32
29
  };
@@ -1,83 +1,67 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { __awaiter, __generator } from "tslib";
4
3
  import { autoTrackMedia, getEventBuffer, resolveCachedSession, resolveConfig, updateCachedSession, } from '../utils';
5
4
  import { getAnalyticsUserAgentString, isAnalyticsEnabled, resolveCredentials, } from '../../../utils';
6
5
  import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
7
6
  import { ConsoleLogger } from '@aws-amplify/core';
8
7
  import { IDENTIFY_EVENT_TYPE, MEDIA_AUTO_TRACK_EVENT_TYPE, } from '../utils/constants';
9
- var logger = new ConsoleLogger('Personalize');
10
- export var record = function (_a) {
11
- var userId = _a.userId, eventId = _a.eventId, eventType = _a.eventType, properties = _a.properties;
8
+ const logger = new ConsoleLogger('Personalize');
9
+ export const record = ({ userId, eventId, eventType, properties, }) => {
12
10
  if (!isAnalyticsEnabled()) {
13
11
  logger.debug('Analytics is disabled, event will not be recorded.');
14
12
  return;
15
13
  }
16
- var _b = resolveConfig(), region = _b.region, trackingId = _b.trackingId, bufferSize = _b.bufferSize, flushSize = _b.flushSize, flushInterval = _b.flushInterval;
14
+ const { region, trackingId, bufferSize, flushSize, flushInterval } = resolveConfig();
17
15
  resolveCredentials()
18
- .then(function (_a) {
19
- var credentials = _a.credentials, identityId = _a.identityId;
20
- return __awaiter(void 0, void 0, void 0, function () {
21
- var timestamp, _b, cachedSessionId, cachedUserId, _c, updatedSessionId, updatedUserId, eventBuffer;
22
- return __generator(this, function (_d) {
23
- switch (_d.label) {
24
- case 0:
25
- timestamp = Date.now();
26
- return [4 /*yield*/, resolveCachedSession()];
27
- case 1:
28
- _b = _d.sent(), cachedSessionId = _b.sessionId, cachedUserId = _b.userId;
29
- if (eventType === IDENTIFY_EVENT_TYPE) {
30
- updateCachedSession(typeof properties.userId === 'string' ? properties.userId : '', cachedSessionId, cachedUserId);
31
- }
32
- else if (!!userId) {
33
- updateCachedSession(userId, cachedSessionId, cachedUserId);
34
- }
35
- return [4 /*yield*/, resolveCachedSession()];
36
- case 2:
37
- _c = _d.sent(), updatedSessionId = _c.sessionId, updatedUserId = _c.userId;
38
- eventBuffer = getEventBuffer({
39
- region: region,
40
- flushSize: flushSize,
41
- flushInterval: flushInterval,
42
- bufferSize: bufferSize,
43
- credentials: credentials,
44
- identityId: identityId,
45
- userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
46
- });
47
- if (eventType === MEDIA_AUTO_TRACK_EVENT_TYPE) {
48
- autoTrackMedia({
49
- trackingId: trackingId,
50
- sessionId: updatedSessionId,
51
- userId: updatedUserId,
52
- event: {
53
- eventId: eventId,
54
- eventType: eventType,
55
- properties: properties,
56
- },
57
- }, eventBuffer);
58
- }
59
- else {
60
- eventBuffer.append({
61
- trackingId: trackingId,
62
- sessionId: updatedSessionId,
63
- userId: updatedUserId,
64
- event: {
65
- eventId: eventId,
66
- eventType: eventType,
67
- properties: properties,
68
- },
69
- timestamp: timestamp,
70
- });
71
- }
72
- if (eventBuffer.length >= bufferSize) {
73
- eventBuffer.flushAll();
74
- }
75
- return [2 /*return*/];
76
- }
77
- });
16
+ .then(async ({ credentials, identityId }) => {
17
+ const timestamp = Date.now();
18
+ const { sessionId: cachedSessionId, userId: cachedUserId } = await resolveCachedSession();
19
+ if (eventType === IDENTIFY_EVENT_TYPE) {
20
+ updateCachedSession(typeof properties.userId === 'string' ? properties.userId : '', cachedSessionId, cachedUserId);
21
+ }
22
+ else if (!!userId) {
23
+ updateCachedSession(userId, cachedSessionId, cachedUserId);
24
+ }
25
+ const { sessionId: updatedSessionId, userId: updatedUserId } = await resolveCachedSession();
26
+ const eventBuffer = getEventBuffer({
27
+ region,
28
+ flushSize,
29
+ flushInterval,
30
+ bufferSize,
31
+ credentials,
32
+ identityId,
33
+ userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
78
34
  });
35
+ if (eventType === MEDIA_AUTO_TRACK_EVENT_TYPE) {
36
+ autoTrackMedia({
37
+ trackingId,
38
+ sessionId: updatedSessionId,
39
+ userId: updatedUserId,
40
+ event: {
41
+ eventId,
42
+ eventType,
43
+ properties,
44
+ },
45
+ }, eventBuffer);
46
+ }
47
+ else {
48
+ eventBuffer.append({
49
+ trackingId,
50
+ sessionId: updatedSessionId,
51
+ userId: updatedUserId,
52
+ event: {
53
+ eventId,
54
+ eventType,
55
+ properties,
56
+ },
57
+ timestamp,
58
+ });
59
+ }
60
+ if (eventBuffer.length >= bufferSize) {
61
+ eventBuffer.flushAll();
62
+ }
79
63
  })
80
- .catch(function (e) {
64
+ .catch(e => {
81
65
  logger.warn('Failed to record event.', e);
82
66
  });
83
67
  };