Skip to content

Instantly share code, notes, and snippets.

@Xetera
Last active May 6, 2022 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xetera/c96482c070cb2c24b70e95c4d2b3562b to your computer and use it in GitHub Desktop.
Save Xetera/c96482c070cb2c24b70e95c4d2b3562b to your computer and use it in GitHub Desktop.
Adblock-proof metric proxy. Make it impossible for adblock maintainers to block your internal metrics.
import Random from "seedrandom";
import express from "express";
const alphabet = "abcdefghijklmnoprstuvwyz123456789////";
const random = (rnd: () => number, len: number, min = 0) =>
Math.floor(rnd() * (len - min)) + min;
function generateEndpoints(salts: string[]) {
return salts.map((salt) => {
const rnd = Random(salt);
const length = random(rnd, 30, 20);
const extension = [...Array(length)]
.map(() => {
const num = random(rnd, alphabet.length);
return alphabet[num];
})
.join("");
// making sure we can't have double leading slashes in the generated url
return `/${extension.replace(/^\/*/g, "")}`;
});
}
const app = express();
const metricEndpoints = generateEndpoints(process.env.METRIC_SEEDS!.split(","));
const latestMetricEndpoint = metricEndpoints[1];
for (const endpoint of metricEndpoints) {
console.log(`Listening for metrics on ${endpoint}`);
app.post(endpoint, () => {
console.log("User did something!");
});
}
app.get("/", (_, res) => {
res.write(
`
<!Doctype html>
<html>
<body>
<h1>Let's make data-driven business decisions</h1>
<script type="text/javascript">
fetch("${latestMetricEndpoint}", { method: "POST" })
</script>
</body>
</html>
`
);
});
app.listen(1234);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment