Skip to content

Commit

Permalink
feat: add post views counter for GoatCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfux authored and cotes2020 committed Feb 25, 2024
1 parent 5dbda0c commit c14709f
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 36 deletions.
15 changes: 10 additions & 5 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ google_site_verification: # fill in to your verification string
# ↑ --------------------------
# The end of `jekyll-seo-tag` settings

google_analytics:
id: # fill in your Google Analytics ID

goatcounter:
id: # fill in your Goatcounter ID
# Web Analytics Settings
analytics:
google:
id: # fill in your Google Analytics ID
goatcounter:
id: # fill in your GoatCounter ID

# Pageviews settings
pageviews:
provider: # [google | goatcounter]

# Prefer color scheme setting.
#
Expand Down
7 changes: 7 additions & 0 deletions _includes/analytics/goatcounter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- GoatCounter -->

<script
data-goatcounter="https://{{ site.analytics.goatcounter.id }}.goatcounter.com/count"
async
src="https://gc.zgo.at/count.js"
></script>
14 changes: 14 additions & 0 deletions _includes/analytics/google.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- The GA snippet -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script defer src="https://www.googletagmanager.com/gtag/js?id={{ site.analytics.google.id }}"></script>
<script>
document.addEventListener('DOMContentLoaded', function (event) {
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}

gtag('js', new Date());
gtag('config', '{{ site.analytics.google.id }}');
});
</script>
8 changes: 0 additions & 8 deletions _includes/goatcounter.html

This file was deleted.

14 changes: 0 additions & 14 deletions _includes/google-analytics.html

This file was deleted.

2 changes: 1 addition & 1 deletion _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
{% endif %}

<!-- GA -->
{% if jekyll.environment == 'production' and site.google_analytics.id != empty and site.google_analytics.id %}
{% if jekyll.environment == 'production' and site.analytics.google.id != empty and site.analytics.google.id %}
<link rel="preconnect" href="https://www.google-analytics.com" crossorigin="use-credentials">
<link rel="dns-prefetch" href="https://www.google-analytics.com">

Expand Down
20 changes: 16 additions & 4 deletions _includes/js-selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,31 @@
<script id="MathJax-script" async src="{{ site.data.origin[type].mathjax.js | relative_url }}"></script>
{% endif %}

{% if page.layout == 'post' %}
<!-- Pageviews counter -->
{% if site.pageviews.provider != empty and site.pageviews.provider %}
{% case site.pageviews.provider %}
{% when 'goatcounter' %}
{% if site.analytics.goatcounter.id != empty and site.analytics.goatcounter.id %}
{% include views-counter/goatcounter.html %}
{% endif %}
{% endcase %}
{% endif %}
{% endif %}

{% if jekyll.environment == 'production' %}
<!-- PWA -->
{% if site.pwa.enabled %}
<script defer src="{{ 'app.min.js' | prepend: js_dist | relative_url }}"></script>
{% endif %}

<!-- GA -->
{% if site.google_analytics.id != empty and site.google_analytics.id %}
{% include google-analytics.html %}
{% if site.analytics.google.id != empty and site.analytics.google.id %}
{% include analytics/google.html %}
{% endif %}

<!-- GoatCounter -->
{% if site.goatcounter.id != empty and site.goatcounter.id %}
{% include goatcounter.html %}
{% if site.analytics.goatcounter.id != empty and site.analytics.goatcounter.id %}
{% include analytics/goatcounter.html %}
{% endif %}
{% endif %}
11 changes: 11 additions & 0 deletions _includes/views-counter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Pageviews counter -->
{% if site.pageviews.provider != empty
and site.pageviews.provider
and site.analytics.goatcounter.id != empty
and site.analytics.goatcounter.id
%}
<span>
<em id="pageviews-counter"> 1 </em>
{{ site.data.locales[lang].post.pageview_measure }}
</span>
{% endif %}
12 changes: 12 additions & 0 deletions _includes/views-counter/goatcounter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
const uri = location.pathname.replace(/\/$/, '');
const fetchPromise = fetch(
`https://{{site.analytics.goatcounter.id}}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`
);

fetchPromise
.then((response) => response.json())
.then((data) => {
document.getElementById('pageviews-counter').innerText = data.count;
});
</script>
10 changes: 7 additions & 3 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
- post-nav
- comments
---

{% include lang.html %}

<article class="px-1">
Expand Down Expand Up @@ -74,8 +73,13 @@ <h1 data-toc-skip>{{ page.title }}</h1>
</em>
</span>

<!-- read time -->
{% include read-time.html content=content prompt=true lang=lang %}
<div>
<!-- pageviews counter -->
{% include views-counter.html %}

<!-- read time -->
{% include read-time.html content=content prompt=true lang=lang %}
</div>
</div>
<!-- .d-flex -->
</div>
Expand Down
2 changes: 1 addition & 1 deletion _sass/layout/post.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}

h1 + .post-meta {
> span + span::before {
span + span::before {
@include dot;
}

Expand Down

0 comments on commit c14709f

Please sign in to comment.