UNPKG

@usenotra/geo

Version:

Request capture SDK for AI traffic attribution and agent feedback collection

98 lines (90 loc) • 2.78 kB
import { createElement } from 'react'; // src/react.ts // src/constants.ts var DEFAULT_ENDPOINT = "https://ingest.usenotra.com"; // src/script/constants.ts var SCRIPT_HOST = "https://static.usenotra.com"; var SCRIPT_HOST_PATH = "/"; var SCRIPT_PATH = "/api/"; var COLLECT_PATH = "/api/collect"; var SCRIPT_FILE = "notra.js"; var SCRIPT_PLUS_FILE = "notra.plus.js"; var WEB_KEY_PREFIX = "nwk."; var INVALID_WEB_KEY_MESSAGE = 'Pass your project\'s public web key as `webKey`. It starts with "nwk." and is in the script tag under Settings \u2192 Tracking in your Notra dashboard.'; var SCRIPT_CROSS_ORIGIN = "anonymous"; // src/script/releases.ts var NOTRA_SCRIPT_RELEASE = { file: "notra.ce3c872ef9c9.js", integrity: "sha384-LuUp5Hj+rSQ4/mZp1sWBOrJDHIHKGqflwF1GYIreqmWdCaI1otOQealWJGRj6Toj" }; var NOTRA_SCRIPT_PLUS_RELEASE = { file: "notra.plus.d703b9cc5508.js", integrity: "sha384-MgLdHKufD0WdkO4pxtkT/bbNCKxl6PxOyUJe+M/qwFGfjOdZ5rEnljxTlOVQIzF+" }; // src/script/attributes.ts function trimOrigin(value) { let end = value.length; while (end > 0 && value[end - 1] === "/") { end -= 1; } return value.slice(0, end); } function scriptBase(endpoint) { return endpoint === void 0 ? `${SCRIPT_HOST}${SCRIPT_HOST_PATH}` : `${trimOrigin(endpoint)}${SCRIPT_PATH}`; } function defaultApi(options) { return options.endpoint === void 0 && options.src === void 0 ? `${DEFAULT_ENDPOINT}${COLLECT_PATH}` : void 0; } function resolveSource(options) { if (options.src) { return { src: options.src, integrity: options.integrity }; } const base = scriptBase(options.endpoint); if (options.pinned === false) { const file = options.errors ? SCRIPT_PLUS_FILE : SCRIPT_FILE; return { src: `${base}${file}`, integrity: options.integrity }; } const release = options.errors ? NOTRA_SCRIPT_PLUS_RELEASE : NOTRA_SCRIPT_RELEASE; return { src: `${base}${release.file}`, integrity: options.integrity ?? release.integrity }; } function notraScript(options) { if (!options.webKey?.startsWith(WEB_KEY_PREFIX)) { throw new Error(INVALID_WEB_KEY_MESSAGE); } const { src, integrity } = resolveSource(options); const attributes = { src, defer: true, "data-key": options.webKey }; const api = options.api ?? defaultApi(options); if (api) { attributes["data-api"] = api; } if (options.hash) { attributes["data-hash"] = ""; } if (integrity) { attributes.integrity = integrity; attributes.crossOrigin = SCRIPT_CROSS_ORIGIN; } return attributes; } // src/react.ts function NotraAnalytics({ disabled, ...options }) { if (disabled) { return null; } return createElement("script", { ...notraScript(options), defer: void 0, async: true }); } export { NotraAnalytics };