ilusm.dev

ana

Analytics - event tracking, sessions, funnels, retention, cohorts, A/B testing, and channel attribution.

Load with: use ana

What this module does

ana is a self-contained analytics engine. You create a tracker, feed it events, then query it for metrics: active users, visit counts, funnel conversion rates, cohort retention, session lengths, and more. Everything is in-memory - you export to CSV or JSON when you need persistence.

It also includes GDPR helpers (anonymise or delete a user's data), A/B test event recording, and last-click / first-click / linear channel attribution.

Quick example

use ana

# Create a tracker for your app
tr = anane("myapp")

# Record a page view
ev = anavt("pgv", "user123", {url: "/home", ref: "google"})
tr = antrk(tr, ev)

# Record a goal conversion
tr = anagl(tr, "user123", "signup", 1)

# Get active users in the last hour
prn(ancts(tr, 3600))

Functions

Tracker setup

anane(ns)

Creates a new tracker with the given namespace string. Returns an object with ns, evts (event list), users, sesh (sessions), props, dim (dimensions), and mets (metrics) fields - all empty.

Events

anavt(evt, usr, props)

Creates an event object with the event name, user ID, current timestamp (tim.now()), session ID (taken from props.sesh), and the full props object.

antrk(tr, evt)

Appends an event to the tracker and updates the user's record (creating it if new). Returns the updated tracker.

anpgv(tr, usr, url, ref)

Shorthand for tracking a page view event ("pgv") with url and ref (referrer) in the props.

anagl(tr, usr, goal, val)

Records a goal conversion event. The event name is "goal:{goal}" with val in the props.

Sessions

anss0(tr, usr)

Starts a new session for a user. Generates a session ID from the user ID and current millisecond timestamp. Returns the session ID.

ansnd(tr, sid, evt)

Adds an event to a session. If the event is a page view ("pgv"), increments the session's page view counter.

anssh(tr, sid)

Returns the session duration in milliseconds - time of the last event minus the session start time.

User properties

ansrs(tr, usr, props)

Sets properties on a user record. Creates the user if they don't exist yet.

Funnels

anfnn(tr, steps, win)

Calculates funnel conversion for a list of event names. win is the time window in seconds (defaults to 7 days). Returns a list of {step, n, conv} objects - n is unique users who hit that step, conv is the conversion rate from the previous step.

Retention

anrtn(tr, st, per, days)

Cohort retention analysis. Takes users who joined in the period starting at st of length per, and for each of days days checks what fraction were active. Returns a list of {day, ret} objects.

Cohorts

anchr(tr, dim, gran)

Groups users into cohorts by their creation date. gran controls granularity: "day", "mth" (default), or year.

Metrics

ancts(tr, per)

Returns the count of unique active users within the last per seconds.

anvts(tr, per)

Returns the total number of events within the last per seconds.

ansss(tr, per)

Returns the number of sessions started within the last per seconds.

ancvr(tr, goal, per)

Conversion rate for a goal within the last per seconds - goal completions divided by active users. Returns 0 if there are no active users.

Segments and time series

anasg(tr, cond)

Filters users by a condition function. Returns the matching user records.

anfts(tr, met, gran, days)

Builds a time series for a metric function met(tr, start, end) over the last days days. Returns a list of {t, v} points.

anart(tr, win)

Returns events from the last win seconds (default 300). Useful for a real-time activity view.

Dashboard

andsh(tr, cfg)

Runs a list of widget configs against the tracker. Each config needs tit, ty, and fn(tr) fields. Returns a list of {tit, ty, val} results.

Export

an2cs(tr, path)

Writes all events to a CSV file at path. Columns: event, user, timestamp, properties (JSON-encoded).

an2js(tr)

Returns a JSON string of the tracker's events and users.

Sampling

ansmp(tr, rate)

Returns a copy of the tracker with events randomly sampled at the given rate (0.0–1.0). Useful for reducing data volume.

Privacy / GDPR

anann(tr, usr)

Anonymises a user by replacing their ID with a SHA-256 hash in all events, and removes their user record. Returns the updated tracker.

andls(tr, usr)

Deletes all events for a user and removes their user record. Returns the updated tracker.

A/B testing

anaab(tr, exp, var)

Records an A/B test exposure event with the experiment name and variant. Tracked as an "ab" event under the "system" user.

Attribution

anttr(tr, usr, touch)

Records a marketing touchpoint for a user. The touch object should contain channel info.

achls(tr, conv)

Last-click attribution - returns the most recent touchpoint before the conversion event.

achfr(tr, conv)

First-click attribution - returns the first touchpoint before the conversion event.

achln(tr, conv)

Linear attribution - returns the most common channel across all touchpoints before conversion, with value split equally across touches.

Custom dimensions and metrics

anddd(tr, nm, scope)

Registers a custom dimension with a name and scope on the tracker.

anddm(tr, nm, type, fn)

Registers a custom metric with a name, type, and a function that computes it from the tracker.

Query

anqry(tr, sel, wh, grp, ord)

SQL-like query over events. wh filters events, grp groups them, sel maps each result, ord sorts. All arguments except sel can be nil to skip that step.

API helper

anapi(tr)

Returns a convenience API object wrapping the tracker with trk, pgv, goal, dash, and stats methods bound to this tracker instance.

Session replay

anrpl(tr, sid)

Replays a session's events with a 100ms delay between each one. Returns the event list. This is a mock implementation.