Skip to content

feat: Add Umami analytics and view count to article details #1129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/icons/eye.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions layouts/partials/article/components/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ <h3 class="article-subtitle">
</time>
</div>
{{ 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>
{{ end }}

Expand Down
1 change: 1 addition & 0 deletions layouts/partials/head/analytics/include.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ partial "head/analytics/provider/umami" . }}
37 changes: 37 additions & 0 deletions layouts/partials/head/analytics/provider/umami.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{- $umami := .Site.Params.Analytics.Umami -}}
{{- if $umami -}}
{{- $host := default "https://cloud.umami.is" $umami.Host -}}
{{- if and ($umami.Enabled) ($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 -}}
1 change: 1 addition & 0 deletions layouts/partials/head/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
{{ end }}

{{- template "_internal/google_analytics.html" . -}}
{{- partial "head/analytics/include.html" . -}}
{{- partial "head/custom.html" . -}}