Proxy Web Analytics in Next.js Using next.config Rewrites
Configure next.config.ts rewrites in 30 seconds to serve web analytics from your own domain so ad blockers stop dropping your visitors.
Add this to next.config.ts. Replace /_ds with a path of your own.
next.config.ts
// next.config.ts
import type { NextConfig } from "next"
const nextConfig: NextConfig = {
async rewrites() {
return [
{ source: "/_ds/js/script.cookies.js", destination: "https://usedataowl.com/js/script.cookies.js" },
{ source: "/_ds/api/e", destination: "https://usedataowl.com/api/e" },
{ source: "/_ds/api/p", destination: "https://usedataowl.com/api/p" },
// Session replay, if you turn it on
{ source: "/_ds/js/recorder.js", destination: "https://usedataowl.com/js/recorder.js" },
{ source: "/_ds/api/replay", destination: "https://usedataowl.com/api/replay" },
// Forms, if you create any
{ source: "/_ds/js/forms.js", destination: "https://usedataowl.com/js/forms.js" },
{ source: "/_ds/api/forms", destination: "https://usedataowl.com/api/forms" },
]
},
}
export default nextConfigThen load the script from your own domain:
html
<script
defer
data-website-id="YOUR_SITE_ID"
src="/_ds/js/script.cookies.js"
></script>Verify
Open your site with an ad blocker on. In the browser's network tab, /_ds/js/script.cookies.js and /_ds/api/e load from your own domain, and the visit shows up on the Realtime page.