ChemPal Documentation - v1.18.0
    Preparing search index...

    Module helpers/analytics

    Functions

    getFingerprintDistinctId

    Computes the device-fingerprint distinct_id: a hash of a handful of highly stable navigator signals, readable identically in the popup, options page, and the service worker (no screen/DOM dependency). Because it's a pure function of the environment rather than something stored, the same physical install produces the same id before and after a reinstall — chrome.storage.local (where the old random id lived) can be wiped with no effect on it.

    getSessionId

    Reads (or creates) the current $session_id, mirroring errorBuffer.ts's read/write pattern: stored in chrome.storage.session (dropped when the browser closes), with a fresh UUIDv7 minted when absent or older than SESSION_MAX_AGE_MS. PostHog requires $session_id to be a UUIDv7, not a plain random UUID — see generateUuidV7.

    canSendAnalytics

    Whether an event should actually leave the device right now: an API key is configured in config.json (analytics), this isn't a Vitest run (MODE === "test", independent of whether the test mocked this module — a safety net against real events leaking from a run that skips the usual test setup), and the user hasn't opted out. Shared by every capture path so trackEvent can skip getSessionId's storage write entirely when the answer is no (an opted-out user shouldn't get session-id writes just because something called trackEvent), and postCaptureEvent re-checks it as the final gate right before the fetch. The e2e suite is deliberately not covered here: it intercepts and aborts capture requests at the page level (see e2e/search-query.e2e.test.ts) and asserts on them actually firing.

    trackEvent

    Sends one event to PostHog's capture endpoint, identified by getFingerprintDistinctId and tagged with the current getSessionId. No-ops (before touching session storage) when canSendAnalytics says not to. Text params are truncated to PARAM_VALUE_LIMIT; numeric params pass through as numbers. Never throws.

    trackInstallOrUpgrade

    Reports a fresh install as extension_installed or a version change as extension_upgraded, the latter carrying both the old and new version. Driven by the service worker's chrome.runtime.onInstalled listener.

    trackRenderError

    Reports a React render-boundary crash to PostHog as a $exception event, shaped like posthog-js's own captureException output ($exception_list with parsed stack frames, $exception_level), so it lands in PostHog's Error Tracking product with a stack-trace viewer, per-frame file/line, and automatic issue grouping. error_name/error_message are also kept as flat properties alongside the structured list, for any existing dashboard built on those two fields; error_cause is added the same way when error.cause is set, even though the full cause chain is also walked into $exception_list (see buildExceptionList) — this flat field makes the immediate cause filterable/visible without opening the stack trace.

    Type Aliases - Helpers

    AnalyticsValue

    A JSON-safe value a PostHog event property may hold — the primitives trackEvent always accepted, plus arrays/objects for structured properties like $exception_list.

    Variables - Helpers

    CAPTURE_PATH

    PostHog single-event capture path, appended to the configured host.

    PARAM_VALUE_LIMIT

    Self-imposed cap on the length of a text property value, from config.json (analytics.paramValueLimit). Not a PostHog limit (its ceiling is ~1MB) — a privacy guard bounding how much of a search term or error message can leave the device.

    COMPONENT_STACK_LIMIT

    Self-imposed cap on how much of a React component stack rides along in a $exception event, from config.json (analytics.componentStackLimit). Unlike JS stack frames (capped by count via STACKTRACE_FRAME_LIMIT), a component stack is one unstructured string, so it's bounded by length.