Skip to main content

Module analytics

Module analytics 

Source
Expand description

Analytics events, with Google Analytics 4 and Tag Manager configured in .env (see AnalyticsConfig).

From a handler, whatever the response is:

renox::analytics::event(&session, "sign_up", json!({ "method": "email" }))?;

The event reaches the browser with this response when it’s an htmx swap (an HX-Trigger) or a page, or with the next page after a redirect; renox.js passes it to gtag('event', …) and, with Tag Manager, pushes it to the dataLayer. Page views need nothing: GA4 counts the history changes hx-boost makes (enhanced measurement), and GTM has a History Change trigger.

Events that must not be lost to ad blockers (a purchase) can go from the server instead, through the Measurement Protocol:

use renox::analytics::{GaClientId, ServerEvent};

async fn paid(State(state): State<AppState>, GaClientId(client): GaClientId) -> Result<Redirect> {
    state.dispatch(ServerEvent::new(client, "purchase").param("value", 18_000).param("currency", "IDR")).await?;
    Ok(Redirect::to("/thanks"))
}

Structs§

Event
One analytics event: a name such as sign_up and its parameters.
GaClientId
The GA4 client id from the _ga cookie (GA1.1.123.456 → 123.456), for server-side events. None when the visitor has none (no consent, an ad blocker, or analytics off).
ServerEvent
An event sent from the server to GA4 (Measurement Protocol), as a queue job so a slow or failing request never holds up the page. Needs GA4_MEASUREMENT_ID and GA4_API_SECRET; outside production it’s only logged.

Functions§

event
Queues an event for the visitor’s browser (see the module docs).