Skip to content

Commit b641b3f

Browse files
kungfuxcotes2020
andauthored
feat(analytics)!: add post pageviews for GoatCounter (mmistakes#1543)
--------- Co-authored-by: Cotes Chung <[email protected]>
1 parent 5dbda0c commit b641b3f

File tree

10 files changed

+85
-42
lines changed

10 files changed

+85
-42
lines changed

_config.yml

+10-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@ google_site_verification: # fill in to your verification string
4949
# ↑ --------------------------
5050
# The end of `jekyll-seo-tag` settings
5151

52-
google_analytics:
53-
id: # fill in your Google Analytics ID
54-
55-
goatcounter:
56-
id: # fill in your Goatcounter ID
52+
# Web Analytics Settings
53+
analytics:
54+
google:
55+
id: # fill in your Google Analytics ID
56+
goatcounter:
57+
id: # fill in your GoatCounter ID
58+
59+
# Pageviews settings
60+
pageviews:
61+
provider: # now only supports 'goatcounter'
5762

5863
# Prefer color scheme setting.
5964
#

_includes/analytics/goatcounter.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!-- GoatCounter -->
2+
<script
3+
async
4+
src="//gc.zgo.at/count.js"
5+
data-goatcounter="https://{{ site.analytics.goatcounter.id }}.goatcounter.com/count"
6+
></script>

_includes/analytics/google.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- Global site tag (gtag.js) - Google Analytics -->
2+
<script defer src="https://www.googletagmanager.com/gtag/js?id={{ site.analytics.google.id }}"></script>
3+
<script>
4+
document.addEventListener('DOMContentLoaded', function (event) {
5+
window.dataLayer = window.dataLayer || [];
6+
function gtag() {
7+
dataLayer.push(arguments);
8+
}
9+
10+
gtag('js', new Date());
11+
gtag('config', '{{ site.analytics.google.id }}');
12+
});
13+
</script>

_includes/goatcounter.html

-8
This file was deleted.

_includes/google-analytics.html

-14
This file was deleted.

_includes/head.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
{% endif %}
7373

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

_includes/js-selector.html

+22-9
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,32 @@
9595
<script id="MathJax-script" async src="{{ site.data.origin[type].mathjax.js | relative_url }}"></script>
9696
{% endif %}
9797

98+
<!-- Pageviews -->
99+
{% if page.layout == 'post' %}
100+
{% assign provider = site.pageviews.provider %}
101+
102+
{% if provider and provider != empty %}
103+
{% case provider %}
104+
{% when 'goatcounter' %}
105+
{% if site.analytics[provider].id != empty and site.analytics[provider].id %}
106+
{% include pageviews/{{ provider }}.html %}
107+
{% endif %}
108+
{% endcase %}
109+
{% endif %}
110+
{% endif %}
111+
98112
{% if jekyll.environment == 'production' %}
99113
<!-- PWA -->
100114
{% if site.pwa.enabled %}
101115
<script defer src="{{ 'app.min.js' | prepend: js_dist | relative_url }}"></script>
102116
{% endif %}
103117

104-
<!-- GA -->
105-
{% if site.google_analytics.id != empty and site.google_analytics.id %}
106-
{% include google-analytics.html %}
107-
{% endif %}
108-
109-
<!-- GoatCounter -->
110-
{% if site.goatcounter.id != empty and site.goatcounter.id %}
111-
{% include goatcounter.html %}
112-
{% endif %}
118+
<!-- Web Analytics -->
119+
{% for analytics in site.analytics %}
120+
{% capture str %}{{ analytics }}{% endcapture %}
121+
{% assign type = str | split: '{' | first %}
122+
{% if site.analytics[type].id and site.analytics[type].id != empty %}
123+
{% include analytics/{{ type }}.html %}
124+
{% endif %}
125+
{% endfor %}
113126
{% endif %}

_includes/pageviews/goatcounter.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!-- Display GoatCounter pageviews -->
2+
<script>
3+
let pv = document.getElementById('pageviews');
4+
5+
if (pv !== null) {
6+
const uri = location.pathname.replace(/\/$/, '');
7+
const url = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`;
8+
9+
fetch(url)
10+
.then((response) => response.json())
11+
.then((data) => {
12+
pv.innerText = new Intl.NumberFormat().format(data.count);
13+
})
14+
.catch((error) => {
15+
pv.innerText = '1';
16+
});
17+
}
18+
</script>

_layouts/post.html

+14-4
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,22 @@ <h1 data-toc-skip>{{ page.title }}</h1>
7474
</em>
7575
</span>
7676

77-
<!-- read time -->
78-
{% include read-time.html content=content prompt=true lang=lang %}
77+
<div>
78+
<!-- pageviews -->
79+
{% if site.pageviews.provider and site.analytics[site.pageviews.provider].id %}
80+
<span>
81+
<em id="pageviews">
82+
<i class="fas fa-spinner fa-spin small"></i>
83+
</em>
84+
{{ site.data.locales[lang].post.pageview_measure }}
85+
</span>
86+
{% endif %}
87+
88+
<!-- read time -->
89+
{% include read-time.html content=content prompt=true lang=lang %}
90+
</div>
7991
</div>
80-
<!-- .d-flex -->
8192
</div>
82-
<!-- .post-meta -->
8393
</header>
8494

8595
<div class="content">

_sass/layout/post.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
}
1616

1717
h1 + .post-meta {
18-
> span + span::before {
18+
span + span::before {
1919
@include dot;
2020
}
2121

0 commit comments

Comments
 (0)