import { componentInterface } from '../factory'; import { optionsInterface, DEFAULT_API_ENDPOINT } from '../options'; import { getVersion } from './version'; import { getExperimentalPayload } from './experimental'; import type { ThumbmarkError } from '../functions'; import type { ObservationsCollection } from './observations'; // ===================== Logging (Internal) ===================== /** * Logs thumbmark data to remote logging endpoint (only once per session) * You can disable this by setting options.logging to false. * Options are sent without credentials or customer metadata (api_key, metadata). * * The experimental payload is fetched here rather than by the caller because * this is its only consumer. That keeps it off getThumbmark's critical path — * this whole function is fire-and-forget, so the round-trip costs the caller * nothing. * * `observations`, when given, is added as a sibling of `components` (outside * the hash) from a snapshot taken when the log is built, so it can include keys * that resolved after the API request's snapshot. It never waits for a pending key. * @internal */ export async function logThumbmarkData(thisHash: string, thumbmarkData: componentInterface, options: optionsInterface, errors: ThumbmarkError[] = [], observations?: Pick): Promise { const apiEndpoint = DEFAULT_API_ENDPOINT; const url = `${apiEndpoint}/log`; sessionStorage.setItem("_tmjs_l", "1"); // Snapshot last, once the payload is otherwise ready, so it holds as much as has resolved. const experimental = await getExperimentalPayload(); const observed = observations?.snapshot(); const { api_key: _omitApiKey, metadata: _omitMetadata, ...loggableOptions } = options; const payload = { thumbmark: thisHash, components: thumbmarkData, ...(observed && { observations: observed }), experimental, version: getVersion(), options: loggableOptions, path: window?.location?.pathname, ...(errors.length > 0 && { errors }), }; try { await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); } catch { /* do nothing */ } }