@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
|
@@ -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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
15
|
-
bufferSize
|
|
16
|
-
flushSize
|
|
17
|
-
flushInterval
|
|
18
|
-
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
|
-
|
|
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
|
|
16
|
-
|
|
15
|
+
export const flushEvents = () => {
|
|
16
|
+
const { region, flushSize, flushInterval, bufferSize, resendLimit } = resolveConfig();
|
|
17
17
|
resolveCredentials()
|
|
18
|
-
.then(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
9
|
-
export
|
|
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
|
-
|
|
16
|
-
|
|
14
|
+
const timestamp = Date.now();
|
|
15
|
+
const { region, bufferSize, flushSize, flushInterval, resendLimit } = resolveConfig();
|
|
17
16
|
resolveCredentials()
|
|
18
|
-
.then(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|
32
|
-
streamName
|
|
29
|
+
region,
|
|
30
|
+
streamName,
|
|
33
31
|
event: ArrayBuffer.isView(data) ? data : fromUtf8(JSON.stringify(data)),
|
|
34
|
-
timestamp
|
|
32
|
+
timestamp,
|
|
35
33
|
retryCount: 0,
|
|
36
34
|
});
|
|
37
35
|
})
|
|
38
|
-
.catch(
|
|
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
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
25
|
-
|
|
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
|
-
|
|
40
|
+
const getClient = () => {
|
|
62
41
|
if (!cachedClients[sessionIdentityKey]) {
|
|
63
42
|
cachedClients[sessionIdentityKey] = new FirehoseClient({
|
|
64
|
-
region
|
|
65
|
-
credentials
|
|
43
|
+
region,
|
|
44
|
+
credentials,
|
|
66
45
|
customUserAgent: userAgentValue,
|
|
67
46
|
});
|
|
68
47
|
}
|
|
69
|
-
|
|
70
|
-
return
|
|
48
|
+
const firehoseClient = cachedClients[sessionIdentityKey];
|
|
49
|
+
return events => submitEvents(events, firehoseClient, resendLimit);
|
|
71
50
|
};
|
|
72
51
|
eventBufferMap[sessionIdentityKey] =
|
|
73
52
|
new EventBuffer({
|
|
74
|
-
bufferSize
|
|
75
|
-
flushSize
|
|
76
|
-
flushInterval
|
|
53
|
+
bufferSize,
|
|
54
|
+
flushSize,
|
|
55
|
+
flushInterval,
|
|
77
56
|
}, getClient);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
15
|
-
bufferSize
|
|
16
|
-
flushSize
|
|
17
|
-
flushInterval
|
|
18
|
-
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
|
-
|
|
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
|
|
16
|
-
|
|
15
|
+
export const flushEvents = () => {
|
|
16
|
+
const { region, flushSize, bufferSize, flushInterval } = resolveConfig();
|
|
17
17
|
resolveCredentials()
|
|
18
|
-
.then(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
10
|
-
export
|
|
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
|
-
|
|
14
|
+
const { region, trackingId, bufferSize, flushSize, flushInterval } = resolveConfig();
|
|
17
15
|
resolveCredentials()
|
|
18
|
-
.then(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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(
|
|
64
|
+
.catch(e => {
|
|
81
65
|
logger.warn('Failed to record event.', e);
|
|
82
66
|
});
|
|
83
67
|
};
|