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

Selfhosted not working as expected #94

Open genox opened 1 year ago

genox commented 1 year ago

Hi,

When I run my page locally it correctly uses the proxy and my self-hosted plausible registers events. However, once I deploy, no events register anymore.

Looking at the POST that is being fired I see that I get a response that I do not expect:

* We are completely uploaded and fine
< HTTP/2 202
< access-control-allow-credentials: true
< access-control-allow-origin: *
< application: 10.0.0.3
< cache-control: must-revalidate, max-age=0, private
< cdn-cachedat: 06/13/2023 22:01:12
< cdn-edgestorageid: 889
< cdn-proxyver: 1.03
< cdn-pullzone: 682664
< cdn-requestcountrycode: CH
< cdn-requestid: dcdb8a31f665641ffb1e8a3c089f89a8
< cdn-requestpullcode: 202
< cdn-requestpullsuccess: True
< cdn-uid: 153cb5b1-399a-48ef-b5bf-098c03770254
< content-type: text/plain; charset=utf-8
< date: Tue, 13 Jun 2023 22:01:12 GMT
< permissions-policy: interest-cohort=()
< server: BunnyCDN-CH1-889
< vary: Accept-Encoding
< x-plausible-dropped: 1
< x-request-id: F2hW9YUo3AFRBhorsKqF
< content-length: 2

This looks like a misconfiguration and the request ends up on plausible.io, even though I have configured it as follows:

module.exports = withPWA(
  withPlausibleProxy({
    subdirectory: 's',
    scriptName: 'p.js',
    customDomain: TELEMETRY_PLAUSIBLE_HOST,
  })(nextConfiguration)
);
                <PlausibleProvider
                  domain={TELEMETRY_PLAUSIBLE_SOURCE_DOMAIN}
                  enabled={!!TELEMETRY_PLAUSIBLE_SOURCE_DOMAIN}
                  trackLocalhost={DEV}
                  trackOutboundLinks
                >

I checked that all env variables are available, which they are.

I use nextjs@latest in a pages dir setup. I tried running the production build locally to rule out PWA interference and it works fine, just not after being deployed.

In production, the page runs behind Traefik but since the initial request to the proxy api endpoint is working just fine, it has to be the server side request made by the proxy that goes to plausible.io instead of my self-hosted instance.

Here's the headers sent by the client to make that request (I removed the cookies):

curl -v 'https://demo.lasius.ch/s/api/event' \
  -H 'authority: demo.lasius.ch' \
  -H 'accept: */*' \
  -H 'accept-language: en-GB,en-US;q=0.9,en;q=0.8,de;q=0.7' \
  -H 'cache-control: no-cache' \
  -H 'content-type: text/plain' \
  -H 'origin: https://demo.lasius.ch' \
  -H 'pragma: no-cache' \
  -H 'referer: https://demo.lasius.ch/user/projects' \
  -H 'sec-ch-ua: "Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: same-origin' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' \
  --data-raw '{"n":"pageview","u":"https://demo.lasius.ch/user/projects","d":"demo.lasius.ch","r":"https://demo.lasius.ch/login?callbackUrl=https%3A%2F%2Fdemo.lasius.ch%2Fuser%2Fhome"}' \
  --compressed

Does anyone have a suggestion what I could try to find out what the issue is?

4lejandrito commented 1 year ago

Hi @genox! sorry for the late reply.

Are you sure TELEMETRY_PLAUSIBLE_HOST is properly set in your production environment (server side)? The domain used is the one passed as customDomain. The logic is pretty simple.

genox commented 1 year ago

The env variable is properly set, I double checked.

Is there a fallback implemented in the plausible script that makes it use plausible.io if the self hosted hostname is unreachable?

4lejandrito commented 1 year ago

There's no such fallback as far as I know.