Express.js proxy
Proxy attribu.tech analytics through your Express.js server.
This is a server-specific guide. See Proxy the tracking script for an overview of why and how proxying works.
Install the proxy middleware
npm install express-http-proxyAdd proxy routes
Create two proxy routes: one for the tracking script and one for the event API.
const express = require("express");
const proxy = require("express-http-proxy");
const app = express();
// Trust proxy headers (required for correct IP forwarding)
app.set("trust proxy", true);
// Proxy the attribu.tech tracking script
app.get("/js/attribu.js", proxy("attribu.tech", {
https: true,
proxyReqPathResolver: () => "/js/script.js",
}));
// Proxy the attribu.tech event API
app.post("/api/proxy/events", proxy("attribu.tech", {
https: true,
proxyReqPathResolver: () => "/api/events",
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
proxyReqOpts.headers["X-Forwarded-For"] = srcReq.ip;
return proxyReqOpts;
},
}));
app.listen(3000);Setting
app.set("trust proxy", true) ensures req.ip resolves to the visitor's real IP address when your app sits behind a load balancer or CDN.Update your script tag
Point the script src to your proxied path and set data-api-url to your proxied API endpoint:
<script
defer
data-website-id="YOUR_SITE_ID"
data-domain="yourdomain.com"
data-api-url="https://yourdomain.com/api/proxy/events"
src="/js/attribu.js"
></script>Replace
YOUR_SITE_ID with your actual Website ID from attribu.tech. Replace yourdomain.com with your website's root domain.Verify installation
- Visit your site in an incognito/private window.
- Open your browser's Developer Tools and go to the Network tab.
- Confirm the script loads from
/js/attribu.json your domain. - Confirm the pageview POST goes to
/api/proxy/eventson your domain. - Check your attribu.tech dashboard. A new pageview should appear within a few seconds.
Troubleshooting
- 502 Bad Gateway - Make sure your server can reach
attribu.techover HTTPS. Check DNS and firewall rules. - Script loads but no pageviews - Verify the
/api/proxy/eventsroute is handling POST requests. Check thattrust proxyis set if you are behind a reverse proxy. - CORS errors - The proxy should eliminate CORS issues since both the script and API are served from your domain. If you still see errors, make sure the proxy is not adding extra CORS headers that conflict.
What's next
- Back to proxy overview
- Nginx proxy setup
- Connect Stripe to attribute revenue to traffic sources.