From 71c8d549ab44a10d3469f779626d86ab5cfab6d0 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Sun, 5 Feb 2023 12:01:49 -0800 Subject: [PATCH] fix: fix duplicate link click tracking --- layouts/partials/head.html | 45 ++++++++++++++++++------------------ layouts/partials/header.html | 2 +- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 5ea0aa32..a608ba70 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) => { @@ -183,28 +206,6 @@ throwOnError : false }); {{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) { - link.addEventListener('click', clickHandler) - link.addEventListener('auxclick', clickHandler) - } }; {{if $data.enableSPA | default $.Site.Data.config.enableSPA}} diff --git a/layouts/partials/header.html b/layouts/partials/header.html index a7f6733b..1925a0cc 100644 --- a/layouts/partials/header.html +++ b/layouts/partials/header.html @@ -1,6 +1,6 @@
{{ $config := cond (eq $.Site.Language.Lang "en") "config" (printf "config.%s" $.Site.Language.Lang) }} -

{{ ( index $.Site.Data $config ).page_title | default $.Site.Data.config.page_title }}

+

{{ ( index $.Site.Data $config ).page_title | default $.Site.Data.config.page_title }}

{{ i18n "search" }}