add plausible support
All checks were successful
Code quality checks / biome (push) Successful in 8s

This commit is contained in:
creations 2025-05-03 07:07:26 -04:00
parent dbe894a568
commit 167d989600
Signed by: creations
GPG key ID: 8F553AA4320FC711
4 changed files with 38 additions and 6 deletions

View file

@ -14,3 +14,6 @@ BADGE_API_URL=http://localhost:8081
# https://www.steamgriddb.com/api/v2, if you want games to have images
STEAMGRIDDB_API_KEY=steamgrid_api_key
# https://plausible.io
PLAUSIBLE_SCRIPT_HTML=''

View file

@ -29,7 +29,7 @@ https://git.creations.works/creations/badgeAPI
### 4. SteamGridDB
>You only have to use this if you want to fetch game icons that Discord doesnt provide:
>Only needed if you want to fetch game icons that Discord doesnt provide:
https://www.steamgriddb.com/api/v2
---
@ -87,6 +87,28 @@ bun run start
---
## Optional: Analytics with Plausible
You can enable [Plausible Analytics](https://plausible.io) tracking by setting a script snippet in your environment.
### `.env` Variable
| Variable | Description |
|-------------------------|------------------------------------------------------------------------|
| `PLAUSIBLE_SCRIPT_HTML` | Full `<script>` tag(s) to inject into the `<head>` for analytics |
#### Example
```env
PLAUSIBLE_SCRIPT_HTML='<script defer data-domain="example.com" src="https://plausible.example.com/js/script.js"></script><script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>'
```
- The script will only be injected if this variable is set.
- Plausible provides the correct script when you add a domain.
- Be sure to wrap it in single quotes (`'`) so it works in `.env`.
---
## Docker Support
### Build & Start with Docker Compose
@ -103,10 +125,10 @@ Make sure the `.env` file is configured correctly before starting the container.
These are the main public routes exposed by the server:
| Route | Description |
|---------------|-----------------------------------------------------------------------------|
| `/` | Loads the profile page for the default Discord user defined in `.env` (`LANYARD_USER_ID`) |
| `/[id]` | Loads the profile page for a specific Discord user ID passed in the URL |
| Route | Description |
|---------|-----------------------------------------------------------------------------|
| `/` | Loads the profile page for the default Discord user defined in `.env` (`LANYARD_USER_ID`) |
| `/[id]` | Loads the profile page for a specific Discord user ID passed in the URL |
> Example: `https://creations.works/209830981060788225` shows the profile of that specific user.

View file

@ -17,3 +17,6 @@ export const lanyardConfig: LanyardConfig = {
export const badgeApi: string | null = process.env.BADGE_API_URL || null;
export const steamGridDbKey: string | undefined =
process.env.STEAMGRIDDB_API_KEY;
export const plausibleScript: string | null =
process.env.PLAUSIBLE_SCRIPT_HTML?.trim() || null;

View file

@ -1,5 +1,5 @@
import { resolve } from "node:path";
import { badgeApi, lanyardConfig } from "@config/environment";
import { badgeApi, lanyardConfig, plausibleScript } from "@config/environment";
import { file } from "bun";
const routeDef: RouteDef = {
@ -23,6 +23,10 @@ async function handler(request: ExtendedRequest): Promise<Response> {
head.setAttribute("data-user-id", id || lanyardConfig.userId);
head.setAttribute("data-instance-uri", instance);
head.setAttribute("data-badge-url", badgeApi || "");
if (plausibleScript) {
head.append(plausibleScript, { html: true });
}
},
})
.transform(await bunFile.text());