Skip to content
Frameworks

Express.js analytics with a first-party proxy

2 min read · Updated · By the Reveata team

Short answerPaste the Reveata snippet into the layout, static HTML or front-end index.html your Express app serves. Two Express routes can serve the script from your own domain with your proxy secret, and your checkout route can copy the visitor id into the checkout metadata.

Express apps often do three jobs at once: serve pages, run the API and create checkouts. Reveata plugs into all three with a script tag, two optional proxy routes, and two metadata keys.

What you get

  • Every page tracked, whether Express renders views, serves static files or serves a single-page app's build.
  • A first-party proxy in two routes, so the script and events go through your own domain and fewer visits are lost to ad blockers.
  • Checkout payments credited to visits, from the route that creates the session.
  • Your own billing posted to the Payments API.

Where the snippet goes

  • A view engine (EJS, Pug, Handlebars): the layout or partial that renders <head>, such as views/partials/head.ejs. Express's view engines are covered in Express's template engine guide (opens in a new tab).
  • Static files with express.static('public'): each HTML file, or the one index.html of a single-page app.
  • A front-end build (React, Vue, Svelte with Vite): that front end's index.html (React analytics).

Page changes in a single-page app go through the History API, which the script follows.

The proxy routes

  1. Create a proxy secret in Settings → General → Tracking code and store it as REVEATA_PROXY_SECRET. Never send it to the browser.
  2. Add the two routes from Install on Express before your other routes (Node.js 18 or later).
  3. Point the snippet at them.

Set trust proxy to the number of proxies in front of your app. Without any proxy in front, remove the line, or anyone could set req.ip with an X-Forwarded-For header. To check, find the /dt/event request in the browser's Network tab: its x-reveata-proxy response header says trusted when the secret was accepted.

Linking payments to visits

js
const visitorId = req.body.reveata_visitor_id || trackerId(req.headers.cookie, '_dt_v');
const session = await stripe.checkout.sessions.create({
  mode: 'payment',
  line_items: [{ price: req.body.priceId, quantity: 1 }],
  success_url: 'https://example.com/thanks',
  metadata: { ...(visitorId && { reveata_visitor_id: visitorId }) },
});

The trackerId cookie parser is in the Stripe Checkout guide. More in Stripe Checkout attribution.

Frequently asked questions

Does Helmet's Content Security Policy block Reveata?

It can. Allow the Reveata script, or use the proxy so 'self' covers it (Content Security Policy).

What if trust proxy is set wrong?

Too low and every visit gets your load balancer's address; on with no proxy in front and visitors could choose their own address.

Can I skip the proxy code?

Yes. The managed proxy does the same with one DNS record.

See it with your own numbersReveata ties every visit and sale to the source that brought it. Add your website in a minute.
Get started
Was this page useful?
IntegrationStripe Checkout analytics and attributionAdd the Reveata visitor and session ids to the metadata of each Stripe Checkout Session your server creates. When Stripe reports the payment, Reveata reads the ids and credits the money to the exact visit that started the checkout.IntegrationAngular analytics with ReveataPaste the Reveata snippet before the closing head tag of src/index.html. ng build keeps it in the built page, server-side rendering included, and the Angular Router's History API navigation is counted automatically. HashLocationStrategy needs data-hash="true".IntegrationAstro analytics with revenue attributionPaste the Reveata snippet with is:inline into the Astro layout that renders the head of every page. Astro then leaves the tag as written, view transitions are tracked, and payment links or checkouts started from your pages are credited to the visit behind them.IntegrationDjango analytics with revenue attributionPaste the Reveata snippet before the closing head tag of the base template your Django templates extend. Every page is tracked, the Django admin is not, and two hidden form fields let your checkout view put the visitor id into the payment's metadata.IntegrationDocusaurus analytics for documentation sitesAdd the Reveata script to the scripts array in docusaurus.config.js, or build the entry with @reveata/next/snippet. Page changes inside the docs are tracked by the script, so every page a reader opens is counted.IntegrationFastAPI analytics with ReveataPaste the Reveata snippet into the base Jinja2 template of your FastAPI app, or into the index.html of the front end FastAPI serves. Two FastAPI routes can serve the script from your own domain, and your checkout endpoint can put the visitor id into the payment's metadata.