From 74b9fe5c57e2c3340c796146c9a2d959e8ca39e4 Mon Sep 17 00:00:00 2001 From: Brendan Kellam Date: Sat, 28 Sep 2024 16:47:07 -0700 Subject: [PATCH] Add 'install' event that is fired once on first run (#11) --- .env | 6 ++++-- Dockerfile | 5 ++++- entrypoint.sh | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/.env b/.env index 8bcb9623..58940ffa 100644 --- a/.env +++ b/.env @@ -1,4 +1,6 @@ -NEXT_PUBLIC_POSTHOG_KEY=phc_VFn4CkEGHRdlVyOOw8mfkoj1DKVoG6y1007EClvzAnS NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com NEXT_PUBLIC_POSTHOG_ASSET_HOST=https://us-assets.i.posthog.com -NEXT_PUBLIC_POSTHOG_UI_HOST=https://us.posthog.com \ No newline at end of file +NEXT_PUBLIC_POSTHOG_UI_HOST=https://us.posthog.com + +# @note: This is also set in the Dockerfile. +NEXT_PUBLIC_POSTHOG_KEY=phc_VFn4CkEGHRdlVyOOw8mfkoj1DKVoG6y1007EClvzAnS \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 71bd631d..7fbe84a6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,11 +35,14 @@ ENV DATA_DIR=/data ENV CONFIG_PATH=$DATA_DIR/config.json ENV DATA_CACHE_DIR=$DATA_DIR/.sourcebot +# @note: This is also set in .env +ENV NEXT_PUBLIC_POSTHOG_KEY=phc_VFn4CkEGHRdlVyOOw8mfkoj1DKVoG6y1007EClvzAnS + # Sourcebot collects anonymous usage data using [PostHog](https://posthog.com/). Uncomment this line to disable. # ENV SOURCEBOT_TELEMETRY_DISABLED=1 # Configure dependencies -RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervisor +RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervisor uuidgen curl # Configure zoekt COPY vendor/zoekt/install-ctags-alpine.sh . diff --git a/entrypoint.sh b/entrypoint.sh index 2c589efa..bb73db23 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,33 @@ #!/bin/sh set -e +# Issue a info message about telemetry +if [ ! -z "$SOURCEBOT_TELEMETRY_DISABLED" ]; then + echo -e "\e[34m[Info] Disabling telemetry since SOURCEBOT_TELEMETRY_DISABLED was set.\e[0m" +fi + +# Check if DATA_CACHE_DIR exists, if not create it +if [ ! -d "$DATA_CACHE_DIR" ]; then + mkdir -p "$DATA_CACHE_DIR" +fi + +# In order to detect if this is the first run, we create a `.installed` file in +# the cache directory. +FIRST_RUN_FILE="$DATA_CACHE_DIR/.installed" +if [ ! -f "$FIRST_RUN_FILE" ]; then + touch "$FIRST_RUN_FILE" + + # If this is our first run, send a `install` event to PostHog + # (if telemetry is enabled) + if [ -z "$SOURCEBOT_TELEMETRY_DISABLED" ]; then + curl -L -s --header "Content-Type: application/json" -d '{ + "api_key": "'"$NEXT_PUBLIC_POSTHOG_KEY"'", + "event": "install", + "distinct_id": "'"$(uuidgen)"'" + }' https://us.i.posthog.com/capture/ > /dev/null + fi +fi + # Fallback to sample config if a config does not exist if [ ! -f "$CONFIG_PATH" ]; then echo -e "\e[33m[Warning] Config file at CONFIG_PATH not found. Falling back on sample config.\e[0m"