Proxy guides
Netlify
Serve the ProductLense script from your Netlify domain with a _redirects rule, and forward events through a Netlify Function on the Scale plan.
Netlify proxies a path to another host when a redirect rule has status 200. That is enough to serve the script; forwarding events needs a function, because the visitor's IP address has to be added to each event.
Serve the script
_redirects
/js/a.js https://api.productlense.app/a.js 200netlify.toml
[[redirects]]
from = "/js/a.js"
to = "https://api.productlense.app/a.js"
status = 200<script defer data-site="SITE_ID" data-api="https://api.productlense.app" src="/js/a.js"></script>Forward events (Scale plan)
Store an API key with the events scope as the PL_API_KEY environment variable, then add this function. Its path makes it answer at /m/api/e with no redirect rule.
import type { Config, Context } from '@netlify/functions';
type Event = Record<string, unknown>;
export default async (req: Request, context: Context) => {
if (req.method !== 'POST') return new Response(null, { status: 405 });
const text = await req.text();
if (text.length > 8192) return new Response(null, { status: 413 });
let body: { b?: Event[] } & Event;
try {
body = JSON.parse(text);
} catch {
return new Response(null, { status: 400 });
}
const extra = { ip: context.ip, ua: req.headers.get('user-agent') ?? '' };
const out = body.b ? { ...body, b: body.b.map((e) => ({ ...e, ...extra })) } : { ...body, ...extra };
const r = await fetch('https://api.productlense.app/api/v1/events', {
method: 'POST',
headers: { Authorization: `Bearer ${Netlify.env.get('PL_API_KEY')}`, 'Content-Type': 'application/json' },
body: JSON.stringify(out),
});
return new Response(null, { status: r.status });
};
export const config: Config = { path: '/m/api/e' };Then set data-api="https://example.com/m" on the tag.
Clicks and Lens
The script loads two small files from the same folder as a.js: c.js when clicks are on, and o.js when you open Lens on your site. Lens then reads the crowd on each page from <data-api>/api/lens/…. With data-api pointing at your server, forward these too, or clicks and Lens will not load:
| The browser asks your server for | Forward it to | Keep |
|---|---|---|
GET /js/c.js and GET /js/o.js (next to a.js) | https://api.productlense.app/c.js and /o.js | the same one-day cache as a.js |
GET /m/api/lens/… with its query string | https://api.productlense.app/api/lens/… with the same query string | the browser's Origin header, and the status and body as they come back |
Lens checks that the Origin header is one of your site's hostnames, so a proxy that drops or rewrites it gets a 404. The Lens read is aggregate data only and never counts as a visit, so a plain forward is fine here, unlike /api/e.
/js/a.js https://api.productlense.app/a.js 200
/js/c.js https://api.productlense.app/c.js 200
/js/o.js https://api.productlense.app/o.js 200
/m/api/lens/* https://api.productlense.app/api/lens/:splat 200Check it works
- Open
https://example.com/js/a.js: it returns JavaScript. - Load a page with the developer tools open. The Network tab shows
a.jsfrom your domain and aPOSTtoapi/ewith status204. - Open the Live page in the ProductLense dashboard. Your visit appears within a few seconds.