From b15bfc0a73253e8e1ff29a036ea02cef6bae3b31 Mon Sep 17 00:00:00 2001 From: nachtjasmin Date: Thu, 8 Sep 2022 15:48:59 +0200 Subject: [PATCH] feat: add goatcounter integration To enable the goatcounter integration, the variable VITE_QUEER_LEXIKON_PRIVATE must be set to "true". This check is intentionally added for development environments so that all dev environments from possible forks do not modify the statistics by accident. --- .env | 2 ++ main.ts | 5 +++++ ts/env.d.ts | 12 ++++++++++++ ts/goatcounter.ts | 22 ++++++++++++++++++++++ tsconfig.json | 2 +- 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .env create mode 100644 ts/env.d.ts create mode 100644 ts/goatcounter.ts diff --git a/.env b/.env new file mode 100644 index 0000000..3f2946c --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +VITE_QUEER_LEXIKON_PRIVATE=false + diff --git a/main.ts b/main.ts index 9839d18..206f5f5 100644 --- a/main.ts +++ b/main.ts @@ -5,6 +5,7 @@ import { initializeMap } from "./ts/map"; import "@fontsource/ubuntu/latin.css"; import { setupLegend } from "./ts/legend"; import { setupEmergencyButton } from "./ts/emergency-button"; +import { addGoatCounter } from "./ts/goatcounter"; document.addEventListener("DOMContentLoaded", () => { initializeMap(); @@ -12,4 +13,8 @@ document.addEventListener("DOMContentLoaded", () => { setupHamburger(); setupLegend(); setupEmergencyButton(); + + if (import.meta.env.VITE_QUEER_LEXIKON_PRIVATE === "true") { + addGoatCounter(); + } }); diff --git a/ts/env.d.ts b/ts/env.d.ts new file mode 100644 index 0000000..72cecca --- /dev/null +++ b/ts/env.d.ts @@ -0,0 +1,12 @@ +interface ImportMeta { + readonly env: ImportMetaEnv; +} +interface ImportMetaEnv { + // This variable is set in our GitHub Actions for adding additional features + // that should be only available on karte.queer-lexikon.net and not + // for development purposes. + // + // Although it's actually a boolean, all environment variables are strings, so we + // type it as string here as well. + readonly VITE_QUEER_LEXIKON_PRIVATE?: string; +} diff --git a/ts/goatcounter.ts b/ts/goatcounter.ts new file mode 100644 index 0000000..85a8d92 --- /dev/null +++ b/ts/goatcounter.ts @@ -0,0 +1,22 @@ +declare global { + interface Window { + goatcounter: { + path: (p: any) => string; + }; + } +} + +export const addGoatCounter = () => { + window.goatcounter = { + path: function (p: any) { + return location.host + p; + }, + }; + + let goatcounter = document.createElement("script"); + goatcounter.src = "//ziege.queer-lexikon.net/count.js"; + goatcounter.async = true; + goatcounter.setAttribute("data-goatcounter", "https://ziege.queer-lexikon.net/count"); + + document.head.appendChild(goatcounter); +}; diff --git a/tsconfig.json b/tsconfig.json index 583a677..b8ab2a2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "exclude": ["/dist"], "compilerOptions": { - "module": "ES2015", + "module": "ES2020", "strict": true, "lib": ["DOM", "ES2015"], "target": "ES2015",