UNPKG

@splitsoftware/splitio-commons

Version:
79 lines (78 loc) 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.impressionsTrackerFactory = void 0; var objectAssign_1 = require("../utils/lang/objectAssign"); var thenable_1 = require("../utils/promise/thenable"); var constants_1 = require("../logger/constants"); var constants_2 = require("../utils/constants"); /** * Impressions tracker stores impressions in cache and pass them to the listener and integrations manager if provided. */ function impressionsTrackerFactory(settings, impressionsCache, noneStrategy, strategy, whenInit, integrationsManager, telemetryCache) { var log = settings.log, impressionListener = settings.impressionListener, _a = settings.runtime, ip = _a.ip, hostname = _a.hostname, version = settings.version; return { track: function (impressions, attributes) { if (settings.userConsent === constants_2.CONSENT_DECLINED) return; var impressionsToStore = impressions.filter(function (_a) { var imp = _a.imp, disabled = _a.disabled; return disabled ? noneStrategy.process(imp) : strategy.process(imp); }); var impressionsLength = impressions.length; var impressionsToStoreLength = impressionsToStore.length; if (impressionsToStoreLength) { var res = impressionsCache.track(impressionsToStore.map(function (item) { return item.imp; })); // If we're on an async storage, handle error and log it. if ((0, thenable_1.thenable)(res)) { res.then(function () { log.info(constants_1.IMPRESSIONS_TRACKER_SUCCESS, [impressionsLength]); }).catch(function (err) { log.error(constants_1.ERROR_IMPRESSIONS_TRACKER, [impressionsLength, err]); }); } else { // Record when impressionsCache is sync only (standalone mode) // @TODO we are not dropping impressions on full queue yet, so DROPPED stats are not recorded if (telemetryCache) { telemetryCache.recordImpressionStats(constants_2.QUEUED, impressionsToStoreLength); telemetryCache.recordImpressionStats(constants_2.DEDUPED, impressionsLength - impressionsToStoreLength); } } } // @TODO next block might be handled by the integration manager. In that case, the metadata object doesn't need to be passed in the constructor if (impressionListener || integrationsManager) { var _loop_1 = function (i) { var impressionData = { // copy of impression, to avoid unexpected behavior if modified by integrations or impressionListener impression: (0, objectAssign_1.objectAssign)({}, impressions[i].imp), attributes: attributes, ip: ip, hostname: hostname, sdkLanguageVersion: version }; whenInit(function () { // Wrap in a timeout because we don't want it to be blocking. setTimeout(function () { // integrationsManager.handleImpression does not throw errors if (integrationsManager) integrationsManager.handleImpression(impressionData); try { // @ts-ignore. An exception on the listeners should not break the SDK. if (impressionListener) impressionListener.logImpression(impressionData); } catch (err) { log.error(constants_1.ERROR_IMPRESSIONS_LISTENER, [err]); } }); }); }; for (var i = 0; i < impressionsLength; i++) { _loop_1(i); } } } }; } exports.impressionsTrackerFactory = impressionsTrackerFactory;