Skip to content

Commit

Permalink
feat(analytics)!: add post pageviews for GoatCounter (#1543)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Cotes Chung <11371340+cotes2020@users.noreply.github.com>
  • Loading branch information
kungfux and cotes2020 authored Feb 25, 2024
1 parent dd9d9c6 commit a19a00d
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 42 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: # now only supports 'goatcounter'

# Prefer color scheme setting.
#
Expand Down
6 changes: 6 additions & 0 deletions _includes/analytics/goatcounter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- GoatCounter -->
<script
async
src="//gc.zgo.at/count.js"
data-goatcounter="https://{{ site.analytics.goatcounter.id }}.goatcounter.com/count"
></script>
13 changes: 13 additions & 0 deletions _includes/analytics/google.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- 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
31 changes: 22 additions & 9 deletions _includes/js-selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,32 @@
<script id="MathJax-script" async src="{{ site.data.origin[type].mathjax.js | relative_url }}"></script>
{% endif %}

<!-- Pageviews -->
{% if page.layout == 'post' %}
{% assign provider = site.pageviews.provider %}

{% if provider and provider != empty %}
{% case provider %}
{% when 'goatcounter' %}
{% if site.analytics[provider].id != empty and site.analytics[provider].id %}
{% include pageviews/{{ provider }}.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 %}
{% endif %}

<!-- GoatCounter -->
{% if site.goatcounter.id != empty and site.goatcounter.id %}
{% include goatcounter.html %}
{% endif %}
<!-- Web Analytics -->
{% for analytics in site.analytics %}
{% capture str %}{{ analytics }}{% endcapture %}
{% assign type = str | split: '{' | first %}
{% if site.analytics[type].id and site.analytics[type].id != empty %}
{% include analytics/{{ type }}.html %}
{% endif %}
{% endfor %}
{% endif %}
18 changes: 18 additions & 0 deletions _includes/pageviews/goatcounter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Display GoatCounter pageviews -->
<script>
let pv = document.getElementById('pageviews');

if (pv !== null) {
const uri = location.pathname.replace(/\/$/, '');
const url = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`;

fetch(url)
.then((response) => response.json())
.then((data) => {
pv.innerText = new Intl.NumberFormat().format(data.count);
})
.catch((error) => {
pv.innerText = '1';
});
}
</script>
18 changes: 14 additions & 4 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,22 @@ <h1 data-toc-skip>{{ page.title }}</h1>
</em>
</span>

<!-- read time -->
{% include read-time.html content=content prompt=true lang=lang %}
<div>
<!-- pageviews -->
{% if site.pageviews.provider and site.analytics[site.pageviews.provider].id %}
<span>
<em id="pageviews">
<i class="fas fa-spinner fa-spin small"></i>
</em>
{{ site.data.locales[lang].post.pageview_measure }}
</span>
{% endif %}

<!-- read time -->
{% include read-time.html content=content prompt=true lang=lang %}
</div>
</div>
<!-- .d-flex -->
</div>
<!-- .post-meta -->
</header>

<div class="content">
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 a19a00d

Please sign in to comment.