From 7df2bb6f5e681ce3329e4faf6ac6cbcda7a3f14e Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Sun, 05 Feb 2023 20:01:49 +0000
Subject: [PATCH] fix: fix duplicate link click tracking

---
 layouts/partials/head.html |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/layouts/partials/head.html b/layouts/partials/head.html
index 8efccb1..a608ba7 100644
--- a/layouts/partials/head.html
+++ b/layouts/partials/head.html
@@ -160,6 +160,29 @@
           )
         }
       {{end}}
+      
+      // analytics
+      function clickHandler(evt) {
+        const target = evt.target 
+        const classNames = target.className.split(" ")
+        const broken = classNames.includes("broken")
+        const internal = classNames.includes("internal-link")
+        plausible("Link Click", {
+          props: {
+            href: target.href,
+            broken,
+            internal,
+            graph: false,
+          }
+        })
+      }
+
+      const links = document.querySelectorAll("a")
+      for (link of links) {
+        if (link.className.includes("root-title")) {
+          link.addEventListener('click', clickHandler, {once: true})
+        }
+      }
     }
 
     const init = (doc = document) => {
@@ -207,4 +230,5 @@
   {{end}}
   {{ $trimmedURL := trim (index (split .Site.BaseURL "://") 1) "/" }}
   <script defer data-domain="{{$trimmedURL}}" src="https://plausible.io/js/script.js"></script>
+  <script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
 </head>

--
Gitblit v1.10.0