umami/src/pages/api/scripts/telemetry.ts

25 lines
816 B
TypeScript
Raw Normal View History

import { ok } from 'next-basics';
2022-08-08 18:31:36 +00:00
import { CURRENT_VERSION, TELEMETRY_PIXEL } from 'lib/constants';
2024-02-04 08:09:15 +00:00
import { NextApiRequest, NextApiResponse } from 'next';
2022-08-02 07:24:17 +00:00
2024-02-04 08:09:15 +00:00
export default function handler(req: NextApiRequest, res: NextApiResponse) {
if (process.env.NODE_ENV === 'production') {
res.setHeader('content-type', 'text/javascript');
2022-08-08 18:31:36 +00:00
if (process.env.DISABLE_TELEMETRY || process.env.PRIVATE_MODE) {
return res.send('/* telemetry disabled */');
}
2022-08-08 18:31:36 +00:00
const script = `
2022-08-02 07:52:08 +00:00
(()=>{const i=document.createElement('img');
2022-08-08 18:31:36 +00:00
i.setAttribute('src','${TELEMETRY_PIXEL}?v=${CURRENT_VERSION}');
2022-08-02 07:52:08 +00:00
i.setAttribute('style','width:0;height:0;position:absolute;pointer-events:none;');
document.body.appendChild(i);})();
`;
return res.send(script.replace(/\s\s+/g, ''));
}
return ok(res);
2022-08-02 07:24:17 +00:00
}