Skip to content

Commit 4d70b70

Browse files
committed
Add support for setting custom script to activate plausible
1 parent 56eb16a commit 4d70b70

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const kStorage = "storage";
2626
const kAnonymizeIp = "anonymize-ip";
2727
const kVersion = "version";
2828

29+
// Plausible analytics
30+
export const kPlausibleAnalytics = "plausible-analytics";
31+
2932
// Cookie consent properties
3033
export const kCookieConsent = "cookie-consent";
3134
const kCookieConsentType = "type";
@@ -77,19 +80,17 @@ ${contents}
7780
}
7881
}
7982

80-
// Generate the script to inject into the head for Google Analytics
83+
// Generate the script to inject into the head for Google Analytics and/or Plausible
8184
export function websiteAnalyticsScriptFile(
8285
project: ProjectContext,
8386
temp: TempContext,
8487
) {
85-
// Find the ga tag
8688
const siteMeta = project.config?.[kWebsite] as Metadata;
87-
88-
// The google analytics metadata (either from the page or the site)
89-
// Deal with page and site options
90-
let gaConfig: GaConfiguration | undefined = undefined;
89+
const scripts: string[] = [];
9190

9291
if (siteMeta) {
92+
// Google Analytics
93+
let gaConfig: GaConfiguration | undefined = undefined;
9394
const siteGa = siteMeta[kGoogleAnalytics];
9495
if (typeof (siteGa) === "object") {
9596
const siteGaMeta = siteGa as Metadata;
@@ -108,16 +109,24 @@ export function websiteAnalyticsScriptFile(
108109
} else if (siteGa && typeof (siteGa) === "string") {
109110
gaConfig = googleAnalyticsConfig(project, siteGa as string);
110111
}
111-
}
112112

113-
// Generate the actual GA dependencies
114-
if (gaConfig) {
115-
const script = analyticsScript(gaConfig);
116-
if (script) {
117-
return scriptFile(script, temp);
118-
} else {
119-
return undefined;
113+
if (gaConfig) {
114+
const gaScript = analyticsScript(gaConfig);
115+
if (gaScript) {
116+
scripts.push(gaScript);
117+
}
118+
}
119+
120+
// Plausible Analytics
121+
const plausibleSnippet = siteMeta[kPlausibleAnalytics];
122+
if (plausibleSnippet && typeof (plausibleSnippet) === "string") {
123+
scripts.push(plausibleSnippet);
120124
}
125+
}
126+
127+
// Return combined script file if we have any analytics
128+
if (scripts.length > 0) {
129+
return scriptFile(scripts.join("\n"), temp);
121130
} else {
122131
return undefined;
123132
}

src/resources/schema/definitions.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,23 @@
751751
752752
This is automatically detected based upon the `tracking-id`, but you may specify it.
753753
description: "Enable Google Analytics for this website"
754+
plausible-analytics:
755+
schema: string
756+
description:
757+
short: "Enable Plausible Analytics for this website by inserting scrips snippets to add in site `<head>`"
758+
long: |
759+
Enable Plausible Analytics for this website by pasting the script snippet from your Plausible dashboard.
760+
761+
Plausible is a privacy-friendly, GDPR-compliant web analytics service that does not use cookies and does not require cookie consent.
762+
763+
To get your script snippet:
764+
765+
1. Log into your Plausible account at <https://plausible.io>
766+
2. Go to your site settings
767+
3. Copy the JavaScript snippet provided
768+
4. Paste it here
769+
770+
For more information, see <https://plausible.io/docs/plausible-script>
754771
announcement:
755772
anyOf:
756773
- string

0 commit comments

Comments
 (0)