From 431a0b45c5110d5f0251a5a1f6dd38d81d17f42f Mon Sep 17 00:00:00 2001 From: Dan Grebb Date: Sat, 6 Jan 2024 20:57:47 -0500 Subject: [PATCH 1/2] feat: add custom pageview property support - adds `pageview-props` script src segment - adds various types and guards for Plausible-enforced property limits - defends against `svelte-preprocess` [issue with shorthand `{src}` attribute](sveltejs/svelte-preprocess#604) (#12) - updates README - updates configuration/npm scripts for prettier@3 --- .prettierrc | 1 - README.md | 38 ++++++++++-- package.json | 4 +- src/lib/PlausibleAnalytics.svelte | 76 +++++++++++++++++++++++- src/lib/guards.ts | 96 +++++++++++++++++++++++++++++++ 5 files changed, 206 insertions(+), 9 deletions(-) create mode 100644 src/lib/guards.ts diff --git a/.prettierrc b/.prettierrc index a77fdde..9573023 100644 --- a/.prettierrc +++ b/.prettierrc @@ -4,6 +4,5 @@ "trailingComma": "none", "printWidth": 100, "plugins": ["prettier-plugin-svelte"], - "pluginSearchDirs": ["."], "overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }] } diff --git a/README.md b/README.md index 86a1017..bbd479c 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ npm i --save-dev @accuser/svelte-plausible-analytics ## Examples -Add Plausible Analytics to the root layout to track page views. +### Add Plausible Analytics to the root layout to track page views. ```svelte + + + + +``` + +Set custom properties in the `pageviewProps` Svelte prop on the `` component. *Beware hydration race conditions and take note when `` is mounted. Especially with SSG.* + +The Plausible-required `event-` prefix can be omitted. Eg. `` becomes ``. + +*Note*: as per the [Plausible documentation](https://plausible.io/docs/custom-props/introduction#limits), up to 30 custom properties can be included alongside a pageview by adding multiple attributes. There is also a 300/2000 character limit on each property `key` and `value`, respectively. + +### Track analytics events: ```svelte +