UNPKG

@grafana/faro-web-sdk

Version:

Faro instrumentations, metas, transports for web.

117 lines 6.59 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeCoreConfig = makeCoreConfig; const faro_core_1 = require("@grafana/faro-core"); const consts_1 = require("../consts"); const instrumentations_1 = require("../instrumentations"); const session_1 = require("../instrumentations/session"); const const_1 = require("../instrumentations/userActions/const"); const metas_1 = require("../metas"); const k6_1 = require("../metas/k6"); const page_1 = require("../metas/page"); const transports_1 = require("../transports"); const getWebInstrumentations_1 = require("./getWebInstrumentations"); function makeCoreConfig(browserConfig) { var _a, _b, _c, _d, _e; const transports = []; const internalLogger = (0, faro_core_1.createInternalLogger)(browserConfig.unpatchedConsole, browserConfig.internalLoggerLevel); if (browserConfig.transports) { if (browserConfig.url || browserConfig.apiKey) { internalLogger.error('if "transports" is defined, "url" and "apiKey" should not be defined'); } transports.push(...browserConfig.transports); } else if (browserConfig.url) { transports.push(new transports_1.FetchTransport({ url: browserConfig.url, apiKey: browserConfig.apiKey, })); } else { internalLogger.error('either "url" or "transports" must be defined'); } const { // properties with default values dedupe = true, eventDomain = consts_1.defaultEventDomain, globalObjectKey = faro_core_1.defaultGlobalObjectKey, instrumentations = (0, getWebInstrumentations_1.getWebInstrumentations)(), internalLoggerLevel = faro_core_1.defaultInternalLoggerLevel, isolate = false, logArgsSerializer = faro_core_1.defaultLogArgsSerializer, metas = createDefaultMetas(browserConfig), paused = false, preventGlobalExposure = false, unpatchedConsole = faro_core_1.defaultUnpatchedConsole, url: browserConfigUrl, experimental } = browserConfig, // Properties without default values or which aren't used to create derived config restProperties = __rest(browserConfig, ["dedupe", "eventDomain", "globalObjectKey", "instrumentations", "internalLoggerLevel", "isolate", "logArgsSerializer", "metas", "paused", "preventGlobalExposure", "unpatchedConsole", "url", "experimental"]); // Extract experimental features with defaults const trackNavigation = (_a = experimental === null || experimental === void 0 ? void 0 : experimental.trackNavigation) !== null && _a !== void 0 ? _a : false; // Extract user actions instrumentation with defaults const userActionsInstrumentation = { dataAttributeName: (_c = (_b = browserConfig.userActionsInstrumentation) === null || _b === void 0 ? void 0 : _b.dataAttributeName) !== null && _c !== void 0 ? _c : const_1.userActionDataAttribute, excludeItem: (_d = browserConfig.userActionsInstrumentation) === null || _d === void 0 ? void 0 : _d.excludeItem, }; return Object.assign(Object.assign({}, restProperties), { batching: Object.assign(Object.assign({}, faro_core_1.defaultBatchingConfig), browserConfig.batching), dedupe: dedupe, globalObjectKey, instrumentations: getFilteredInstrumentations(instrumentations, browserConfig), internalLoggerLevel, isolate, logArgsSerializer, metas, parseStacktrace: instrumentations_1.parseStacktrace, paused, preventGlobalExposure, transports, unpatchedConsole, eventDomain, ignoreUrls: [ ...((_e = browserConfig.ignoreUrls) !== null && _e !== void 0 ? _e : []), // ignore configured cloud collector url by default ...(browserConfigUrl ? [browserConfigUrl] : []), // Try our best to exclude collector URLs form other Faro instances. By default these are URLs ending with /collect or /collect/ followed by alphanumeric characters. /\/collect(?:\/[\w]*)?$/, ], sessionTracking: Object.assign(Object.assign(Object.assign({}, session_1.defaultSessionTrackingConfig), browserConfig.sessionTracking), crateSessionMeta({ trackGeolocation: browserConfig.trackGeolocation, sessionTracking: browserConfig.sessionTracking, })), userActionsInstrumentation, experimental: { trackNavigation, } }); } function getFilteredInstrumentations(instrumentations, { experimental }) { var _a; const trackNavigation = (_a = experimental === null || experimental === void 0 ? void 0 : experimental.trackNavigation) !== null && _a !== void 0 ? _a : false; return instrumentations.filter((instr) => { if (instr.name === '@grafana/faro-web-sdk:instrumentation-navigation' && !trackNavigation) { return false; } return true; }); } function createDefaultMetas(browserConfig) { var _a, _b; const { page, generatePageId } = (_a = browserConfig === null || browserConfig === void 0 ? void 0 : browserConfig.pageTracking) !== null && _a !== void 0 ? _a : {}; const initialMetas = [ metas_1.browserMeta, metas_1.osMeta, (0, page_1.createPageMeta)({ generatePageId, initialPageMeta: page }), ...((_b = browserConfig.metas) !== null && _b !== void 0 ? _b : []), metas_1.sdkMeta, ]; const isK6BrowserSession = (0, faro_core_1.isObject)(window === null || window === void 0 ? void 0 : window.k6); if (isK6BrowserSession) { return [...initialMetas, k6_1.k6Meta]; } return initialMetas; } function crateSessionMeta({ trackGeolocation, sessionTracking, }) { var _a; const overrides = {}; if ((0, faro_core_1.isBoolean)(trackGeolocation)) { overrides.geoLocationTrackingEnabled = trackGeolocation; } if ((0, faro_core_1.isEmpty)(overrides)) { return {}; } return { session: Object.assign(Object.assign({}, ((_a = sessionTracking === null || sessionTracking === void 0 ? void 0 : sessionTracking.session) !== null && _a !== void 0 ? _a : {})), { overrides }), }; } //# sourceMappingURL=makeCoreConfig.js.map