@aws-amplify/analytics
Version:
Analytics category of aws-amplify
99 lines (97 loc) • 3.6 kB
JavaScript
;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.record = void 0;
const utils_1 = require("@aws-amplify/core/internals/utils");
const core_1 = require("@aws-amplify/core");
const utils_2 = require("../utils");
const utils_3 = require("../../../utils");
const constants_1 = require("../utils/constants");
const logger = new core_1.ConsoleLogger('Personalize');
/**
* Record one analytic event and send it to Personalize. Events will be buffered and periodically sent to Amazon
* Personalize.
*
* For more examples, you can refer to {@link https://docs.amplify.aws/javascript/build-a-backend/more-features/analytics/personalize-recommendations/#working-with-the-api the API usage guidance.}
*
* @param input The input object used to construct the request.
*
* @throws validation: {@link AnalyticsValidationErrorCode} - Thrown when the provided parameters or library
* configuration is incorrect.
*
* @example
* ```ts
* // Record an `Identify` event to Personalize.
* record({
* eventType: "Identify",
* properties: {
* userId: "<USER_ID>"
* }
* });
* ```
* @param input - The event to record.
*
* @returns void
*/
const record = ({ userId, eventId, eventType, properties, }) => {
if (!(0, utils_3.isAnalyticsEnabled)()) {
logger.debug('Analytics is disabled, event will not be recorded.');
return;
}
const { region, trackingId, bufferSize, flushSize, flushInterval } = (0, utils_2.resolveConfig)();
(0, utils_3.resolveCredentials)()
.then(async ({ credentials, identityId }) => {
const timestamp = Date.now();
const { sessionId: cachedSessionId, userId: cachedUserId } = await (0, utils_2.resolveCachedSession)();
if (eventType === constants_1.IDENTIFY_EVENT_TYPE) {
(0, utils_2.updateCachedSession)(typeof properties.userId === 'string' ? properties.userId : '', cachedSessionId, cachedUserId);
}
else if (userId) {
(0, utils_2.updateCachedSession)(userId, cachedSessionId, cachedUserId);
}
const { sessionId: updatedSessionId, userId: updatedUserId } = await (0, utils_2.resolveCachedSession)();
const eventBuffer = (0, utils_2.getEventBuffer)({
region,
flushSize,
flushInterval,
bufferSize,
credentials,
identityId,
userAgentValue: (0, utils_3.getAnalyticsUserAgentString)(utils_1.AnalyticsAction.Record),
});
if (eventType === constants_1.MEDIA_AUTO_TRACK_EVENT_TYPE) {
(0, utils_2.autoTrackMedia)({
trackingId,
sessionId: updatedSessionId,
userId: updatedUserId,
event: {
eventId,
eventType,
properties,
},
}, eventBuffer);
}
else {
eventBuffer.append({
trackingId,
sessionId: updatedSessionId,
userId: updatedUserId,
event: {
eventId,
eventType,
properties,
},
timestamp,
});
}
if (eventBuffer.length >= bufferSize) {
eventBuffer.flushAll();
}
})
.catch(e => {
logger.warn('Failed to record event.', e);
});
};
exports.record = record;
//# sourceMappingURL=record.js.map