@aws-amplify/analytics
Version:
Analytics category of aws-amplify
82 lines (79 loc) • 3.25 kB
JavaScript
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 { AnalyticsAction } from '@aws-amplify/core/internals/utils';
import '../../../errors/validation.mjs';
import { ConsoleLogger } from '@aws-amplify/core';
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
const logger = new ConsoleLogger('Personalize');
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