This document explains how the shared analytics helper (SharedResources/analytics.js) works, where the data is stored, and how to disable or debug tracking when testing locally.
-
Session + defaults
- Each browser session gets a generated ID stored in
sessionStorage(sess_<timestamp>_<random>). - The helper reads
document.body.dataset.pageSlug(or the current path) to tag every event with the active page. - Optional defaults can be injected via
window.__SITE_ANALYTICS_PRESET__before the script runs.
- Each browser session gets a generated ID stored in
-
Queue + batching
- Events are pushed onto an in-memory queue (max 25 items).
- A flush timer (~2 seconds) batches rows so we only hit Supabase a few times per page load.
- Activity/heartbeat events are throttled to avoid spamming.
-
Supabase targets
- Events are written to the
site_eventstable. - Errors (only when explicitly logged) go to
site_errorswith a severity field (warning,error,critical). - Credentials/URLs come from
SharedResources/supabase-env.js, so test vs live environments automatically point at the correct Supabase project.
- Events are written to the
-
Event helpers
trackPageDrawn()is invoked automatically once per iframe/page to note that the chart rendered.trackInteraction(label, payload)is used by the chart code (bubble/line/resources) for button clicks, share actions, tutorial launches, etc.- Heartbeat logic periodically sends
*_page_seenevents while the tab stays active.
Add these query parameters to the page URL when testing:
| Flag | Effect |
|---|---|
?analytics=off |
Disables all tracking for the current page load. The script still initializes but early-returns before enqueueing anything. |
?debug=1 or ?analyticsDebug=1 |
Enables verbose console logging so you can see events being queued/flushed. ?logs=1 works as well. |
- No PII is collected; only session IDs, basic browser fingerprinting, viewport info, and high-level interaction names are stored.
- The code skips analytics entirely if Supabase isn’t configured (e.g., during local file testing) to avoid console noise.
- Passive activity listeners (mouse move, scroll, touch) are throttled to 2.5s, and heartbeats stop after 60 seconds of inactivity.
- Keep the table names (
site_events,site_errors) in sync withlinechart/analytics_setup.sqlif schema changes are needed. - Test with
?debug=1to confirm events look correct before deploying. - Remember to update BOTH environments’ Supabase projects if you add new columns or policies.