2023-11-21T15:58:33.805608029-zjjy.png

Plausible是一种开源的网站流量统计工具,它可以帮助你获取有关网站访问量、访问来源、受访页面等方面的统计数据。以下是关于Plausible的一些主要特点:

  1. 隐私导向:Plausible致力于保护用户隐私,不使用cookie、不进行个人身份追踪。它仅收集匿名的统计数据,满足GDPR等隐私法规的要求。

  2. 简单易用:Plausible的界面简洁清晰,易于使用。你只需要在网站上添加一小段JavaScript代码,就能开始跟踪流量统计。

  3. 实时数据:Plausible提供实时的数据更新,你可以随时了解网站的最新访问情况。

  4. 自定义事件追踪:除了基本的页面访问统计,Plausible还支持自定义事件的追踪,帮助你跟踪特定的用户行为。

  5. 开放源代码:Plausible是开源项目,代码公开透明,任何人都可以参与贡献和改进。

下载项目

git clone https://github.com/plausible/hosting

修改docker-compose.yml

version: "3.3"
services:
  caddy:
    image: caddy
    container_name: caddy
    restart: unless-stopped
    network_mode: host
    volumes:
      - /data/caddy/data:/data
      - /data/caddy/Caddyfile:/etc/caddy/Caddyfile
  mail:
    image: bytemark/smtp
    restart: always
    container_name: mail
  plausible_db:
    # supported versions are 12, 13, and 14
    image: postgres:14-alpine
    container_name: plausible_db
    restart: always
    volumes:
      - /data/postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres

  plausible_events_db:
    image: clickhouse/clickhouse-server:23.3.7.5-alpine
    restart: always
    container_name: plausible_events_db
    volumes:
      - /data/events:/var/lib/clickhouse
      - ./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: plausible/analytics:v2.0
    container_name: plausible
    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
      - mail
    ports:
      - 8000:8000
    env_file:
      - plausible-conf.env

创建挂载目录

mkdir -p /data/caddy/data/logs
mkdir /data/events
mkdir /data/postgres

修改配置

#生成秘钥
openssl rand -base64 64 | tr -d '\n' ; echo
#修改配置
vim plausible-conf.env
BASE_URL=填你打算给plausible准备的域名,比如https://analytics.baidu.com
SECRET_KEY_BASE=填刚刚生成的那个密钥
DISABLE_REGISTRATION=invite_only  #不允许注册

Caddy配置

(security_headers) {
    header * {
        # enable HSTS
        # https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#strict-transport-security-hsts
        # NOTE: Read carefully how this header works before using it.
        # If the HSTS header is misconfigured or if there is a problem with
        # the SSL/TLS certificate being used, legitimate users might be unable
        # to access the website. For example, if the HSTS header is set to a
        # very long duration and the SSL/TLS certificate expires or is revoked,
        # legitimate users might be unable to access the website until
        # the HSTS header duration has expired.
        # The recommended value for the max-age is 2 year (63072000 seconds).
        # But we are using 1 hour (3600 seconds) for testing purposes
        # and ensure that the website is working properly before setting
        # to two years.

        Strict-Transport-Security "max-age=3600; includeSubDomains; preload"

        # disable clients from sniffing the media type
        # https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#x-content-type-options
        X-Content-Type-Options "nosniff"

        # clickjacking protection
        # https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#x-frame-options
        X-Frame-Options "DENY"

        # xss protection
        # https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#x-xss-protection
        X-XSS-Protection "1; mode=block"

        # Remove -Server header, which is an information leak
        # Remove Caddy from Headers
        -Server

        # keep referrer data off of HTTP connections
        # https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#referrer-policy
        Referrer-Policy strict-origin-when-cross-origin
    }
}

{
    admin :2019
    servers {
        metrics
    }
}

analytics.baidu.com:443 {
    import security_headers
    encode gzip
    route /* {
        rate_limit {remote.ip} 10r/s 10000 401
        reverse_proxy /* IP:8000
    }
    log {   
        output file /data/caddy/logs/access.log {
            roll_size     100MiB
            roll_keep     30
            roll_keep_for 24h
        }   
        format json {
            time_format "iso8601"
        }   
    }
}

站点配置

如果你想了解更多关于Plausible的信息,可以访问官方网站:https://plausible.io/

如果你对Plausible的代码相关问题感兴趣,以下是示例代码来添加Plausible跟踪代码到网站中:

<script async defer data-domain="your-domain.com" src="https://plausible.io/js/plausible.js"></script>

你需要将"your-domain.com"替换为你自己的域名。将上述代码添加到你网站的页面中,通常放在<head>标签内或页面底部的</body>标签前。

请注意,上述代码是用于Plausible的标准跟踪,如果需要添加自定义事件追踪,可以参考Plausible的文档或官方网站上的更多指南和示例。

效果图

2023-11-21T15:57:40.451047470-cefc.jpg