From 1d4b612a4438ba6fcc2271da4e6c66749e8c9c57 Mon Sep 17 00:00:00 2001
From: Alphonse Mariya <alphonse.mariya@hotmail.com>
Date: Fri, 22 Jan 2021 16:39:34 +0000
Subject: [PATCH] Add support for Matomo (formerly Piwik) (#508)

---
 layouts/partials/analytics/matomo.html |   13 ++++++
 exampleSite/config.toml                |   45 ++++++++++++++--------
 layouts/_default/baseof.html           |    4 ++
 stackbit.yaml                          |   10 +++++
 CONTRIBUTORS.md                        |    1 
 5 files changed, 57 insertions(+), 16 deletions(-)

diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index 13543b4..3498a30 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -91,3 +91,4 @@
 - [Felix](https://github.com/lazyyz)
 - [Peter Duchnovsky](https://pduchnovsky.com)
 - [Alex Miranda](https://ammiranda.com)
+- [Alphonse Mariya](https://github.com/alfunx)
diff --git a/exampleSite/config.toml b/exampleSite/config.toml
index a9b89f5..fb1c55f 100644
--- a/exampleSite/config.toml
+++ b/exampleSite/config.toml
@@ -80,24 +80,37 @@
 [params.cloudflare]
     token = "token"
 
+# If you want to use Matomo(https://matomo.org) for analytics, add this section
+[params.matomo]
+    # Default value is "1", overwrite this if you are cloud-hosting
+    siteID = "ABCDE"
+    # For cloud-hosting, use provided URL, e.g. example.matomo.cloud
+    serverURL = "analytics.example.com"
+
 # If you want to implement a Content-Security-Policy, add this section
 [params.csp]
-	childsrc = ["'self'"]
-	fontsrc=["'self'",
-		"https://fonts.gstatic.com",
-		"https://cdn.jsdelivr.net/"]
-	formaction = ["'self'"]
-	framesrc = ["'self'"]
-	imgsrc = ["'self'"]
-	objectsrc = ["'none'"]
-	stylesrc = ["'self'",
-		"'unsafe-inline'",
-		"https://fonts.googleapis.com/",
-		"https://cdn.jsdelivr.net/"]
-	scriptsrc = ["'self'",
-		"'unsafe-inline'",
-		"https://www.google-analytics.com"]
-	prefetchsrc = ["'self'"]
+	  childsrc = ["'self'"]
+	  fontsrc = [
+        "'self'",
+		    "https://fonts.gstatic.com",
+		    "https://cdn.jsdelivr.net/"
+    ]
+	  formaction = ["'self'"]
+	  framesrc = ["'self'"]
+	  imgsrc = ["'self'"]
+	  objectsrc = ["'none'"]
+	  stylesrc = [
+        "'self'",
+		    "'unsafe-inline'",
+		    "https://fonts.googleapis.com/",
+		    "https://cdn.jsdelivr.net/"
+    ]
+	  scriptsrc = [
+        "'self'",
+		    "'unsafe-inline'",
+		    "https://www.google-analytics.com"
+    ]
+	  prefetchsrc = ["'self'"]
 
 [taxonomies]
   category = "categories"
diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html
index 435808f..dcdb107 100644
--- a/layouts/_default/baseof.html
+++ b/layouts/_default/baseof.html
@@ -145,6 +145,10 @@
     {{ if and .Site.Params.cloudflare .Site.Params.cloudflare.token }}
       {{- partial "analytics/cloudflare" . -}}
     {{ end }}
+
+    {{ if and .Site.Params.matomo .Site.Params.matomo.serverURL }}
+      {{- partial "analytics/matomo" . -}}
+    {{ end }}
   </body>
 
 </html>
diff --git a/layouts/partials/analytics/matomo.html b/layouts/partials/analytics/matomo.html
new file mode 100644
index 0000000..8fabb63
--- /dev/null
+++ b/layouts/partials/analytics/matomo.html
@@ -0,0 +1,13 @@
+<script type="application/javascript">
+  var _paq = window._paq = window._paq || [];
+  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
+  _paq.push(['trackPageView']);
+  _paq.push(['enableLinkTracking']);
+  (function() {
+    var u="https://{{ $.Site.Params.matomo.serverURL }}/";
+    _paq.push(['setTrackerUrl', u+'matomo.php']);
+    _paq.push(['setSiteId', '{{ $.Site.Params.matomo.siteID | default "1" }}']);
+    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
+    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
+  })();
+</script>
diff --git a/stackbit.yaml b/stackbit.yaml
index 78d9baf..5a95aa3 100644
--- a/stackbit.yaml
+++ b/stackbit.yaml
@@ -139,6 +139,16 @@
               - type: string
                 name: token
                 label: token for Cloudflare Web Analytics
+          - type: object
+            name: matomo
+            label: Matomo (optional)
+            fields:
+              - type: string
+                name: siteID
+                label: Site ID for Matomo
+              - type: string
+                name: serverURL
+                label: URL for Matomo
       - type: object
         name: languages
         fields:

--
Gitblit v1.10.0