UNPKG

@aws-amplify/analytics

Version:

Analytics category of aws-amplify

107 lines (104 loc) 4.03 kB
import { AnalyticsAction } from '@aws-amplify/core/internals/utils'; import { ConsoleLogger } from '@aws-amplify/core'; import { getEventBuffer } from '../utils/getEventBuffer.mjs'; import { resolveConfig } from '../utils/resolveConfig.mjs'; import { autoTrackMedia } from '../utils/autoTrackMedia.mjs'; import { resolveCachedSession, updateCachedSession } from '../utils/cachedSession.mjs'; import { IDENTIFY_EVENT_TYPE, MEDIA_AUTO_TRACK_EVENT_TYPE } from '../utils/constants.mjs'; import { resolveCredentials } from '../../../utils/resolveCredentials.mjs'; import '../../../utils/eventBuffer/EventBuffer.mjs'; import { isAnalyticsEnabled } from '../../../utils/statusHelpers.mjs'; import { getAnalyticsUserAgentString } from '../../../utils/userAgent.mjs'; import '../../../trackers/EventTracker.mjs'; import '../../../trackers/PageViewTracker.mjs'; import '../../../trackers/SessionTracker.mjs'; import '../../../errors/validation.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 const logger = new 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 (!isAnalyticsEnabled()) { logger.debug('Analytics is disabled, event will not be recorded.'); return; } const { region, trackingId, bufferSize, flushSize, flushInterval } = resolveConfig(); resolveCredentials() .then(async ({ credentials, identityId }) => { const timestamp = Date.now(); const { sessionId: cachedSessionId, userId: cachedUserId } = await resolveCachedSession(); if (eventType === IDENTIFY_EVENT_TYPE) { updateCachedSession(typeof properties.userId === 'string' ? properties.userId : '', cachedSessionId, cachedUserId); } else if (userId) { updateCachedSession(userId, cachedSessionId, cachedUserId); } const { sessionId: updatedSessionId, userId: updatedUserId } = await resolveCachedSession(); const eventBuffer = getEventBuffer({ region, flushSize, flushInterval, bufferSize, credentials, identityId, userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record), }); if (eventType === MEDIA_AUTO_TRACK_EVENT_TYPE) { 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); }); }; export { record }; //# sourceMappingURL=record.mjs.map