4lejandrito / next-plausible

Simple integration for https://nextjs.org and https://plausible.io analytics
https://next-plausible.vercel.app
MIT License
603 stars 33 forks source link

withPlausibleProxy wrapper not forwarding country #75

Closed alexandre-dos-reis closed 12 months ago

alexandre-dos-reis commented 1 year ago

Hi @4lejandrito and thank you for your excellent work !

I tested the 2 methods described in the docs.

1° This config is working properly, I get all the data in my plausible dashboard.

_app.tsx

<PlausibleProvider
      domain="my-next.app"
      customDomain="https://my-plausible-self-hosted.app"
      selfHosted
    >
      {...rest_of_my_app}
</PlausibleProvider>

2° This method is working partially, I'm missing the countries data in my plausible dashboard.

_app.tsx

<PlausibleProvider
      domain="my-next.app"
      customDomain="https://my-plausible-self-hosted.app"
      selfHosted
    >
      {...rest_of_my_app}
</PlausibleProvider>

next.config.js

const { withPlausibleProxy } = require('next-plausible')

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
}

module.exports = withPlausibleProxy({
  customDomain: "https://my-plausible-self-hosted.app"
})(nextConfig)

I think this is caused by the second option not properly passing the header x-forwarded-for.

By the way, is the second config correct ?

Thank you.

4lejandrito commented 1 year ago

Hi @alexandre-dos-reis! Sorry for the late response.

  1. I think both methods look correct.
  2. Next plausible relies on nextjs rewrites to do the proxying. As far as I know all the headers are forwarded. I have no control over the headers. I think you might be able to tweak them through a middleware.

Let me know your findings.

nealf commented 1 year ago

I also ran into this issue, where it seems the nextjs rewrites does not pass the correct X-Forwarded-For header during the proxying. I'm in the position that I have a workaround that proxies the plausible event traffic before it reaches the nextjs app.

I couldn't spend too much time investigating, but I did find this discussion with an open pull request mentioned from September 2022 that would allow custom headers, but I think @4lejandrito might be correct that as of NextJS v13, using middleware to add request headers is the best route. If I ever get around to trying it, I'll report back!

4lejandrito commented 12 months ago

I am closing this as there is little we can do on the library side.