plausible / community-edition

Example Docker Compose setup for hosting Plausible Community Edition
1.28k stars 286 forks source link

Unique visitors not working (Remix) #148

Closed zamson closed 1 month ago

zamson commented 1 month ago

Just trying out the community edition and running into an issue where unique visitors are always 1. I'm running Remix and using this guide: https://rogerstringer.com/blog/proxying-plausible-in-remix

This was working properly when using the hosted version. I'm also running a Next.js site on the same install, and it's working properly.

Any ideas why unique visitors are stuck? Other events like page views update correctly.

ruslandoga commented 1 month ago

👋 @zamson

Would you be able to share your docker-compose.yml and plausible-conf.env files, with secrets redacted? Do you use any reverse-proxies or services like Cloudflare?

zamson commented 1 month ago
// docker-compose.yml
services:
  plausible_db:
    # Plausible v2.1.1 was tested against PostgreSQL versions 15 and 16
    # https://github.com/plausible/analytics/blob/v2.1.1/.github/workflows/elixir.yml#L21-L32
    image: postgres:16-alpine
    restart: always
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres

  plausible_events_db:
    image: clickhouse/clickhouse-server:24.3.3.102-alpine
    restart: always
    volumes:
      - event-data:/var/lib/clickhouse
      - event-logs:/var/log/clickhouse-server
      - ./clickhouse/clickhouse-config.xml:/etc/clickhouse-server/config.d/logging.xml:ro
      - ./clickhouse/clickhouse-user-config.xml:/etc/clickhouse-server/users.d/logging.xml:ro
    ulimits:
      nofile:
        soft: 262144
        hard: 262144

  plausible:
    image: ghcr.io/plausible/community-edition:v2.1.1
    restart: always
    command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh run"
    depends_on:
      - plausible_db
      - plausible_events_db
    ports:
      - 127.0.0.1:8000:8000
    env_file:
      - plausible-conf.env

volumes:
  db-data:
    driver: local
  event-data:
    driver: local
  event-logs:
    driver: local
// plausible-conf.env                                                                               
BASE_URL=https://plausible.mydomain.com
SECRET_KEY_BASE=x
TOTP_VAULT_KEY=x
# Email
MAILER_EMAIL=plausible@mydomain.com
SMTP_HOST_ADDR=email-smtp.eu-north-1.amazonaws.com
SMTP_HOST_PORT=587
SMTP_USER_NAME=x
SMTP_USER_PWD=x
SMTP_HOST_SSL_ENABLED=false
# Security
DISABLE_REGISTRATION=true
// nginx site config
proxy_cache_path /var/cache/nginx/data/jscache levels=1:2 keys_zone=jscache:100m inactive=30d use_temp_path=off max_size=100m;
server {
    server_name  plausible.mydomain.com;

    access_log  /var/log/nginx/plausible.access.log;
    error_log   /var/log/nginx/plausible.error.log;

    location / {
      proxy_pass http://localhost:8000;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
  }

  location = /api/event {
        proxy_pass http://localhost:8000/api/event;
        proxy_set_header Host plausible.mydomain.com;
        proxy_ssl_server_name on;
        proxy_ssl_session_reuse off;
        proxy_buffering on;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host  $host;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/plausible.mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/plausible.mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = plausible.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen       80;
    listen       [::]:80;
    server_name  plausible.mydomain.com;
    return 404; # managed by Certbot
}

I don't use Cloudflare's proxy; it's set to DNS-only mode. The site in question is a Shopify store hosted on Oxygen, built with Shopify Hydrogen (which is based on Remix). What's strange is that "current visitors" and page views are being recorded correctly, but unique visitors and total visitors aren't, and sometimes UTM tags are also not being tracked.

My other site, which is running Next.js on the same Plausible account (using next-plausible), is working as expected. This makes me think the issue might be related to the implementation in Remix. However, the same approach was working properly in Remix when using the hosted version of Plausible.

Screenshot by Dropbox Capture

Compared to another tracking service running on the same website: Screenshot by Dropbox Capture

As I mentioned I'm using a slightly modified version of this implementation method: https://rogerstringer.com/blog/proxying-plausible-in-remix

// js.script.[.js].ts
export const loader = async () => {
  const response = await fetch('https://plausible.mydomain.com/js/script.js');
  const script = await response.text();
  const {status, headers} = response;

  return new Response(script, {
    status,
    headers,
  });
};
// api.event.ts
import type {ActionFunctionArgs} from '@remix-run/node';

export const action = async ({request}: ActionFunctionArgs) => {
  const {method, body} = request;

  const response = await fetch('https://plausible.mydomain.com/api/event', {
    body,
    method,
    headers: {
      'Content-Type': 'application/json',
    },
  });

  const responseBody = await response.text();
  const {status, headers} = response;

  return new Response(responseBody, {
    status,
    headers,
  });
};
// root.tsx
...
 <script defer data-domain="mydomain.com" src="/js/script.js" />
...

My best guess is that api/events are working but not the initial visitor event. Any input appreciated!

zamson commented 1 month ago

Removing the script proxy and adding the script normally solved the issue