UNPKG

@aws-amplify/analytics

Version:

Analytics category of aws-amplify

85 lines (82 loc) • 3.38 kB
import { resolveCtxArgs, 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'; import '../../../utils/deprecatePinpoint.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 const logger = new ConsoleLogger('Personalize'); function record(...args) { const [ctx, input] = resolveCtxArgs(args); const { userId, eventId, eventType, properties } = input; if (!isAnalyticsEnabled()) { logger.debug('Analytics is disabled, event will not be recorded.'); return; } const { region, trackingId, bufferSize, flushSize, flushInterval } = resolveConfig(ctx); resolveCredentials(ctx) .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