GuidesBackend

Express analytics middleware, cookieless and EU-hosted data

Track events from your Express routes. Cookieless, EU-hosted, a few lines to wire up.

Privytics tracks Express apps through `@openpanel/express`, a middleware that wraps the OpenPanel JavaScript SDK and attaches an SDK instance to every request as `req.op`. You call `req.op.track()` inside your route handlers, so events are sent from your server rather than from the browser. Setup is one install, one `app.use()` call, and a track call wherever something worth measuring happens.

Why Express teams use Privytics

Server-side events are not blocked

Events leave your own Express process and go straight to https://api.privytics.com. There is no browser script for content blockers, privacy extensions, or ITP to intercept, so backend counts stay complete even when frontend analytics is stripped out.

You already have the truth on the server

Sign-ups, payments, plan changes and API usage are all confirmed in your route handlers. Tracking them where they actually succeed avoids the usual browser problem of firing an event for something that later failed server-side.

No cookies to add to a backend

Privytics is cookieless by default and writes nothing to the device, so adding analytics to an Express API does not introduce a cookie banner, a session cookie, or new state you have to carry through your middleware chain.

Per-route control over what gets tracked

The middleware takes `trackRequest(url)` and `getProfileId(req)`, so you can keep health checks, webhooks and static routes out of your data and attach your own authenticated user id to everything else without a separate identify call on each route.

Set it up

About five minutes, on the free plan, without a credit card.

  1. Install the package

    The package declares Express as a peer dependency and supports Express 4.17 and above as well as Express 5.

    pnpm install @openpanel/express
  2. Add the middleware

    The default export returns an Express middleware and attaches the SDK to `req.op`. Point `apiUrl` at Privytics. `clientSecret` is required because these are server-side events. `trackRequest` and `getProfileId` are optional.

    import express from 'express';
    
    import createOpenpanelMiddleware from '@openpanel/express';
    
    const app = express();
    
    app.use(
      createOpenpanelMiddleware({
        apiUrl: 'https://api.privytics.com',
        clientId: 'YOUR_CLIENT_ID',
        clientSecret: 'YOUR_CLIENT_SECRET',
        // trackRequest(url) {
        //   return url.includes('/v1')
        // },
        // getProfileId(req) {
        //   return req.user.id
        // }
      })
    );
    
    app.listen(3000);
  3. Track an event in a route handler

    Use `req.op.track()` with an event name and any properties you want to break the numbers down by.

    app.get('/sign-up', (req, res) => {
      // track sign up events
      req.op.track('sign-up', {
        email: req.body.email,
      });
      res.send('Hello World');
    });
  4. Identify a user and their company

    On an authenticated route you can attach your own user id for cross-session profiles, and group events under an account with `upsertGroup` and `setGroup`.

    app.post('/login', async (req, res) => {
      const user = await loginUser(req.body);
    
      // Identify the user
      req.op.identify({ profileId: user.id, email: user.email });
    
      // Create/update the group entity
      req.op.upsertGroup({
        id: user.organizationId,
        type: 'company',
        name: user.organizationName,
        properties: { plan: user.plan },
      });
    
      // Assign the user to the group
      req.op.setGroup(user.organizationId);
    
      res.json({ ok: true });
    });

Privytics runs the open-source OpenPanel engine, so the upstream Express SDK reference applies here too. Point apiUrl at Privytics and the data lands in your Privytics project.

Questions

Why is req.op not typed in TypeScript?

The middleware adds `op` to the request object at runtime, so TypeScript needs a declaration to know about it. Add this to a `.d.ts` file or any module in your project: ```ts import { OpenPanel } from '@openpanel/express'; declare global { namespace Express { export interface Request { op: OpenPanel; } } } ```

Do I need a client secret for Express?

Yes. `clientSecret` is required for server-side events, unlike browser SDKs where the client id alone is enough. Keep it in an environment variable and never ship it to a frontend bundle.

Which versions of Express are supported?

The package declares `express` as a peer dependency with the range `^4.17.0 || ^5.0.0`, so Express 4.17 and later on the 4.x line, and Express 5, are both supported.

Does Express analytics use cookies?

No. Privytics is cookieless by default and stores nothing on the device. On the server side there is no device to identify in the first place, so the way you get stable profiles is to call `identify()` with your own user id, or to return one from `getProfileId(req)`. That is opt-in and needs your own lawful basis. For browser traffic, the anonymous visitor id is a daily-rotating salted hash of IP, user agent and project id that rotates at midnight UTC.

How do I stop tracking health checks and internal routes?

Pass a `trackRequest(url)` function to the middleware and return `false` for the paths you want to skip. There is also a `filter` option that runs before an event is sent and drops it if it returns false, plus `disabled` to turn sending off entirely in local or test environments.

Where is the data stored?

Event data is stored in EU data centres in Germany. Privytics is a managed service built on the open-source OpenPanel engine (AGPL-3.0) and is not affiliated with, endorsed by, or sponsored by OpenPanel. The SDK packages are OpenPanel's, published under the @openpanel npm scope.