@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.
- package/lib/apis/disable.js +1 -1
- package/lib/apis/enable.js +1 -1
- package/lib/errors/AnalyticsError.js +7 -11
- package/lib/errors/assertValidationError.js +6 -6
- package/lib/errors/validation.js +10 -11
- package/lib/providers/kinesis/apis/flushEvents.js +20 -23
- package/lib/providers/kinesis/apis/record.js +24 -26
- package/lib/providers/kinesis/utils/getEventBuffer.js +39 -70
- package/lib/providers/kinesis/utils/resolveConfig.js +14 -13
- package/lib/providers/kinesis-firehose/apis/flushEvents.js +19 -22
- package/lib/providers/kinesis-firehose/apis/record.js +22 -24
- package/lib/providers/kinesis-firehose/utils/getEventBuffer.js +39 -68
- package/lib/providers/kinesis-firehose/utils/resolveConfig.js +14 -13
- package/lib/providers/personalize/apis/flushEvents.js +18 -21
- package/lib/providers/personalize/apis/record.js +55 -71
- package/lib/providers/personalize/utils/autoTrackMedia.js +96 -98
- package/lib/providers/personalize/utils/cachedSession.js +30 -42
- package/lib/providers/personalize/utils/getEventBuffer.js +35 -62
- package/lib/providers/personalize/utils/resolveConfig.js +14 -13
- package/lib/providers/pinpoint/apis/configureAutoTrack.js +6 -6
- package/lib/providers/pinpoint/apis/flushEvents.js +10 -13
- package/lib/providers/pinpoint/apis/identifyUser.js +18 -30
- package/lib/providers/pinpoint/apis/record.js +15 -16
- package/lib/providers/pinpoint/utils/resolveConfig.js +5 -6
- package/lib/providers/pinpoint/utils/resolveCredentials.js +7 -15
- package/lib/setupTests.js +1 -1
- package/lib/trackers/EventTracker.js +39 -45
- package/lib/trackers/PageViewTracker.js +41 -45
- package/lib/trackers/SessionTracker.js +25 -31
- package/lib/trackers/SessionTracker.native.js +25 -31
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/utils/eventBuffer/EventBuffer.js +34 -62
- package/lib/utils/groupBy.js +4 -7
- package/lib/utils/resolveCredentials.js +7 -15
- package/lib/utils/statusHelpers.js +4 -4
- package/lib/utils/trackerConfigHelpers.js +2 -2
- package/lib/utils/trackerConfigHelpers.native.js +2 -2
- package/lib/utils/trackerHelpers.js +5 -5
- package/lib/utils/userAgent.js +3 -3
- package/lib-esm/apis/disable.js +1 -1
- package/lib-esm/apis/enable.js +1 -1
- package/lib-esm/errors/AnalyticsError.js +6 -11
- package/lib-esm/errors/assertValidationError.js +4 -4
- package/lib-esm/errors/validation.js +10 -11
- package/lib-esm/providers/kinesis/apis/flushEvents.js +15 -18
- package/lib-esm/providers/kinesis/apis/record.js +18 -20
- package/lib-esm/providers/kinesis/utils/constants.js +1 -1
- package/lib-esm/providers/kinesis/utils/getEventBuffer.js +37 -68
- package/lib-esm/providers/kinesis/utils/resolveConfig.js +11 -10
- package/lib-esm/providers/kinesis-firehose/apis/flushEvents.js +15 -18
- package/lib-esm/providers/kinesis-firehose/apis/record.js +17 -19
- package/lib-esm/providers/kinesis-firehose/utils/constants.js +1 -1
- package/lib-esm/providers/kinesis-firehose/utils/getEventBuffer.js +37 -66
- package/lib-esm/providers/kinesis-firehose/utils/resolveConfig.js +11 -10
- package/lib-esm/providers/personalize/apis/flushEvents.js +14 -17
- package/lib-esm/providers/personalize/apis/record.js +50 -66
- package/lib-esm/providers/personalize/utils/autoTrackMedia.js +94 -96
- package/lib-esm/providers/personalize/utils/cachedSession.js +28 -40
- package/lib-esm/providers/personalize/utils/constants.js +4 -4
- package/lib-esm/providers/personalize/utils/getEventBuffer.js +33 -60
- package/lib-esm/providers/personalize/utils/resolveConfig.js +11 -10
- package/lib-esm/providers/pinpoint/apis/configureAutoTrack.js +4 -4
- package/lib-esm/providers/pinpoint/apis/flushEvents.js +5 -8
- package/lib-esm/providers/pinpoint/apis/identifyUser.js +14 -26
- package/lib-esm/providers/pinpoint/apis/record.js +9 -10
- package/lib-esm/providers/pinpoint/utils/resolveConfig.js +3 -4
- package/lib-esm/providers/pinpoint/utils/resolveCredentials.js +5 -13
- package/lib-esm/setupTests.js +1 -1
- package/lib-esm/trackers/EventTracker.js +37 -44
- package/lib-esm/trackers/PageViewTracker.js +39 -44
- package/lib-esm/trackers/SessionTracker.js +22 -29
- package/lib-esm/trackers/SessionTracker.native.js +22 -29
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/utils/eventBuffer/EventBuffer.js +33 -62
- package/lib-esm/utils/groupBy.js +4 -7
- package/lib-esm/utils/resolveCredentials.js +5 -13
- package/lib-esm/utils/statusHelpers.js +4 -4
- package/lib-esm/utils/trackerConfigHelpers.js +1 -1
- package/lib-esm/utils/trackerConfigHelpers.native.js +1 -1
- package/lib-esm/utils/trackerHelpers.js +4 -4
- package/lib-esm/utils/userAgent.js +2 -2
- package/package.json +6 -9
- package/src/providers/kinesis/utils/getEventBuffer.ts +6 -7
- package/src/providers/kinesis-firehose/utils/getEventBuffer.ts +6 -7
- package/src/providers/personalize/utils/getEventBuffer.ts +6 -7
- package/src/trackers/SessionTracker.native.ts +2 -3
- package/src/trackers/SessionTracker.ts +2 -3
|
@@ -3,89 +3,61 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.EventBuffer = void 0;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
function EventBuffer(config, getAnalyticsClient) {
|
|
6
|
+
const core_1 = require("@aws-amplify/core");
|
|
7
|
+
const logger = new core_1.ConsoleLogger('EventBuffer');
|
|
8
|
+
class EventBuffer {
|
|
9
|
+
constructor(config, getAnalyticsClient) {
|
|
11
10
|
this.list = [];
|
|
12
11
|
this.config = config;
|
|
13
12
|
this.getAnalyticsClient = getAnalyticsClient;
|
|
14
13
|
this.startEventLoop();
|
|
15
14
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
try {
|
|
23
|
-
for (var events_1 = tslib_1.__values(events), events_1_1 = events_1.next(); !events_1_1.done; events_1_1 = events_1.next()) {
|
|
24
|
-
var event_1 = events_1_1.value;
|
|
25
|
-
if (this.list.length + 1 > this.config.bufferSize) {
|
|
26
|
-
logger.debug("Exceed ".concat(typeof event_1, " event buffer limits, event dropped"));
|
|
27
|
-
continue;
|
|
28
|
-
}
|
|
29
|
-
this.list.push(event_1);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
33
|
-
finally {
|
|
34
|
-
try {
|
|
35
|
-
if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
|
|
15
|
+
append(...events) {
|
|
16
|
+
for (const event of events) {
|
|
17
|
+
if (this.list.length + 1 > this.config.bufferSize) {
|
|
18
|
+
logger.debug(`Exceed ${typeof event} event buffer limits, event dropped`);
|
|
19
|
+
continue;
|
|
36
20
|
}
|
|
37
|
-
|
|
21
|
+
this.list.push(event);
|
|
38
22
|
}
|
|
39
|
-
}
|
|
40
|
-
|
|
23
|
+
}
|
|
24
|
+
flushAll() {
|
|
41
25
|
return this.submitEvents(this.list.length);
|
|
42
|
-
}
|
|
43
|
-
|
|
26
|
+
}
|
|
27
|
+
release() {
|
|
44
28
|
this.list = [];
|
|
45
29
|
if (this.timer) {
|
|
46
30
|
clearInterval(this.timer);
|
|
47
31
|
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
enumerable: false,
|
|
54
|
-
configurable: true
|
|
55
|
-
});
|
|
56
|
-
EventBuffer.prototype.head = function (count) {
|
|
32
|
+
}
|
|
33
|
+
get length() {
|
|
34
|
+
return this.list.length;
|
|
35
|
+
}
|
|
36
|
+
head(count) {
|
|
57
37
|
return this.list.splice(0, count);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
data[_i] = arguments[_i];
|
|
64
|
-
}
|
|
65
|
-
(_a = this.list).unshift.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(data), false));
|
|
66
|
-
};
|
|
67
|
-
EventBuffer.prototype.startEventLoop = function () {
|
|
68
|
-
var _this = this;
|
|
38
|
+
}
|
|
39
|
+
insertAtBeginning(...data) {
|
|
40
|
+
this.list.unshift(...data);
|
|
41
|
+
}
|
|
42
|
+
startEventLoop() {
|
|
69
43
|
if (this.timer) {
|
|
70
44
|
clearInterval(this.timer);
|
|
71
45
|
}
|
|
72
|
-
|
|
73
|
-
setInterval(
|
|
74
|
-
|
|
46
|
+
const { flushSize, flushInterval } = this.config;
|
|
47
|
+
setInterval(() => {
|
|
48
|
+
this.submitEvents(flushSize);
|
|
75
49
|
}, flushInterval);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
var events = this.head(count);
|
|
50
|
+
}
|
|
51
|
+
submitEvents(count) {
|
|
52
|
+
const events = this.head(count);
|
|
80
53
|
if (events.length === 0) {
|
|
81
54
|
return Promise.resolve();
|
|
82
55
|
}
|
|
83
|
-
return this.getAnalyticsClient()(events).then(
|
|
56
|
+
return this.getAnalyticsClient()(events).then(result => {
|
|
84
57
|
if (result.length > 0) {
|
|
85
|
-
|
|
58
|
+
this.insertAtBeginning(...result);
|
|
86
59
|
}
|
|
87
60
|
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}());
|
|
61
|
+
}
|
|
62
|
+
}
|
|
91
63
|
exports.EventBuffer = EventBuffer;
|
package/lib/utils/groupBy.js
CHANGED
|
@@ -3,13 +3,10 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.groupBy = void 0;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var _b;
|
|
11
|
-
var groupId = getGroupId(current);
|
|
12
|
-
return tslib_1.__assign(tslib_1.__assign({}, result), (_a = {}, _a[groupId] = tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(((_b = result[groupId]) !== null && _b !== void 0 ? _b : [])), false), [current], false), _a));
|
|
6
|
+
const groupBy = (getGroupId, list) => {
|
|
7
|
+
return list.reduce((result, current) => {
|
|
8
|
+
const groupId = getGroupId(current);
|
|
9
|
+
return { ...result, [groupId]: [...(result[groupId] ?? []), current] };
|
|
13
10
|
}, {});
|
|
14
11
|
};
|
|
15
12
|
exports.groupBy = groupBy;
|
|
@@ -3,19 +3,11 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.resolveCredentials = void 0;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
|
|
13
|
-
case 0: return [4 /*yield*/, (0, core_1.fetchAuthSession)()];
|
|
14
|
-
case 1:
|
|
15
|
-
_a = _b.sent(), credentials = _a.credentials, identityId = _a.identityId;
|
|
16
|
-
(0, errors_1.assertValidationError)(!!credentials, errors_1.AnalyticsValidationErrorCode.NoCredentials);
|
|
17
|
-
return [2 /*return*/, { credentials: credentials, identityId: identityId }];
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
}); };
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
|
+
const core_1 = require("@aws-amplify/core");
|
|
8
|
+
const resolveCredentials = async () => {
|
|
9
|
+
const { credentials, identityId } = await (0, core_1.fetchAuthSession)();
|
|
10
|
+
(0, errors_1.assertValidationError)(!!credentials, errors_1.AnalyticsValidationErrorCode.NoCredentials);
|
|
11
|
+
return { credentials, identityId };
|
|
12
|
+
};
|
|
21
13
|
exports.resolveCredentials = resolveCredentials;
|
|
@@ -3,17 +3,17 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.isAnalyticsEnabled = exports.disableAnalytics = exports.enableAnalytics = void 0;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
let analyticsEnabled = true;
|
|
7
|
+
const enableAnalytics = () => {
|
|
8
8
|
analyticsEnabled = true;
|
|
9
9
|
};
|
|
10
10
|
exports.enableAnalytics = enableAnalytics;
|
|
11
|
-
|
|
11
|
+
const disableAnalytics = () => {
|
|
12
12
|
analyticsEnabled = false;
|
|
13
13
|
};
|
|
14
14
|
exports.disableAnalytics = disableAnalytics;
|
|
15
15
|
/**
|
|
16
16
|
* Returns the current status of the Analytics category.
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
const isAnalyticsEnabled = () => analyticsEnabled;
|
|
19
19
|
exports.isAnalyticsEnabled = isAnalyticsEnabled;
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.validateTrackerConfiguration = void 0;
|
|
6
|
-
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
7
|
/**
|
|
8
8
|
* Validates tracker configuration.
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
const validateTrackerConfiguration = (input) => {
|
|
11
11
|
(0, errors_1.assertValidationError)(input.type === 'event' ||
|
|
12
12
|
input.type === 'pageView' ||
|
|
13
13
|
input.type === 'session', errors_1.AnalyticsValidationErrorCode.InvalidTracker);
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.validateTrackerConfiguration = void 0;
|
|
6
|
-
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
7
|
/**
|
|
8
8
|
* Validates tracker configuration.
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
const validateTrackerConfiguration = (input) => {
|
|
11
11
|
// React Native only supports session tracking
|
|
12
12
|
(0, errors_1.assertValidationError)(input.type === 'session', errors_1.AnalyticsValidationErrorCode.UnsupportedPlatform);
|
|
13
13
|
};
|
|
@@ -3,17 +3,17 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.updateProviderTrackers = void 0;
|
|
6
|
-
|
|
6
|
+
const trackers_1 = require("../trackers");
|
|
7
7
|
/**
|
|
8
8
|
* Updates a provider's trackers as appropriate for the provided auto-track configuration.
|
|
9
9
|
*
|
|
10
10
|
* @remark
|
|
11
11
|
* This utility will mutate the provider's configured trackers via `providerTrackers`.
|
|
12
12
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const updateProviderTrackers = (input, providerEventRecorder, providerTrackers) => {
|
|
14
|
+
let trackerInstance;
|
|
15
|
+
const trackerType = input.type;
|
|
16
|
+
const currentTracker = providerTrackers[trackerType];
|
|
17
17
|
// Check if the tracker was disabled & should be cleaned up
|
|
18
18
|
if (!input.enable) {
|
|
19
19
|
if (currentTracker) {
|
package/lib/utils/userAgent.js
CHANGED
|
@@ -3,18 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getAnalyticsUserAgentString = exports.getAnalyticsUserAgent = void 0;
|
|
4
4
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
5
|
// SPDX-License-Identifier: Apache-2.0
|
|
6
|
-
|
|
6
|
+
const utils_1 = require("@aws-amplify/core/internals/utils");
|
|
7
7
|
function getAnalyticsUserAgent(action) {
|
|
8
8
|
return (0, utils_1.getAmplifyUserAgentObject)({
|
|
9
9
|
category: utils_1.Category.Analytics,
|
|
10
|
-
action
|
|
10
|
+
action,
|
|
11
11
|
});
|
|
12
12
|
}
|
|
13
13
|
exports.getAnalyticsUserAgent = getAnalyticsUserAgent;
|
|
14
14
|
function getAnalyticsUserAgentString(action) {
|
|
15
15
|
return (0, utils_1.getAmplifyUserAgent)({
|
|
16
16
|
category: utils_1.Category.Analytics,
|
|
17
|
-
action
|
|
17
|
+
action,
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
exports.getAnalyticsUserAgentString = getAnalyticsUserAgentString;
|
package/lib-esm/apis/disable.js
CHANGED
|
@@ -8,4 +8,4 @@ import { disableAnalytics } from '../utils';
|
|
|
8
8
|
* When Analytics is disabled events will not be buffered or transmitted to your selected service. Any auto-tracking
|
|
9
9
|
* behavior that you have configured via `configureAutoTrack` will not have any effect while Analytics is disabled.
|
|
10
10
|
*/
|
|
11
|
-
export
|
|
11
|
+
export const disable = disableAnalytics;
|
package/lib-esm/apis/enable.js
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { __extends } from "tslib";
|
|
4
3
|
import { AmplifyError, } from '@aws-amplify/core/internals/utils';
|
|
5
4
|
/**
|
|
6
5
|
* @internal
|
|
7
6
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var _this = _super.call(this, params) || this;
|
|
7
|
+
export class AnalyticsError extends AmplifyError {
|
|
8
|
+
constructor(params) {
|
|
9
|
+
super(params);
|
|
12
10
|
// Hack for making the custom error class work when transpiled to es5
|
|
13
11
|
// TODO: Delete the following 2 lines after we change the build target to >= es2015
|
|
14
|
-
|
|
15
|
-
Object.setPrototypeOf(
|
|
16
|
-
return _this;
|
|
12
|
+
this.constructor = AnalyticsError;
|
|
13
|
+
Object.setPrototypeOf(this, AnalyticsError.prototype);
|
|
17
14
|
}
|
|
18
|
-
|
|
19
|
-
}(AmplifyError));
|
|
20
|
-
export { AnalyticsError };
|
|
15
|
+
}
|
|
@@ -6,12 +6,12 @@ import { validationErrorMap } from './validation';
|
|
|
6
6
|
* @internal
|
|
7
7
|
*/
|
|
8
8
|
export function assertValidationError(assertion, name, message) {
|
|
9
|
-
|
|
9
|
+
const { message: defaultMessage, recoverySuggestion } = validationErrorMap[name];
|
|
10
10
|
if (!assertion) {
|
|
11
11
|
throw new AnalyticsError({
|
|
12
|
-
name
|
|
13
|
-
message: message
|
|
14
|
-
recoverySuggestion
|
|
12
|
+
name,
|
|
13
|
+
message: message ?? defaultMessage,
|
|
14
|
+
recoverySuggestion,
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
17
|
}
|
|
@@ -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
|
-
var _a;
|
|
4
3
|
export var AnalyticsValidationErrorCode;
|
|
5
4
|
(function (AnalyticsValidationErrorCode) {
|
|
6
5
|
AnalyticsValidationErrorCode["NoAppId"] = "NoAppId";
|
|
@@ -12,29 +11,29 @@ export var AnalyticsValidationErrorCode;
|
|
|
12
11
|
AnalyticsValidationErrorCode["NoTrackingId"] = "NoTrackingId";
|
|
13
12
|
AnalyticsValidationErrorCode["InvalidFlushSize"] = "InvalidFlushSize";
|
|
14
13
|
})(AnalyticsValidationErrorCode || (AnalyticsValidationErrorCode = {}));
|
|
15
|
-
export
|
|
16
|
-
|
|
14
|
+
export const validationErrorMap = {
|
|
15
|
+
[AnalyticsValidationErrorCode.NoAppId]: {
|
|
17
16
|
message: 'Missing application id.',
|
|
18
17
|
},
|
|
19
|
-
|
|
18
|
+
[AnalyticsValidationErrorCode.NoCredentials]: {
|
|
20
19
|
message: 'Credentials should not be empty.',
|
|
21
20
|
},
|
|
22
|
-
|
|
21
|
+
[AnalyticsValidationErrorCode.NoEventName]: {
|
|
23
22
|
message: 'Events must specify a name.',
|
|
24
23
|
},
|
|
25
|
-
|
|
24
|
+
[AnalyticsValidationErrorCode.NoRegion]: {
|
|
26
25
|
message: 'Missing region.',
|
|
27
26
|
},
|
|
28
|
-
|
|
27
|
+
[AnalyticsValidationErrorCode.InvalidTracker]: {
|
|
29
28
|
message: 'Invalid tracker type specified.',
|
|
30
29
|
},
|
|
31
|
-
|
|
30
|
+
[AnalyticsValidationErrorCode.UnsupportedPlatform]: {
|
|
32
31
|
message: 'Only session tracking is supported on React Native.',
|
|
33
32
|
},
|
|
34
|
-
|
|
33
|
+
[AnalyticsValidationErrorCode.InvalidFlushSize]: {
|
|
35
34
|
message: 'Invalid FlushSize, it should be smaller than BufferSize',
|
|
36
35
|
},
|
|
37
|
-
|
|
36
|
+
[AnalyticsValidationErrorCode.NoTrackingId]: {
|
|
38
37
|
message: 'A trackingId is required to use Amazon Personalize',
|
|
39
38
|
},
|
|
40
|
-
|
|
39
|
+
};
|
|
@@ -5,7 +5,7 @@ import { getAnalyticsUserAgentString, resolveCredentials, } from '../../../utils
|
|
|
5
5
|
import { getEventBuffer } from '../utils/getEventBuffer';
|
|
6
6
|
import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
|
|
7
7
|
import { ConsoleLogger } from '@aws-amplify/core';
|
|
8
|
-
|
|
8
|
+
const logger = new ConsoleLogger('Kinesis');
|
|
9
9
|
/**
|
|
10
10
|
* Flushes all buffered Kinesis events to the service.
|
|
11
11
|
*
|
|
@@ -13,22 +13,19 @@ var logger = new ConsoleLogger('Kinesis');
|
|
|
13
13
|
* This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
|
|
14
14
|
* this API may not be included in the flush.
|
|
15
15
|
*/
|
|
16
|
-
export
|
|
17
|
-
|
|
16
|
+
export const flushEvents = () => {
|
|
17
|
+
const { region, flushSize, flushInterval, bufferSize, resendLimit } = resolveConfig();
|
|
18
18
|
resolveCredentials()
|
|
19
|
-
.then(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
})
|
|
32
|
-
.then(function (eventBuffer) { return eventBuffer.flushAll(); })
|
|
33
|
-
.catch(function (e) { return logger.warn('Failed to flush events.', e); });
|
|
19
|
+
.then(({ credentials, identityId }) => getEventBuffer({
|
|
20
|
+
region,
|
|
21
|
+
flushSize,
|
|
22
|
+
flushInterval,
|
|
23
|
+
bufferSize,
|
|
24
|
+
credentials,
|
|
25
|
+
identityId,
|
|
26
|
+
resendLimit,
|
|
27
|
+
userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
|
|
28
|
+
}))
|
|
29
|
+
.then(eventBuffer => eventBuffer.flushAll())
|
|
30
|
+
.catch(e => logger.warn('Failed to flush events.', e));
|
|
34
31
|
};
|
|
@@ -6,38 +6,36 @@ import { getAnalyticsUserAgentString, isAnalyticsEnabled, resolveCredentials, }
|
|
|
6
6
|
import { fromUtf8 } from '@smithy/util-utf8';
|
|
7
7
|
import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
|
|
8
8
|
import { ConsoleLogger } from '@aws-amplify/core';
|
|
9
|
-
|
|
10
|
-
export
|
|
11
|
-
var streamName = _a.streamName, partitionKey = _a.partitionKey, data = _a.data;
|
|
9
|
+
const logger = new ConsoleLogger('Kinesis');
|
|
10
|
+
export const record = ({ streamName, partitionKey, data, }) => {
|
|
12
11
|
if (!isAnalyticsEnabled()) {
|
|
13
12
|
logger.debug('Analytics is disabled, event will not be recorded.');
|
|
14
13
|
return;
|
|
15
14
|
}
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const timestamp = Date.now();
|
|
16
|
+
const { region, bufferSize, flushSize, flushInterval, resendLimit } = resolveConfig();
|
|
18
17
|
resolveCredentials()
|
|
19
|
-
.then(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
resendLimit: resendLimit,
|
|
18
|
+
.then(({ credentials, identityId }) => {
|
|
19
|
+
const buffer = getEventBuffer({
|
|
20
|
+
region,
|
|
21
|
+
bufferSize,
|
|
22
|
+
flushSize,
|
|
23
|
+
flushInterval,
|
|
24
|
+
credentials,
|
|
25
|
+
identityId,
|
|
26
|
+
resendLimit,
|
|
29
27
|
userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
|
|
30
28
|
});
|
|
31
29
|
buffer.append({
|
|
32
|
-
region
|
|
33
|
-
streamName
|
|
34
|
-
partitionKey
|
|
30
|
+
region,
|
|
31
|
+
streamName,
|
|
32
|
+
partitionKey,
|
|
35
33
|
event: ArrayBuffer.isView(data) ? data : fromUtf8(JSON.stringify(data)),
|
|
36
|
-
timestamp
|
|
34
|
+
timestamp,
|
|
37
35
|
retryCount: 0,
|
|
38
36
|
});
|
|
39
37
|
})
|
|
40
|
-
.catch(
|
|
38
|
+
.catch(e => {
|
|
41
39
|
// An error occured while fetching credentials or persisting the event to the buffer
|
|
42
40
|
logger.warn('Failed to record event.', e);
|
|
43
41
|
});
|
|
@@ -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 { KinesisClient, PutRecordsCommand } from '@aws-sdk/client-kinesis';
|
|
6
5
|
/**
|
|
@@ -11,88 +10,58 @@ import { KinesisClient, PutRecordsCommand } from '@aws-sdk/client-kinesis';
|
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
const eventBufferMap = {};
|
|
14
|
+
const cachedClients = {};
|
|
15
|
+
const createKinesisPutRecordsCommand = (streamName, events) => new PutRecordsCommand({
|
|
16
|
+
StreamName: streamName,
|
|
17
|
+
Records: events.map(event => ({
|
|
18
|
+
PartitionKey: event.partitionKey,
|
|
19
|
+
Data: event.event,
|
|
20
|
+
})),
|
|
21
|
+
});
|
|
22
|
+
const submitEvents = async (events, client, resendLimit) => {
|
|
23
|
+
const groupedByStreamName = Object.entries(groupBy(event => event.streamName, events));
|
|
24
|
+
const requests = groupedByStreamName
|
|
25
|
+
.map(([streamName, events]) => createKinesisPutRecordsCommand(streamName, events))
|
|
26
|
+
.map(command => client.send(command));
|
|
27
|
+
const responses = await Promise.allSettled(requests);
|
|
28
|
+
const failedEvents = responses
|
|
29
|
+
.map((response, i) => response.status === 'rejected' ? groupedByStreamName[i][1] : [])
|
|
30
|
+
.flat();
|
|
31
|
+
return resendLimit
|
|
32
|
+
? failedEvents
|
|
33
|
+
.filter(event => event.retryCount < resendLimit)
|
|
34
|
+
.map(event => ({ ...event, retryCount: event.retryCount + 1 }))
|
|
35
|
+
.sort((a, b) => a.timestamp - b.timestamp)
|
|
36
|
+
: [];
|
|
24
37
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return __generator(this, function (_a) {
|
|
28
|
-
switch (_a.label) {
|
|
29
|
-
case 0:
|
|
30
|
-
groupedByStreamName = Object.entries(groupBy(function (event) { return event.streamName; }, events));
|
|
31
|
-
requests = groupedByStreamName
|
|
32
|
-
.map(function (_a) {
|
|
33
|
-
var _b = __read(_a, 2), streamName = _b[0], events = _b[1];
|
|
34
|
-
return createKinesisPutRecordsCommand(streamName, events);
|
|
35
|
-
})
|
|
36
|
-
.map(function (command) { return client.send(command); });
|
|
37
|
-
return [4 /*yield*/, Promise.allSettled(requests)];
|
|
38
|
-
case 1:
|
|
39
|
-
responses = _a.sent();
|
|
40
|
-
failedEvents = responses
|
|
41
|
-
.map(function (response, i) {
|
|
42
|
-
return response.status === 'rejected' ? groupedByStreamName[i][1] : [];
|
|
43
|
-
})
|
|
44
|
-
.flat();
|
|
45
|
-
return [2 /*return*/, resendLimit
|
|
46
|
-
? failedEvents
|
|
47
|
-
.filter(function (event) { return event.retryCount < resendLimit; })
|
|
48
|
-
.map(function (event) { return (__assign(__assign({}, event), { retryCount: event.retryCount + 1 })); })
|
|
49
|
-
.sort(function (a, b) { return a.timestamp - b.timestamp; })
|
|
50
|
-
: []];
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
}); };
|
|
54
|
-
export var getEventBuffer = function (_a) {
|
|
55
|
-
var e_1, _b;
|
|
56
|
-
var region = _a.region, flushInterval = _a.flushInterval, flushSize = _a.flushSize, bufferSize = _a.bufferSize, credentials = _a.credentials, identityId = _a.identityId, resendLimit = _a.resendLimit, userAgentValue = _a.userAgentValue;
|
|
57
|
-
var sessionToken = credentials.sessionToken;
|
|
58
|
-
var sessionIdentityKey = [region, sessionToken, identityId]
|
|
59
|
-
.filter(function (x) { return !!x; })
|
|
60
|
-
.join('-');
|
|
38
|
+
export const getEventBuffer = ({ region, flushInterval, flushSize, bufferSize, credentials, identityId, resendLimit, userAgentValue, }) => {
|
|
39
|
+
const sessionIdentityKey = [region, identityId].filter(x => !!x).join('-');
|
|
61
40
|
if (!eventBufferMap[sessionIdentityKey]) {
|
|
62
|
-
|
|
41
|
+
const getKinesisClient = () => {
|
|
63
42
|
if (!cachedClients[sessionIdentityKey]) {
|
|
64
43
|
cachedClients[sessionIdentityKey] = new KinesisClient({
|
|
65
|
-
credentials
|
|
66
|
-
region
|
|
44
|
+
credentials,
|
|
45
|
+
region,
|
|
67
46
|
customUserAgent: userAgentValue,
|
|
68
47
|
});
|
|
69
48
|
}
|
|
70
|
-
return
|
|
71
|
-
return submitEvents(events, cachedClients[sessionIdentityKey], resendLimit);
|
|
72
|
-
};
|
|
49
|
+
return events => submitEvents(events, cachedClients[sessionIdentityKey], resendLimit);
|
|
73
50
|
};
|
|
74
51
|
// create new session
|
|
75
52
|
eventBufferMap[sessionIdentityKey] = new EventBuffer({
|
|
76
|
-
flushInterval
|
|
77
|
-
flushSize
|
|
78
|
-
bufferSize
|
|
53
|
+
flushInterval,
|
|
54
|
+
flushSize,
|
|
55
|
+
bufferSize,
|
|
79
56
|
}, getKinesisClient);
|
|
80
57
|
// release other sessions
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
var releaseSessionKey = releaseSessionKeys_1_1.value;
|
|
58
|
+
const releaseSessionKeys = Object.keys(eventBufferMap).filter(x => x !== sessionIdentityKey);
|
|
59
|
+
for (const releaseSessionKey of releaseSessionKeys) {
|
|
60
|
+
eventBufferMap[releaseSessionKey].flushAll().finally(() => {
|
|
85
61
|
eventBufferMap[releaseSessionKey].release();
|
|
86
62
|
delete eventBufferMap[releaseSessionKey];
|
|
87
63
|
delete cachedClients[releaseSessionKey];
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
91
|
-
finally {
|
|
92
|
-
try {
|
|
93
|
-
if (releaseSessionKeys_1_1 && !releaseSessionKeys_1_1.done && (_b = releaseSessionKeys_1.return)) _b.call(releaseSessionKeys_1);
|
|
94
|
-
}
|
|
95
|
-
finally { if (e_1) throw e_1.error; }
|
|
64
|
+
});
|
|
96
65
|
}
|
|
97
66
|
}
|
|
98
67
|
return eventBufferMap[sessionIdentityKey];
|