UNPKG

@aws-amplify/analytics

Version:

Analytics category of aws-amplify

48 lines (46 loc) • 2.28 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.configureAutoTrack = configureAutoTrack; const errors_1 = require("../../../errors"); const utils_1 = require("../../../utils"); const record_1 = require("./record"); // Configured Tracker instances for Kinesis const configuredTrackers = {}; function configureAutoTrack(...args) { // Peek the optional leading context WITHOUT falling back to the global // context. The context is only needed when events are emitted; resolving it // eagerly would (a) throw when trackers are configured before // `Amplify.configure()` and (b) pin auto-tracked events to the configuration // snapshot captured at setup time after a later `configure()` call. const { ctx, input } = (0, utils_1.peekCtxArgs)(args); (0, utils_1.validateTrackerConfiguration)(input); if (input.enable) { (0, errors_1.assertValidationError)(!!input.options?.streamName, errors_1.AnalyticsValidationErrorCode.NoStreamName); (0, errors_1.assertValidationError)(!!input.options?.partitionKey, errors_1.AnalyticsValidationErrorCode.NoPartitionKey); } // Callback that will emit an appropriate event to Kinesis when required by the Tracker. // When no explicit context was supplied, `record` resolves the global context // lazily at emit time so auto-tracked events follow the live configuration. const emitTrackingEvent = (eventName, attributes) => { const recordInput = { streamName: input.options.streamName, partitionKey: input.options.partitionKey, data: { name: eventName, attributes, }, }; if (ctx) { (0, record_1.record)(ctx, recordInput); } else { (0, record_1.record)(recordInput); } }; // Initialize or update this provider's trackers. The 'kinesis' namespace keeps // page-view tracking state isolated from other providers (e.g. Pinpoint). (0, utils_1.updateProviderTrackers)(input, emitTrackingEvent, configuredTrackers, 'kinesis'); } //# sourceMappingURL=configureAutoTrack.js.map