Skip to content

Commit da2076b

Browse files
committed
Add path option for plausible-analytics option
1 parent c5edb6f commit da2076b

File tree

7 files changed

+82
-7
lines changed

7 files changed

+82
-7
lines changed

news/changelog-1.9.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ All changes included in 1.9:
3333
### `website`
3434

3535
- Algolia Insights now uses privacy-friendly defaults: `useCookie: false` with random session tokens when cookie consent is not configured. When `cookie-consent: true` is enabled, Algolia scripts are deferred and only use cookies after user grants "tracking" consent, ensuring GDPR compliance.
36-
- Add support for Plausible Analytics via `plausible-analytics` configuration option. Users can paste their Plausible script snippet directly from their Plausible dashboard.
36+
- Add support for Plausible Analytics via `plausible-analytics` configuration option. Users can either paste their Plausible script snippet directly in YAML or provide a path to a file containing the snippet using `plausible-analytics: { path: _plausible_snippet.html }`.
3737

3838
## `publish`
3939

src/project/types/website/website-analytics.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import { Document } from "../../../core/deno-dom.ts";
77
import { join } from "../../../deno_ral/path.ts";
8+
import { existsSync } from "../../../deno_ral/fs.ts";
89
import { kLang, kTitle } from "../../../config/constants.ts";
910
import { Format, Metadata } from "../../../config/types.ts";
1011
import { projectTypeResourcePath } from "../../../core/resources.ts";
@@ -28,6 +29,7 @@ const kVersion = "version";
2829

2930
// Plausible analytics
3031
export const kPlausibleAnalytics = "plausible-analytics";
32+
const kPlausiblePath = "path";
3133

3234
// Cookie consent properties
3335
export const kCookieConsent = "cookie-consent";
@@ -119,8 +121,26 @@ export function websiteAnalyticsScriptFile(
119121

120122
// Plausible Analytics
121123
const plausibleSnippet = siteMeta[kPlausibleAnalytics];
122-
if (plausibleSnippet && typeof plausibleSnippet === "string") {
123-
scripts.push(plausibleSnippet);
124+
if (plausibleSnippet) {
125+
if (typeof plausibleSnippet === "string") {
126+
// Inline snippet provided directly in YAML
127+
scripts.push(plausibleSnippet);
128+
} else if (typeof plausibleSnippet === "object") {
129+
// Path to file containing snippet
130+
const plausibleMeta = plausibleSnippet as Metadata;
131+
const snippetPath = plausibleMeta[kPlausiblePath] as string;
132+
if (snippetPath) {
133+
const absolutePath = join(project.dir, snippetPath);
134+
if (existsSync(absolutePath)) {
135+
const snippetContent = Deno.readTextFileSync(absolutePath);
136+
scripts.push(snippetContent);
137+
} else {
138+
throw new Error(
139+
`Plausible Analytics snippet file not found: ${snippetPath}`,
140+
);
141+
}
142+
}
143+
}
124144
}
125145
}
126146

src/resources/schema/definitions.yml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,20 +752,45 @@
752752
This is automatically detected based upon the `tracking-id`, but you may specify it.
753753
description: "Enable Google Analytics for this website"
754754
plausible-analytics:
755-
schema: string
755+
anyOf:
756+
- string
757+
- object:
758+
closed: true
759+
properties:
760+
path:
761+
path:
762+
description: "Path to a file containing the Plausible Analytics script snippet"
763+
required: [path]
756764
description:
757-
short: "Enable Plausible Analytics for this website by inserting scrips snippets to add in site `<head>`"
765+
short: "Enable Plausible Analytics for this website by providing a script snippet or path to snippet file"
758766
long: |
759-
Enable Plausible Analytics for this website by pasting the script snippet from your Plausible dashboard.
767+
Enable Plausible Analytics for this website by pasting the script snippet from your Plausible dashboard,
768+
or by providing a path to a file containing the snippet.
760769
761770
Plausible is a privacy-friendly, GDPR-compliant web analytics service that does not use cookies and does not require cookie consent.
762771
772+
**Option 1: Inline snippet**
773+
774+
```yaml
775+
website:
776+
plausible-analytics: |
777+
<script async src="https://plausible.io/js/script.js"></script>
778+
```
779+
780+
**Option 2: File path**
781+
782+
```yaml
783+
website:
784+
plausible-analytics:
785+
path: _plausible_snippet.html
786+
```
787+
763788
To get your script snippet:
764789
765790
1. Log into your Plausible account at <https://plausible.io>
766791
2. Go to your site settings
767792
3. Copy the JavaScript snippet provided
768-
4. Paste it here
793+
4. Either paste it directly in your configuration or save it to a file
769794
770795
For more information, see <https://plausible.io/docs/plausible-script>
771796
announcement:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.quarto/
2+
/_site/
3+
4+
**/*.quarto_ipynb
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script async src="https://plausible.io/js/pa-FROMFILE99.js"></script>
2+
<script>
3+
window.plausibleFile=window.plausibleFile||function(){(plausibleFile.q=plausibleFile.q||[]).push(arguments)};
4+
plausibleFile.init()
5+
</script>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
project:
2+
type: website
3+
4+
website:
5+
title: "Plausible Path Test"
6+
plausible-analytics:
7+
path: _plausible_snippet.html
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: "Plausible Path Test"
3+
_quarto:
4+
tests:
5+
html:
6+
ensureHtmlElements:
7+
-
8+
- 'script[async][src="https://plausible.io/js/pa-FROMFILE99.js"]'
9+
ensureFileRegexMatches:
10+
- ['window\.plausibleFile', 'plausibleFile\.init']
11+
---
12+
13+
This test verifies that Plausible Analytics with a path to an external file
14+
loads the snippet correctly from the file.

0 commit comments

Comments
 (0)