@@ -26,6 +26,9 @@ const kStorage = "storage";
2626const kAnonymizeIp = "anonymize-ip" ;
2727const kVersion = "version" ;
2828
29+ // Plausible analytics
30+ export const kPlausibleAnalytics = "plausible-analytics" ;
31+
2932// Cookie consent properties
3033export const kCookieConsent = "cookie-consent" ;
3134const 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
8184export 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 }
0 commit comments