mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-04-28 19:43:31 +08:00
**feat:** Add Umami analytics and view count to article details
- Added conditional rendering for view count in article details based on `Site.Params.Analytics`. - Updated Umami analytics script to use structured parameters and fixed script inclusion. - Included Umami analytics script in the head section.
This commit is contained in:
parent
9e6b7b22a9
commit
96be9b767b
1
assets/icons/eye.svg
Normal file
1
assets/icons/eye.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 1024 1024" width="24" height="24"><path d="M997 435.6c-28.2-36.7-65.4-81.4-107.4-122.2-54.6-53.2-110.5-95.6-166.1-126.3-71.2-39.2-142-59.1-210.7-59.1s-139.6 19.9-210.7 59.1c-55.5 30.7-111.4 73.1-166.1 126.3-42 40.8-79.3 85.5-107.5 122.1-34.8 45.3-34.8 107.8 0 153.1 28.2 36.7 65.4 81.4 107.4 122.2C190.5 764 246.5 806.4 302 837.1c71.2 39.2 142.1 59.1 210.7 59.1 68.7 0 139.6-19.9 210.8-59 55.5-30.7 111.4-73.1 166.1-126.3 42-40.8 79.3-85.6 107.4-122.2 34.8-45.3 34.8-107.8 0-153.1z m-37.3 99c-66.2 89-243.8 299-446.8 299-57.5 0-117.7-17.1-179-50.8-50.5-27.7-101.7-66.6-152.2-115.6-51.1-49.5-91-99.5-115.7-132.6-10-13.5-10-31.6 0-44.9 66.1-89 243.7-299 446.8-299 57.5 0 117.6 17.1 179 50.8 50.5 27.7 101.7 66.6 152.2 115.6 51.1 49.5 91 99.5 115.7 132.6 9.9 13.4 9.9 31.6 0 44.9z" p-id="5343" fill="#8a8a8a"></path><path d="M512 321.7c-105.7 0-191.6 86-191.6 191.6 0 105.7 86 191.6 191.6 191.6s191.6-86 191.6-191.6c0-105.7-86-191.6-191.6-191.6z m0 319.2c-70.3 0-127.6-57.2-127.6-127.6S441.7 385.7 512 385.7s127.6 57.2 127.6 127.6S582.3 640.9 512 640.9z" p-id="5344" fill="#8a8a8a"></path></svg>
|
After Width: | Height: | Size: 1.1 KiB |
@ -45,6 +45,13 @@
|
|||||||
</time>
|
</time>
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
{{ if .Site.Params.Analytics }}
|
||||||
|
<div class="article-time--count-wrapper" style="display: none">
|
||||||
|
{{ partial "helper/icon" "eye" }}
|
||||||
|
<time class="article-time--count"></time>
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
||||||
</footer>
|
</footer>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
1
layouts/partials/head/analytics/include.html
Normal file
1
layouts/partials/head/analytics/include.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
{{ partial "head/analytics/provider/umami" . }}
|
37
layouts/partials/head/analytics/provider/umami.html
Normal file
37
layouts/partials/head/analytics/provider/umami.html
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
{{- $umami := .Site.Params.Analytics.Umami -}}
|
||||||
|
{{- if $umami -}}
|
||||||
|
{{- $host := default "https://cloud.umami.is" $umami.Host -}}
|
||||||
|
{{- if and (not $umami.Disabled) ($umami.SiteId) -}}
|
||||||
|
<script defer src="{{ $host }}/script.js" data-website-id="{{ $umami.SiteId }}"></script>
|
||||||
|
|
||||||
|
{{- if not .IsHome -}}
|
||||||
|
<script>
|
||||||
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
|
const wrapper = document.getElementsByClassName("article-time--count-wrapper")[0];
|
||||||
|
const e = wrapper && wrapper.getElementsByClassName("article-time--count")[0];
|
||||||
|
if (e == null)
|
||||||
|
return;
|
||||||
|
const path = window.location.pathname;
|
||||||
|
let options;
|
||||||
|
{{- if $umami.Token -}}
|
||||||
|
const token = {{ $umami.Token }};
|
||||||
|
options = {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
{{- end -}}
|
||||||
|
let st = new Date({{ $umami.StartAt }}).getTime();
|
||||||
|
if (isNaN(st))
|
||||||
|
st = 0; // fallback to 1970-01-01T00:00:00Z
|
||||||
|
const now = new Date().getTime();
|
||||||
|
const url = `{{ $host }}/api/websites/{{ $umami.SiteId }}/stats?startAt=${st}&endAt=${now}&url=${encodeURIComponent(path)}`;
|
||||||
|
fetch(url, options).then(r => r.json()).then(data => {
|
||||||
|
wrapper.style.display = '';
|
||||||
|
e.textContent = String(data.visits ? data.visits.value : 0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}
|
@ -23,4 +23,5 @@
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{- template "_internal/google_analytics.html" . -}}
|
{{- template "_internal/google_analytics.html" . -}}
|
||||||
|
{{- partial "head/analytics/include.html" . -}}
|
||||||
{{- partial "head/custom.html" . -}}
|
{{- partial "head/custom.html" . -}}
|
||||||
|
Loading…
Reference in New Issue
Block a user