Skip to content

Commit 1896f62

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

File tree

9 files changed

+75
-37
lines changed

9 files changed

+75
-37
lines changed
Lines changed: 6 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 0 additions & 8 deletions
This file was deleted.

_includes/google-analytics.html

Lines changed: 0 additions & 14 deletions
This file was deleted.

_includes/head.html

Lines changed: 1 addition & 1 deletion
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

Lines changed: 22 additions & 9 deletions
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 %}
Lines changed: 18 additions & 0 deletions
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

Lines changed: 14 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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)