UNPKG

@aws-amplify/analytics

Version:

Analytics category of aws-amplify

74 lines (72 loc) 2.82 kB
'use strict'; // 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("../utils"); const utils_2 = require("../../../utils"); const utils_3 = require("@aws-amplify/core/internals/utils"); const core_1 = require("@aws-amplify/core"); const constants_1 = require("../utils/constants"); const logger = new core_1.ConsoleLogger('Personalize'); const record = ({ userId, eventId, eventType, properties, }) => { if (!(0, utils_2.isAnalyticsEnabled)()) { logger.debug('Analytics is disabled, event will not be recorded.'); return; } const { region, trackingId, bufferSize, flushSize, flushInterval } = (0, utils_1.resolveConfig)(); (0, utils_2.resolveCredentials)() .then(async ({ credentials, identityId }) => { const timestamp = Date.now(); const { sessionId: cachedSessionId, userId: cachedUserId } = await (0, utils_1.resolveCachedSession)(); if (eventType === constants_1.IDENTIFY_EVENT_TYPE) { (0, utils_1.updateCachedSession)(typeof properties.userId === 'string' ? properties.userId : '', cachedSessionId, cachedUserId); } else if (!!userId) { (0, utils_1.updateCachedSession)(userId, cachedSessionId, cachedUserId); } const { sessionId: updatedSessionId, userId: updatedUserId } = await (0, utils_1.resolveCachedSession)(); const eventBuffer = (0, utils_1.getEventBuffer)({ region, flushSize, flushInterval, bufferSize, credentials, identityId, userAgentValue: (0, utils_2.getAnalyticsUserAgentString)(utils_3.AnalyticsAction.Record), }); if (eventType === constants_1.MEDIA_AUTO_TRACK_EVENT_TYPE) { (0, utils_1.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