UNPKG

@aws-amplify/analytics

Version:

Analytics category of aws-amplify

70 lines (68 loc) 2.44 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 util_utf8_1 = require("@smithy/util-utf8"); const utils_1 = require("@aws-amplify/core/internals/utils"); const core_1 = require("@aws-amplify/core"); const getEventBuffer_1 = require("../utils/getEventBuffer"); const resolveConfig_1 = require("../utils/resolveConfig"); const utils_2 = require("../../../utils"); const logger = new core_1.ConsoleLogger('Kinesis'); /** * Record one analytic event and send it to Kinesis. Events will be buffered and periodically sent to * Kinesis. * * @param params 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({ * streamName: 'myKinesisStream', * partitionKey: 'myPartitionKey', * data: { } // The data blob to put into the record * }); * ``` * @param input - The event to record. * * @returns void */ const record = ({ streamName, partitionKey, data, }) => { if (!(0, utils_2.isAnalyticsEnabled)()) { logger.debug('Analytics is disabled, event will not be recorded.'); return; } const timestamp = Date.now(); const { region, bufferSize, flushSize, flushInterval, resendLimit } = (0, resolveConfig_1.resolveConfig)(); (0, utils_2.resolveCredentials)() .then(({ credentials, identityId }) => { const buffer = (0, getEventBuffer_1.getEventBuffer)({ region, bufferSize, flushSize, flushInterval, credentials, identityId, resendLimit, userAgentValue: (0, utils_2.getAnalyticsUserAgentString)(utils_1.AnalyticsAction.Record), }); buffer.append({ region, streamName, partitionKey, event: ArrayBuffer.isView(data) ? data : (0, util_utf8_1.fromUtf8)(JSON.stringify(data)), timestamp, retryCount: 0, }); }) .catch(e => { // An error occured while fetching credentials or persisting the event to the buffer logger.warn('Failed to record event.', e); }); }; exports.record = record; //# sourceMappingURL=record.js.map