Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
RFC + POC: Add Plausible for analytics (#804)
Dhruv Bhanushalied85afe1ad7b
diff --git a/clickhouse/clickhouse-config.xml b/clickhouse/clickhouse-config.xmlnew file mode 100644index 000000000..d82e453b5--- /dev/null+++ b/clickhouse/clickhouse-config.xml@@ -0,0 +1,16 @@+<clickhouse>+ <logger>+ <level>warning</level>+ <console>true</console>+ </logger>++ <!-- Stop all the unnecessary logging -->+ <query_thread_log remove="remove"/>+ <query_log remove="remove"/>+ <text_log remove="remove"/>+ <trace_log remove="remove"/>+ <metric_log remove="remove"/>+ <asynchronous_metric_log remove="remove"/>+ <session_log remove="remove"/>+ <part_log remove="remove"/>+</clickhouse>diff --git a/clickhouse/clickhouse-user-config.xml b/clickhouse/clickhouse-user-config.xmlnew file mode 100644index 000000000..9a9a1abf1--- /dev/null+++ b/clickhouse/clickhouse-user-config.xml@@ -0,0 +1,8 @@+<clickhouse>+ <profiles>+ <default>+ <log_queries>0</log_queries>+ <log_query_threads>0</log_query_threads>+ </default>+ </profiles>+</clickhouse>diff --git a/docker-compose.yml b/docker-compose.ymlindex 1004e4810..81655b787 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -4,12 +4,12 @@ services:image: postgres:13.2-alpineports:- "50254:5432"+ volumes:+ - api-postgres:/var/lib/postgresql/dataenv_file:- postgres/env.dockerhealthcheck:test: "pg_isready -U deploy -d openledger"- volumes:- - api-postgres:/var/lib/postgresql/dataupstream_db:image: postgres:13.2-alpine@@ -23,6 +23,39 @@ services:healthcheck:test: "pg_isready -U deploy -d openledger"+ plausible_db:+ image: postgres:13.2-alpine+ expose:+ - "5432"+ volumes:+ - plausible-postgres:/var/lib/postgresql/data+ env_file:+ - postgres/plausible.env.docker+ healthcheck:+ test: "pg_isready -U deploy -d plausible"++ plausible_ch:+ image: clickhouse/clickhouse-server:22.6-alpine+ volumes:+ - plausible-clickhouse:/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:latest+ ports:+ - "50288:8000"+ command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh run"+ depends_on:+ - plausible_db+ - plausible_ch+ env_file:+ - plausible/env.docker+cache:image: redis:4.0.10ports:@@ -134,4 +167,6 @@ services:volumes:api-postgres:catalog-postgres:+ plausible-postgres:+ plausible-clickhouse:es-data:diff --git a/justfile b/justfileindex bbefa3ee1..b7a57e3a7 100644--- a/justfile+++ b/justfile@@ -99,6 +99,7 @@ env:# Load sample data into the Docker Compose servicesinit: _all-up./load_sample_data.sh+ ./setup_plausible.sh########### Docker #diff --git a/plausible/env.docker b/plausible/env.dockernew file mode 100644index 000000000..0c22b92ca--- /dev/null+++ b/plausible/env.docker@@ -0,0 +1,4 @@+BASE_URL=http://localhost:50288+SECRET_KEY_BASE=MN00EYOjQegVd5Z2NkMoSk3o1cklN/37YINtjYtM3yxsoXUhRNnvgWu3pgnEibKR126HmlGeHu7KHpPaYj/Awg==+DATABASE_URL=postgres://deploy:deploy@plausible_db:5432/plausible+CLICKHOUSE_DATABASE_URL=http://plausible_ch:8123/plausible_chdiff --git a/postgres/plausible.env.docker b/postgres/plausible.env.dockernew file mode 100644index 000000000..2520ff225--- /dev/null+++ b/postgres/plausible.env.docker@@ -0,0 +1,4 @@+POSTGRES_DB="plausible"+POSTGRES_USER="deploy"+POSTGRES_PASSWORD="deploy"+POSTGRES_HOST="0.0.0.0"diff --git a/rfcs/analytics/20230307-backend_service.md b/rfcs/analytics/20230307-backend_service.mdnew file mode 100644index 000000000..790357839--- /dev/null+++ b/rfcs/analytics/20230307-backend_service.md@@ -0,0 +1,111 @@+# RFC: Analytics backend and visualisation service++This RFC deals with the backend of analytics, about how we record events sent+from the frontend and what we do with those events.++## Reviewers++- [ ] TBD+- [ ] TBD++## Rationale++As discussed in the [previous RFC](20221006-implementation_plan_frontend.md),+the frontend will emit analytic events for a number of interactions such as+clicking on buttons and navigating between pages. These events will be sent to+some endpoint where they will be recorded and then this recorded data can be+used to make decisions, draw visualisations etc.++There are numerous approaches to how the backend could be built, but they fall+primarily into two categories.++- Bespoke in-house implementation+- Off-the-shelf analytics framework++Considering the size of the team, needs of the project and volume of other+projects in the roadmap, an off-the-shelf solution makes more sense compared to+building something, even if rudimentary, from scratch. Such solutions are+numerous, so we must apply the following criteria to narrow down our list.++- Open source+- GDPR compliant+- Privacy respecting+- Simple (subjective)++This leaves us with only two main contenders, [Plausible](https://plausible.io/)+and [Umami](https://umami.is/). They're basically identical (just look at their+landing pages!) but have a few small differences.++| Criteria | Umami | Plausible |+| ----------------- | --------------- | ---------------------- |+| URL | [umami.is][1] | [plausible.io][2] |+| UI | Clean | Little dated |+| Language | JS | Elixir |+| License | MIT | AGPL |+| GH stars | 14.8k | 14.1k |+| Integration | [nuxt-umami][3] | [vue-plausible][4] |+| Integration stars | 41 | 112 |+| npm package | No | [plausible-tracker][5] |+| Cloud | [Beta][6] | [GA][7] |++[1]: https://umami.is/+[2]: https://plausible.io/+[3]: https://github.com/ijkml/nuxt-umami/tree/main+[4]: https://github.com/moritzsternemann/vue-plausible+[5]: https://github.com/plausible/plausible-tracker+[6]: https://cloud.umami.is/login+[7]: https://plausible.io/login++Practically there is no reason to prefer one over the other, and it is very easy+for us to switch between them, if needed, thanks to our composable abstracting+the framework.++Considering this, we can go ahead with Plausible because++- Plausible has an NPM package that can be bundled with the app (whereas Umami+ needs a `<script>` tag).+- Plausible has a generally available cloud platform (whereas Umami's is in+ beta).+- there was some talk of it being used in other WordPress endeavours, so it+ makes more sense to use it.+- I've already done some experimental work with it, and it might save us the+ effort if that existing work were to be used.++## Production deployment++To start with we will be using the managed cloud instance of Plausible. This+will help us get up and running with analytics super quickly without having to+provision all the following services:++- a PostgreSQL database+- a Clickhouse database+- a Plausible instance++If we move to our own instance sometime in the future, we can+[export data](https://plausible.io/docs/export-stats) and migrate that to our+instance.++## Pricing++[Plausible pricing](https://plausible.io/#pricing) is based on number of events+/ month. Each page view and each custom event recorded counts towards the total+quota. The annual plan is 10x the monthly price i.e. 2 months free.++There is no cap on:++- data retention, so we can export it much even after lots of usage.+- number of users, so the entire team can be onboarded to see the dashboards.++Considering we have ~15 pages and ~20 custom event types, for a person that+looks through the entire site, we can estimate ~35 API hits. It can get pricey+quite fast. Even if we go with the cloud solution to get up and running quickly,+switching to self-hosted seems like the more economical option in the long term.++> We're operating a sustainable project funded solely by the fees that our+> subscribers pay us. And we donate 5% of our revenue.+>+> \- Plausible++On the other hand, if we're being generous, this can be thought of as investment+towards an open-source project that has doesn't have any other source of+funding.diff --git a/setup_plausible.sh b/setup_plausible.shnew file mode 100755index 000000000..fe7d193d7--- /dev/null+++ b/setup_plausible.sh@@ -0,0 +1,27 @@+#!/bin/bash+set -e+PLAUSIBLE_SERVICE_NAME="${PLAUSIBLE_SERVICE_NAME:-plausible}"+PLAUSIBLE_DB_SERVICE_NAME="${PLAUSIBLE_DB_SERVICE_NAME:-plausible_db}"++# Create Plausible user+docker-compose exec -T "$PLAUSIBLE_SERVICE_NAME" \+ /app/bin/plausible rpc \+ "Plausible.Auth.User.new(%{name: \"Deploy\", email: \"[email protected]\", password: \"deploy\", password_confirmation: \"deploy\"}) |> Plausible.Repo.insert"++# Create an API key with '{sites:provision:*}' scope+# API key: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa (that's 64 times 'a')+# Key hash: SHA256(`SECRET_KEY_BASE` + API key)+docker-compose exec -T "$PLAUSIBLE_DB_SERVICE_NAME" /bin/bash -c "psql -U deploy -d plausible <<-EOF+ INSERT INTO api_keys+ (id, user_id, name, key_prefix, key_hash, inserted_at, updated_at, scopes, hourly_request_limit)+ VALUES+ (1, 1, 'Development', 'aaaaaa', '332015ffc9f0e1f475c0fadb1e1a14d2eb09774249f71961f5a2c477efc0a0fc', now(), now(), '{sites:provision:*}', 1000)+ EOF"++# Create site using API key+curl \+ -X POST \+ -H "Authorization: Bearer aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \+ -F 'domain="localhost"' \+ -F 'timezone="UTC"' \+ http://localhost:50288/api/v1/sites