feat(analytics)!: add post pageviews for GoatCounter (#1543)
--------- Co-authored-by: Cotes Chung <11371340+cotes2020@users.noreply.github.com>
This commit is contained in:
parent
5dbda0c09f
commit
b641b3f1f2
10 changed files with 84 additions and 41 deletions
11
_config.yml
11
_config.yml
|
@ -49,11 +49,16 @@ google_site_verification: # fill in to your verification string
|
||||||
# ↑ --------------------------
|
# ↑ --------------------------
|
||||||
# The end of `jekyll-seo-tag` settings
|
# The end of `jekyll-seo-tag` settings
|
||||||
|
|
||||||
google_analytics:
|
# Web Analytics Settings
|
||||||
|
analytics:
|
||||||
|
google:
|
||||||
id: # fill in your Google Analytics ID
|
id: # fill in your Google Analytics ID
|
||||||
|
|
||||||
goatcounter:
|
goatcounter:
|
||||||
id: # fill in your Goatcounter ID
|
id: # fill in your GoatCounter ID
|
||||||
|
|
||||||
|
# Pageviews settings
|
||||||
|
pageviews:
|
||||||
|
provider: # now only supports 'goatcounter'
|
||||||
|
|
||||||
# Prefer color scheme setting.
|
# Prefer color scheme setting.
|
||||||
#
|
#
|
||||||
|
|
6
_includes/analytics/goatcounter.html
Normal file
6
_includes/analytics/goatcounter.html
Normal file
|
@ -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
_includes/analytics/google.html
Normal file
13
_includes/analytics/google.html
Normal file
|
@ -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>
|
|
@ -1,8 +0,0 @@
|
||||||
<!-- GoatCounter -->
|
|
||||||
|
|
||||||
<script
|
|
||||||
data-goatcounter="https://{{ site.goatcounter.id }}.goatcounter.com/count"
|
|
||||||
async
|
|
||||||
src="https://gc.zgo.at/count.js"
|
|
||||||
></script>
|
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
<!--
|
|
||||||
The GA snippet
|
|
||||||
-->
|
|
||||||
<!-- Global site tag (gtag.js) - Google Analytics -->
|
|
||||||
<script defer src="https://www.googletagmanager.com/gtag/js?id={{ site.google_analytics.id }}"></script>
|
|
||||||
<script>
|
|
||||||
document.addEventListener("DOMContentLoaded", function(event) {
|
|
||||||
window.dataLayer = window.dataLayer || [];
|
|
||||||
function gtag(){dataLayer.push(arguments);}
|
|
||||||
|
|
||||||
gtag('js', new Date());
|
|
||||||
gtag('config', '{{ site.google_analytics.id }}');
|
|
||||||
});
|
|
||||||
</script>
|
|
|
@ -72,7 +72,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<!-- GA -->
|
<!-- 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="preconnect" href="https://www.google-analytics.com" crossorigin="use-credentials">
|
||||||
<link rel="dns-prefetch" href="https://www.google-analytics.com">
|
<link rel="dns-prefetch" href="https://www.google-analytics.com">
|
||||||
|
|
||||||
|
|
|
@ -95,19 +95,32 @@
|
||||||
<script id="MathJax-script" async src="{{ site.data.origin[type].mathjax.js | relative_url }}"></script>
|
<script id="MathJax-script" async src="{{ site.data.origin[type].mathjax.js | relative_url }}"></script>
|
||||||
{% endif %}
|
{% 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' %}
|
{% if jekyll.environment == 'production' %}
|
||||||
<!-- PWA -->
|
<!-- PWA -->
|
||||||
{% if site.pwa.enabled %}
|
{% if site.pwa.enabled %}
|
||||||
<script defer src="{{ 'app.min.js' | prepend: js_dist | relative_url }}"></script>
|
<script defer src="{{ 'app.min.js' | prepend: js_dist | relative_url }}"></script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<!-- GA -->
|
<!-- Web Analytics -->
|
||||||
{% if site.google_analytics.id != empty and site.google_analytics.id %}
|
{% for analytics in site.analytics %}
|
||||||
{% include google-analytics.html %}
|
{% capture str %}{{ analytics }}{% endcapture %}
|
||||||
{% endif %}
|
{% assign type = str | split: '{' | first %}
|
||||||
|
{% if site.analytics[type].id and site.analytics[type].id != empty %}
|
||||||
<!-- GoatCounter -->
|
{% include analytics/{{ type }}.html %}
|
||||||
{% if site.goatcounter.id != empty and site.goatcounter.id %}
|
|
||||||
{% include goatcounter.html %}
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
18
_includes/pageviews/goatcounter.html
Normal file
18
_includes/pageviews/goatcounter.html
Normal file
|
@ -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>
|
|
@ -74,12 +74,22 @@ tail_includes:
|
||||||
</em>
|
</em>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
|
<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 -->
|
<!-- read time -->
|
||||||
{% include read-time.html content=content prompt=true lang=lang %}
|
{% include read-time.html content=content prompt=true lang=lang %}
|
||||||
</div>
|
</div>
|
||||||
<!-- .d-flex -->
|
|
||||||
</div>
|
</div>
|
||||||
<!-- .post-meta -->
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<div class="content">
|
<div class="content">
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
h1 + .post-meta {
|
h1 + .post-meta {
|
||||||
> span + span::before {
|
span + span::before {
|
||||||
@include dot;
|
@include dot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue