Engineering

How to Bypass Ad-Blockers for Posthog Using Next.js

RG

Rasmus GustafssonDec 7, 2023

Cover about How to Bypass Ad-Blockers for Posthog Using Next.js
Learn how to tweak Next.js config and Posthog setup to bypass ad-blockers, ensuring complete analytics even with ad-blocking browsers like Arc.

With new browsers like Arc blocking ads by default and users flocking to them, it's getting tougher to get the full picture about how your product is used from product analytics. It's like trying to understand the plot of a movie when you've missed half the scenes. Frustrating, right?
We struggled with this, and found ourselves missing important data in our analytics to be able to understand how our product is being used and how we can improve it. We’re sure that many others have also been facing this problem, so here’s a quick write-up of how we use Next.js rewrites to be able to get product analytics from all of our users, regardless if they have an ad-blocker enabled or not.

First things first, let's tweak that . It's our secret sauce to making sure Posthog sneaks past those ad-blockers like a ninja:

/** @type {import("next").NextConfig} */
const config = {
	reactStrictMode: true,
  // ... the rest of your config
	...(process?.env?.NODE_ENV === 'production' && {
		async rewrites() {
			return [
				// Posthog
				{
					source: '/posthog/:path*',
					destination: 'https://eu.posthog.com/:path*'
				},
				{
					source: '/posthog/:path*/',
					destination: 'https://eu.posthog.com/:path*/'
				}
			]
		},
		async headers() {
			async function getMyIp() {
				// const x = await fetch('https://api.ipify.org')
				const x = await fetch('https://api.my-ip.io/ip')
				return await x.text()
			}
			const ip = await getMyIp()
			return [
				{
					source: '/posthog/:path*',
					headers: [
						{ key: 'X-Forwarded-Proto', value: 'https' },
						{ key: 'X-Forwarded-Host', value: 'https://www.useflytrap.com' },
						{ key: 'X-Forwarded-For', value: ip }
					]
				},
				{
					source: '/posthog/:path*/',
					headers: [
						{ key: 'X-Forwarded-Proto', value: 'https' },
						{ key: 'X-Forwarded-Host', value: 'https://www.useflytrap.com' },
						{ key: 'X-Forwarded-For', value: ip }
					]
				}
			]
		}
	})
}
export default config
  • IP API: Pick any you like. We're not picky here. It should be reliable. If the fetch fails for any reason, your build/deployment will fail.
  • Domain: Swap in your own. We're using ours (https://www.useflytrap.com) as a placeholder.
  • Posthog API Base: We’re using the EU data centers. Using the US data center? Change https://eu.posthog.com to your https://app.posthog.com.
  • Next up, the Posthog setup. This is where we tell our analytics to chill during development but go full ninja in production:

    posthog.init(clientEnv.NEXT_PUBLIC_POSTHOG_API_KEY as string, {
        api_host: process.env.NODE_ENV === 'development' ? 'https://eu.posthog.com' : '/posthog',
        // Disable in development
        loaded: (posthog) => {
            if (process.env.NODE_ENV === 'development') posthog.opt_out_capturing();
        }
    });
  • API Base: Don't forget to adjust it for your data center, again; https://eu.posthog.com for EU, or https://app.posthog.com for US
  • Remember, we're keeping it low-key in development mode.
  • So there you have it! A little tweak here, a little change there, and just like that – you've outsmarted those ad-blockers. Now you can actually see what your users are up to, which is, let's face it, why we do all this in the first place. Go forth, analyze, and maybe catch a few bugs along the way.

    Get started

    Describe the bug.

    Automate the fix.

    Get verified bug fixes directly in your editor.

    © 2024 Flytrap

    Built with ♥ in Helsinki, FI.