Skip to content

Commit

Permalink
Plausible analytics (#2226)
Browse files Browse the repository at this point in the history
Closes #2222 

Adds a runtime script that gets set to either inject the plausible
script tags, or do nothing, that runs at initialization of the frontend
container.
  • Loading branch information
emma-sg authored Dec 11, 2024
1 parent 541298e commit a65ca49
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions chart/templates/frontend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ spec:
value: "{{ .Values.minio_local_bucket_name }}"
{{- end }}

{{- if .Values.inject_analytics }}
- name: INJECT_ANALYTICS
value: {{ .Values.inject_analytics }}
{{- end }}

resources:
limits:
memory: {{ .Values.frontend_memory }}
Expand Down
3 changes: 3 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ ingress:

ingress_class: nginx

# Optional: Analytics injection script
# This runs as a blocking script on the frontend, so usually you'll want to have it just add a single script tag to the page with the `defer` attribute.
# inject_analytics: // your analytics injection script here


# Signing Options
Expand Down
9 changes: 9 additions & 0 deletions frontend/00-browsertrix-nginx-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ else
sed -i "s/\$LOCAL_BUCKET/$LOCAL_BUCKET/g" /etc/nginx/includes/minio.conf
fi

# Plausible analytics
if [ -z "$INJECT_ANALYTICS" ]; then
echo "analytics disabled, injecting blank script"
echo "" > /usr/share/nginx/html/_plausible.js
else
echo "analytics enabled, injecting script"
echo "$INJECT_ANALYTICS" > /usr/share/nginx/html/_plausible.js
fi

mkdir -p /etc/nginx/resolvers/
echo resolver $(grep -oP '(?<=nameserver\s)[^\s]+' /etc/resolv.conf | awk '{ if ($1 ~ /:/) { printf "[" $1 "] "; } else { printf $1 " "; } }') valid=10s ipv6=off";" > /etc/nginx/resolvers/resolvers.conf

Expand Down
16 changes: 15 additions & 1 deletion frontend/docs/docs/deploy/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ storages:
endpoint_url: "http://local-minio.default:9000/"
is_default_replica: True
- name: "replica-1"
type: "s3"
access_key: "accesskey"
Expand Down Expand Up @@ -150,3 +150,17 @@ Browsertrix has the ability to cryptographically sign WACZ files with [Authsign]
## Enable Open Registration

You can enable sign-ups by setting `registration_enabled` to `"1"`. Once enabled, your users can register by visiting `/sign-up`.

## Analytics

You can add a script to inject any sort of analytics into the frontend by setting `inject_analytics` to the script. If present, it will be injected as a blocking script tag into every page — so we recommend you create the script tags that handle your analytics from within this script.

For example, here's a script that adds Plausible Analytics tracking:

```ts
const plausible = document.createElement("script");
plausible.src = "https://plausible.io/js/script.js";
plausible.defer = true;
plausible.dataset.domain = "app.browsertrix.com";
document.head.appendChild(plausible);
```
1 change: 1 addition & 0 deletions frontend/src/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<link rel="manifest" href="/manifest.webmanifest" />
<script src="/_plausible.js"></script>
</head>
<body>
<script>
Expand Down

0 comments on commit a65ca49

Please sign in to comment.