Install DataPocket on Express

Updated

Serve the script and forward events from your Express server. Sign in and these snippets fill in your site ID and domain. Until then, swap YOUR_SITE_ID and YOUR_DOMAIN with the values from your Install page.

  1. 1

    Add two routes (Node 18+, global fetch):

    js
    const PD = "https://datapocket.vo1d.studio";
    
    app.get("/dp/js/:file", async (req, res) => {
      if (!/^script(\.[a-z]+)?(@\d+\.\d+\.\d+)?\.js$/.test(req.params.file)) return res.sendStatus(404);
      const r = await fetch(`${PD}/js/${req.params.file}`);
      res.set("Content-Type", "application/javascript").set("Cache-Control", "public, max-age=3600").send(await r.text());
    });
    
    app.post("/dp/api/event", express.text({ type: "*/*", limit: "16kb" }), async (req, res) => {
      const ip = (req.headers["x-forwarded-for"] || req.socket.remoteAddress || "").toString().split(",")[0].trim();
      const r = await fetch(`${PD}/api/event`, {
        method: "POST",
        body: req.body,
        headers: { "Content-Type": "text/plain", "User-Agent": req.get("user-agent") || "", "X-Forwarded-For": ip },
      });
      res.status(r.status).type("application/json").send(await r.text());
    });
  2. 2

    Then load the script from your own domain. Ad blockers can't tell it apart from your own code, so visits from people whose ad blocker blocks analytics domains are still counted:

    html
    <script
      defer
      data-site-id="YOUR_SITE_ID"
      data-domain="YOUR_DOMAIN"
      src="/dp/js/script.js"
    ></script>

Revenue attribution

  1. 1

    Pass the visitor cookie to Stripe Checkout so each sale is attributed to the channel that brought the buyer:

    js
    const session = await stripe.checkout.sessions.create({
      // …your line_items, mode, success_url…
      metadata: {
        datapocket_visitor_id: req.cookies.dp_vid,
        datapocket_session_id: req.cookies.dp_sid,
      },
    });
  2. 2

    Using another payment provider? Send payments from your backend (create an API key in Settings):

    bash
    curl -X POST https://datapocket.vo1d.studio/api/v1/payments \
      -H "Authorization: Bearer dpk_…" \
      -H "Content-Type: application/json" \
      -d '{"amount": 29.99, "currency": "USD", "transaction_id": "order_123", "visitor_id": "<dp_vid cookie>"}'

Related guides

Install DataPocket on Express · DataPocket