Add page view counter

master
Maik de Kruif 1 month ago
parent 479a4d8ebc
commit 8ff3722780
No known key found for this signature in database
GPG Key ID: DB1A8C782DD43CB3
  1. 6
      config/_default/hugo.toml
  2. 2
      config/production/hugo.toml
  3. 2
      content/portfolio/learn-to-program.md
  4. 5
      themes/maik-blog/archetypes/default.md
  5. 4
      themes/maik-blog/i18n/en.toml
  6. 10
      themes/maik-blog/layouts/partials/javascript.html
  7. 16
      themes/maik-blog/layouts/partials/page_views.html
  8. 14
      themes/maik-blog/layouts/posts/single.html
  9. 14
      themes/maik-blog/layouts/writeups/single.html

@ -60,6 +60,12 @@ enableEmoji = true
# Prefix of link to the git commit detail page. GitInfo must be enabled. # Prefix of link to the git commit detail page. GitInfo must be enabled.
gitUrl = "https://github.com/maikka39/Personal-Website/commit/" gitUrl = "https://github.com/maikka39/Personal-Website/commit/"
# Add domain for GoatCounter to enable
goatCounter = "webstats.maik.dev"
# Show pageviews on posts, writeups, etc.
showPageViews = true
# Set disableReadOtherPosts to true in order to hide the links to other posts. # Set disableReadOtherPosts to true in order to hide the links to other posts.
disableReadOtherPosts = false disableReadOtherPosts = false

@ -2,4 +2,4 @@ baseURL = "https://maik.dev/"
[services] [services]
[services.googleAnalytics] [services.googleAnalytics]
id = "UA-136337666-1" id = "UA-136337666-1"

@ -15,3 +15,5 @@ skills = [
This website was created for a school assignment. It was created with HTML and CSS only and no frameworks were used. This website was created for a school assignment. It was created with HTML and CSS only and no frameworks were used.
[Github](https://github.com/maikka39/Learn-to-Program) [Github](https://github.com/maikka39/Learn-to-Program)
[Live](https://maikka39.github.io/Learn-to-Program/)

@ -1,13 +1,16 @@
+++ +++
author = "Maik de Kruif" author = "Maik de Kruif"
title = "{{ replace .Name "-" " " | title }}" title = "{{ replace .Name "-" " " | title }}"
subtitle = ""
# showPageViews = false
date = {{ .Date }} date = {{ .Date }}
description = "" description = ""
images = [] cover = "img/writeups/google-ctf/2021/beginners-quest/10/cover.png"
tags = [ tags = [
"untagged", "untagged",
] ]
categories = [ categories = [
"uncategorized", "uncategorized",
] ]
# aliases = []
+++ +++

@ -34,3 +34,7 @@ other = "Table of Contents"
[wordCount] [wordCount]
one = "One Word" one = "One Word"
other = "{{ .Count }} Words" other = "{{ .Count }} Words"
[post]
pageViews = "Views"
unknownViews = "Unknown"

@ -8,7 +8,7 @@
{{- if .Site.Config.Services.GoogleAnalytics.ID }} {{- if .Site.Config.Services.GoogleAnalytics.ID }}
<!-- Global site tag (gtag.js) - Google Analytics --> <!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ .Site.Config.Services.GoogleAnalytics.ID }}"></script> <script async src="https://www.googletagmanager.com/gtag/js?id={{ .Site.Config.Services.GoogleAnalytics.ID }}"></script>
<script> <script async>
window.dataLayer = window.dataLayer || []; window.dataLayer = window.dataLayer || [];
function gtag() { function gtag() {
@ -24,4 +24,10 @@
{{- if gt (len $val) 0 }} {{- if gt (len $val) 0 }}
<script src="{{ $val }}"></script> <script src="{{ $val }}"></script>
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- if .Site.Params.goatCounter }}
<script data-goatcounter="https://{{ .Site.Params.goatCounter }}/count"
data-goatcounter-settings='{"path": "{{ .RelPermalink }}"}'
async src="//gc.zgo.at/count.js"></script>
{{- end}}

@ -0,0 +1,16 @@
{{- if .Site.Params.goatCounter }}
<span id="{{ .File.UniqueID }}" title="{{ i18n "post.pageViews" }}">{{ i18n "post.unknownViews" }}</span>
<script async>
let url =
"https://{{ .Site.Params.goatCounter }}/counter/" +
encodeURIComponent({{ .RelPermalink }}.replace(/(\/)?$/, "")) +
".json";
fetch(url)
.then(res => res.json())
.then(data => data.count)
.then(count => {
document.getElementById("{{ .File.UniqueID }}").innerText = count + " " + {{ i18n "post.pageViews" }};
});
</script>
{{- end}}

@ -77,6 +77,20 @@
{{ i18n "wordCount" .Page.WordCount }} {{ i18n "wordCount" .Page.WordCount }}
</p> </p>
{{- if and (default true .Params.showPageViews) (default true .Site.Params.showPageViews) }}
{{- if .Site.Params.goatCounter }}
<p>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
class="feather feather-activity">
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
<circle cx="12" cy="12" r="3"></circle>
</svg>
{{- partial "page_views.html" . -}}
</p>
{{- end }}
{{- end }}
<p> <p>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"

@ -77,6 +77,20 @@
{{ i18n "wordCount" .Page.WordCount }} {{ i18n "wordCount" .Page.WordCount }}
</p> </p>
{{- if and (default true .Params.showPageViews) (default true .Site.Params.showPageViews) }}
{{- if .Site.Params.goatCounter }}
<p>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
class="feather feather-activity">
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
<circle cx="12" cy="12" r="3"></circle>
</svg>
{{- partial "page_views.html" . -}}
</p>
{{- end }}
{{- end }}
<p> <p>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"

Loading…
Cancel
Save