GoatCounter

简单的 Web 统计。 不追踪个人数据。「Simple web statistics. No tracking of personal data.」

Github stars Tracking Chart

GoatCounter 是一个开源 Web 分析平台,可以作为托管服务(非商业用途免费)或自托管应用程序使用。它旨在提供易于使用且有意义的隐私友好型 Web 分析,以替代 Google Analytics(分析)或 Matomo。

有两种方法可以运行此代码:作为 goatcounter.com 上的托管服务,免费提供给非商业用途,或在您自己的服务器上运行(源代码完全是开放源代码/免费软件,并且可以在不进行任何操作的情况下自行托管)限制)。

关于这个项目的 "为什么",请参阅 docs/rationale.markdown

docs/rationale.markdown 上有一个现场演示。

如果您是自托管 GoatCounter,请考虑捐助,以便我支付房租:-)

GoatCounter 由 NLnet 的 NGI 零宠物基金资助。

特性

  • 隐私意识;不会使用唯一标识符跟踪用户,也不需要 GDPR 同意通知。另请参阅隐私政策。
  • 轻量级和快速;仅向您的网站添加约 5K(约2.5K压缩)的额外数据。还具有无 JavaScript 的 "跟踪像素 "选项,或者您可以从您的应用程序的中间件中使用它。
  • 简单;如果你已经被Google Analytics 和 Matomo 的无数选项和灵活性所迷惑,而你又不需要,那么 GoatCounter 将是一股清新的空气。
  • 在没有 cookies 的情况下,使用不可识别的哈希(技术细节)来识别独特的访问。
  • 保存有用的统计数据,如浏览器信息、位置和屏幕大小。跟踪推荐网站和活动。
  • 可访问性是一个高优先级的功能,界面在屏幕阅读器,无 JavaScript,甚至文本浏览器下都能很好地工作(尽管不是所有的功能在没有 JS 的情况下都能同样地工作)。
  • 100%致力于开源;你可以确切地看到代码的作用并进行改进。
  • 拥有您的数据;您可以随时导出所有数据并随时取消。
  • 只需一个脚本标签就可以集成到你的网站上。
<script data-goatcounter="https://yoursite.goatcounter.com/count"
        async src="//gc.zgo.at/count.js"></script>
  • 对于大多数人来说,JavaScript 集成是一个不错的选择,但是您也可以使用基于 JavaScript 的无图像跟踪器或集成到后端中间件中。

技术

快速:使用默认设置,每月 5 美元的 Linode VPS 可以处理约 800次/秒。
自包含的二进制文件:所有内容(包括静态资产)都在单个 〜7M 静态编译的二进制文件中。您唯一需要做的另一件事就是 SQLite 数据库文件或 PostgreSQL 连接(没有办法)。

运行你自己的

发布页面包含用于 Linux amd64、arm 和 arm64 的二进制文件。它们是静态编译的,并且几乎可以在任何 Linux 环境中工作。 GoatCounter 应该可以在 Go 支持的任何平台上运行,但是还没有二进制文件。您现在必须从源代码构建(我保证这并不难)。

请注意,此自述文件适用于最新版本的主机。使用 release-1.4分支来获取 1.4 README。

通常来说,仅支持最新版本,尽管关键修复程序(安全性,数据丢失等)可能会反向移植到以前的版本中。

部署脚本等

"StackScript" for Linode;您也可以将其用于其他 Alpine Linux 计算机。

每月5美元的 Linode 足以运行 GoatCounter,除非您拥有数百万的综合浏览量。如果您还没有 Linode 帐户,请考虑使用我的推荐网址,我将从 Linode 处获得一些现金返还:-)

有些人已经创建了 Dockerfiles。你并不真的需要 Docker,因为 GoatCounter 是一个没有外部依赖的静态二进制文件;恕我直言,它可能制造的问题比解决的问题多。无论如何,如果你一定要这样做的话,这里有一些看起来还不错的文件:

https://github.com/baethon/docker-goatcounter (https://hub.docker.com/r/baethon/goatcounter)
https://github.com/sent-hil/dokku-gocounter
https://github.com/anarcat/goatcounter/blob/Docker...

从源代码构建

使用以下代码从源代码编译:

$ git clone -b release-1.4 https://github.com/zgoat/goatcounter.git
$ cd goatcounter
$ go build -ldflags="-X main.version=$(git log -n1 --format='%h_%cI')" ./cmd/goatcounter

-ldflags=[..] 设置版本;严格来说,这不是必需的,但是建议使用它,因为它用于“清除”静态文件的缓存,并且在以后报告错误时也很有用。它可以是任何字符串,并且不遵循任何特定格式,您也可以将其设置为当前日期或香蕉或任何您真正想要的东西。

或构建一个静态链接的二进制文件:

$ go build -ldflags="-X main.version=$(git log -n1 --format='%h_%cI')" \
    -tags osusergo,netgo,sqlite_omit_load_extension \
    -ldflags='-extldflags=-static' \
    ./cmd/goatcounter

现在,您将在当前目录中拥有一个 goatcounter 二进制文件。

您需要 Go 1.13 或更高版本以及 C 编译器(用于 SQLite),或者使用 CGO_ENABLED=0 对其进行编译,然后使用 PostgreSQL 进行编译。

建议使用上述命令中的最新版本。 master 分支应该相当稳定,但不能保证,有时我在实际发布之前不会写详细的发行/升级说明。

不建议在 GOPATH 模式下使用 go get,因为它将忽略 go.mod 中的依赖版本。

运行

您可以使用以下方法启动服务器:

$ goatcounter serve

默认是在 ./db/goatcounter.sqlite3 使用 SQLite 数据库,如果尚不存在,则会创建该数据库。请参阅 -db 标志和 goatcounter 帮助 db 对此进行定制。

默认情况下,GoatCounter 将在端口 *:80 和 *:443 上侦听。您不需要以 root 身份运行它,并且可以使用以下命令在 Linux 上授予适当的权限:

$ setcap 'cap_net_bind_service=+ep' goatcounter

由于 ACME/Let 的 Encrypt 证书生成,在其他端口上侦听可能会有些棘手。goatcounter 帮助侦听这些文件。

您可以使用 create 命令创建新站点:

$ goatcounter create -email me@example.com -domain stats.example.com

这将要求您输入新帐户的密码;您还可以在命令行上使用 -password 添加密码。如果使用自定义数据库,则还必须在此处传递 -db 标志。

更新

更新时,您可能需要运行运行数据库迁移。使用 goatcounter -automigrate 始终在启动时运行所有挂起的迁移。这是最简单的方法,尽管可以说不是“最佳”方法。

使用 goatcounter migrate <file>或 goatcounter migrate all 来手动运行迁移。通常,您要上传新版本,在旧版本仍在运行时运行迁移,然后重新启动,以便新版本生效。

使用 goatcounter migrate show 获取未决迁移的列表。

PostgreSQL

SQLite 和 PostgreSQL 均受支持。 SQLite 应该适合大多数人,并且是推荐的数据库引擎。 PostgreSQL 在大多数情况下都不会更快,并且首先添加支持的主要原因是支持在多台服务器上平衡 Web 请求的负载。要使用它:

创建数据库,与SQLite 不同,它不是自动完成的(您可能需要修改 -db 标志):

$ createdb goatcounter
$ psql goatcounter -c '\i db/schema.pgsql'
$ goatcounter -db 'postgresql://dbname=goatcounter' migrate all

使用自定义 -db 标志运行:

$ goatcounter serve \
    -db 'postgresql://user=goatcounter dbname=goatcounter sslmode=disable'

有关连接字符串的更多详细信息,请参见 pq 文档。

如果不使用 SQLite,则可以在不使用 cgo 的情况下编译 goatcounter:

$ CGO_ENABLED=0 go build -ldflags="-X main.version=$(git log -n1 --format='%h_%cI')" ./cmd/goatcounter

从功能上来说并没有太大关系,但是由于不需要 C 编译器,因此构建会变得更加轻松快捷。

开发/测试

您可以使用以下命令启动测试/开发服务器:

$ goatcounter serve -dev

-dev 标志使一些小事情更易于开发;默认情况下,TLS 是禁用的,它将在localhost:8081 上侦听,应用程序将在重新编译和其他一些小的更改时自动重启。

有关如何运行开发服务器,编写补丁程序等的更多详细信息,请参见 .github/CONTRIBUTING.markdown

各种汇总数据文件可在 https://www.goatcounter.com/data 获得

 

(The first version translated by vz on 2020.07.26)
(Second edition: vz revised at 2020.10.10)

Overview

Name With Ownerarp242/goatcounter
Primary LanguageGo
Program languageGo (Language Count: 6)
PlatformDocker, Linux
License:Other
Release Count28
Last Release Namev2.5.0 (Posted on )
First Release Namev1.0.0 (Posted on 2020-01-13 14:52:13)
Created At2019-05-28 06:31:26
Pushed At2024-04-23 19:15:39
Last Commit At2024-04-23 20:09:12
Stargazers Count4.2k
Watchers Count31
Fork Count167
Commits Count1.9k
Has Issues Enabled
Issues Count484
Issue Open Count43
Pull Requests Count205
Pull Requests Open Count1
Pull Requests Close Count42
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

GoatCounter is an open source web analytics platform available as a (free)
hosted service or self-hosted app. It aims to offer easy to use and meaningful
privacy-friendly web analytics as an alternative to Google Analytics or Matomo.

There are two ways to run this: as hosted service on goatcounter.com, or
run it on your own server. The source code is completely Open Source/Free
Software, and it can be self-hosted without restrictions.

See docs/rationale.markdown for some more details on
the "why?" of this project.

There's a live demo at https://stats.arp242.net.

Please consider contributing financially if you're using
goatcounter.com to pay for the server costs.

Features

  • Privacy-aware; doesn’t track users with unique identifiers and doesn't
    need a GDPR notice. Fine-grained control over which data is collected.
    Also see the privacy policy and GDPR consent notices.

  • Lightweight and fast; adds just ~3.5K of extra data to your site. Also
    has JavaScript-free "tracking pixel" option, or you can use it from your
    application's middleware or import from logfiles.

  • Identify unique visits without cookies using a non-identifiable hash
    (technical details).

  • Keeps useful statistics such as browser information, location, and
    screen size. Keep track of referring sites and campaigns.

  • Easy; if you've been confused by the myriad of options and flexibility of
    Google Analytics and Matomo that you don't need then GoatCounter will be a
    breath of fresh air.

  • Accessibility is a high-priority feature, and the interface works well
    with assistive technology such as screen readers.

  • 100% committed to open source; you can see exactly what the code does and
    make improvements, or self-host it for any purpose.

  • Own your data; you can always export all data and cancel at any time.

  • Integrate on your site with just a single script tag:

    <script data-goatcounter="https://yoursite.goatcounter.com/count"
            async src="//gc.zgo.at/count.js"></script>
    
  • The JavaScript integration is a good option for most, but you can also use a
    no-JavaScript image-based tracker, integrate it in your backend
    middleware
    , or parse log files.

Getting data in to GoatCounter

There are three ways:

  1. Add the JavaScript code on your site; this is the easiest and most common
    method. Detailed documentation for this is available at
    https://www.goatcounter.com/code

  2. Integrate in your middleware; send data to GoatCounter by calling the API
    from your backend server middleware. Detailed documentation for this is
    available at https://www.goatcounter.com/api#backend-integration

  3. Parse logfiles. GoatCounter can parse logfiles from nginx, Apache,
    CloudFront, or any other HTTP middleware or proxy. See goatcounter help import for detailed documentation on this.

Running your own

Note this README is for the latest master and may be inaccurate for the latest
released version; use the release-2.4 branch for the 2.4 README.

The release page has binaries for Linux amd64, arm, and arm64. These
are statically compiled, contain everything you need, and should work in pretty
much any Linux environment. The only other thing you need is somewhere to store
a SQLite database file or a PostgreSQL connection.

GoatCounter should run on any platform supported by Go, but there are no
binaries for them (yet) as cross-compiling SQLite is somewhat complex. You'll
have to build from source if you want to run it on e.g. FreeBSD or macOS.

Generally speaking only the latest release is supported, although critical fixes
(security, data loss, etc.) may get backported to previous releases.

Deploy scripts and such

Building from source

You need Go 1.19 or newer and a C compiler (for SQLite). If you compile it with
CGO_ENABLED=0 you don't need a C compiler but can only use PostgreSQL.

You can install from source to $GOBIN (go env GOBIN) with:

% go install zgo.at/goatcounter/v2@latest

Or from a git checkout, also setting the version string:

% go build -ldflags="-X zgo.at/goatcounter/v2.Version=$(git log -n1 --format='%h_%cI')" ./cmd/goatcounter

Which will produce a goatcounter binary in the current directory.

The -ldflags=[..] sets the version; this isn't strictly required as such,
but it's recommended as it's used to "bust" the cache for static files and may
also be useful later when reporting bugs. This can be any string and doesn't
follow any particular format, you can also set this to the current date or
banana or anything you want really.

To build a fully statically linked binary:

% go build -tags osusergo,netgo,sqlite_omit_load_extension \
    -ldflags="-X zgo.at/goatcounter/v2.Version=$(git log -n1 --format='%h_%cI') -extldflags=-static" \
    ./cmd/goatcounter

It's recommended to use the latest release as in the above command. The master
branch should be reasonably stable but no guarantees, and sometimes I don't
write detailed release/upgrade notes until the actual release so you may run in
to surprises.

You can compile goatcounter without cgo if you're planning to use PostgreSQL and
don't use SQLite:

% CGO_ENABLED=0 go build \
    -ldflags="-X zgo.at/goatcounter.Version=$(git log -n1 --format='%h_%cI')" \
    ./cmd/goatcounter

Functionally it doesn't matter too much, but builds will be a bit easier and
faster as it won't require a C compiler.

Running

You can start a server with:

% goatcounter serve

The default is to use an SQLite database at ./db/goatcounter.sqlite3, which
will be created if it doesn't exist yet. See the -db flag and goatcounter help db to customize this.

Both SQLite and PostgreSQL are supported. SQLite should work well for most
smaller sites, but PostgreSQL gives better performance. There are some
benchmarks over here
to give some indication of what performance to
expect from SQLite and PostgreSQL.

GoatCounter will listen on port *:80 and *:443 by default. You don't need
to run it as root and can grant the appropriate permissions on Linux with:

% setcap 'cap_net_bind_service=+ep' goatcounter

Listening on a different port can be a bit tricky due to the ACME/Let's Encrypt
certificate generation; goatcounter help listen documents this in depth.

You can create new sites with the db create site command:

% goatcounter db create site -vhost stats.example.com -user.email me@example.com

This will ask for a password for your new account; you can also add a password
on the commandline with -password. You must also pass the -db flag here if
you use something other than the default.

Updating

You may need to run the database migrations when updating. Use goatcounter serve -automigrate to always run all pending migrations on startup. This is the
easiest way, although arguably not the "best" way.

Use goatcounter migrate <file> or goatcounter migrate all to manually run
migrations; generally you want to upload the new version, run migrations while
the old one is still running, and then restart so the new version takes effect.

Use goatcounter migrate pending to get a list of pending migrations, or
goatcounter migrate list to show all migrations.

PostgreSQL

To use PostgreSQL run GoatCounter with a custom -db flag; for example:

% goatcounter serve -db 'postgresql+dbname=goatcounter'
% goatcounter serve -db 'postgresql+host=/run/postgresql dbname=goatcounter sslmode=disable'

This follows the format in the psql CLI; you can also use the PG*
environment variables:

% PGDATABASE=goatcounter DBHOST=/run/postgresql goatcounter serve -db 'postgresql'

The database will be created automatically if possible; if you want to create it
for a specific user you can use:

% createuser --interactive --pwprompt goatcounter
% createdb --owner goatcounter goatcounter

You can manually import the schema with:

% goatcounter db schema-pgsql | psql --user=goatcounter --dbname=goatcounter

See goatcounter help db and the pq docs for more details.

Development/testing

You can start a test/development server with:

% goatcounter serve -dev

The -dev flag makes some small things a bit more convenient for development;
TLS is disabled by default, it will listen on localhost:8081, the application
will automatically restart on recompiles, templates and static files will be
read directly from the filesystem, and a few other minor changes.

See .github/CONTRIBUTING.markdown for more
details on how to run a development server, write patches, etc.

To the top