From 68f53352e715861b155bd11baffe9f6e3032ff1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliv=C3=A9r=20Falvai?= Date: Thu, 28 Dec 2023 17:49:35 +0100 Subject: [PATCH] feat: Self-hosted Plausible support (#656) * Self-hosted Plausible support * Remove leftover import --- package-lock.json | 9 --------- package.json | 1 - quartz/cfg.ts | 1 + quartz/components/scripts/plausible.inline.ts | 3 --- quartz/plugins/emitters/componentResources.ts | 17 ++++++++++++++--- 5 files changed, 15 insertions(+), 16 deletions(-) delete mode 100644 quartz/components/scripts/plausible.inline.ts diff --git a/package-lock.json b/package-lock.json index dd88ff7..ecf6a2a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,7 +32,6 @@ "mdast-util-to-hast": "^13.0.2", "mdast-util-to-string": "^4.0.0", "micromorph": "^0.4.5", - "plausible-tracker": "^0.3.8", "preact": "^10.19.3", "preact-render-to-string": "^6.3.1", "pretty-bytes": "^6.1.1", @@ -4451,14 +4450,6 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, - "node_modules/plausible-tracker": { - "version": "0.3.8", - "resolved": "https://registry.npmjs.org/plausible-tracker/-/plausible-tracker-0.3.8.tgz", - "integrity": "sha512-lmOWYQ7s9KOUJ1R+YTOR3HrjdbxIS2Z4de0P/Jx2dQPteznJl2eX3tXxKClpvbfyGP59B5bbhW8ftN59HbbFSg==", - "engines": { - "node": ">=10" - } - }, "node_modules/preact": { "version": "10.19.3", "resolved": "https://registry.npmjs.org/preact/-/preact-10.19.3.tgz", diff --git a/package.json b/package.json index 70df833..b99471d 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,6 @@ "mdast-util-to-hast": "^13.0.2", "mdast-util-to-string": "^4.0.0", "micromorph": "^0.4.5", - "plausible-tracker": "^0.3.8", "preact": "^10.19.3", "preact-render-to-string": "^6.3.1", "pretty-bytes": "^6.1.1", diff --git a/quartz/cfg.ts b/quartz/cfg.ts index 8371b5e..7f0f206 100644 --- a/quartz/cfg.ts +++ b/quartz/cfg.ts @@ -7,6 +7,7 @@ export type Analytics = | null | { provider: "plausible" + host?: string } | { provider: "google" diff --git a/quartz/components/scripts/plausible.inline.ts b/quartz/components/scripts/plausible.inline.ts deleted file mode 100644 index 704f5d5..0000000 --- a/quartz/components/scripts/plausible.inline.ts +++ /dev/null @@ -1,3 +0,0 @@ -import Plausible from "plausible-tracker" -const { trackPageview } = Plausible() -document.addEventListener("nav", () => trackPageview()) diff --git a/quartz/plugins/emitters/componentResources.ts b/quartz/plugins/emitters/componentResources.ts index b51d091..e8a81bc 100644 --- a/quartz/plugins/emitters/componentResources.ts +++ b/quartz/plugins/emitters/componentResources.ts @@ -4,8 +4,6 @@ import { QuartzEmitterPlugin } from "../types" // @ts-ignore import spaRouterScript from "../../components/scripts/spa.inline" // @ts-ignore -import plausibleScript from "../../components/scripts/plausible.inline" -// @ts-ignore import popoverScript from "../../components/scripts/popover.inline" import styles from "../../styles/custom.scss" import popoverStyle from "../../components/styles/popover.scss" @@ -103,7 +101,20 @@ function addGlobalPageResources( }); });`) } else if (cfg.analytics?.provider === "plausible") { - componentResources.afterDOMLoaded.push(plausibleScript) + const plausibleHost = cfg.analytics.host ?? "https://plausible.io" + componentResources.afterDOMLoaded.push(` + const plausibleScript = document.createElement("script") + plausibleScript.src = "${plausibleHost}/js/script.manual.js" + plausibleScript.setAttribute("data-domain", location.hostname) + plausibleScript.defer = true + document.head.appendChild(plausibleScript) + + window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) } + + document.addEventListener("nav", () => { + plausible("pageview") + }) + `) } else if (cfg.analytics?.provider === "umami") { componentResources.afterDOMLoaded.push(` const umamiScript = document.createElement("script")