@hiveit/express
v1.1.1
Published
hiveit analytics for Express: server-side tracking on req.hiveit and a proxy route for browser events.
Maintainers
Readme
@hiveit/express
hiveit analytics for Express. Two pieces, both optional:
import express from "express";
import { createClient, hiveit, hiveitProxy } from "@hiveit/express";
const client = createClient({ endpoint: "http://hiveit:3000/e", apiKey: process.env.HIVEIT_WRITE_KEY });
const app = express();
// 1. Server-side tracking on every request.
app.use(hiveit({ client, distinctId: (req) => req.user?.id ?? req.cookies?.hiveit_id }));
app.post("/orders", (req, res) => {
req.hiveit.track("order_placed", { total: 4200 });
res.sendStatus(201);
});
// 2. The one line that lets browsers send events through your backend:
// no CORS, no ad-blocked third-party domain, and the write key stays server-side.
app.use("/api/hiveit", hiveitProxy({ target: "http://hiveit:3000", apiKey: process.env.HIVEIT_WRITE_KEY }));
process.on("SIGTERM", () => void client.shutdown());req.hiveit.identify(userId, properties) links the request's anonymous id to a known user
(the service merges the two), and req.hiveit.alias(alias) adds another id for the same
person. When no distinct id could be resolved, tracking calls are dropped and reported
through the client's onError rather than throwing inside your handlers.
