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
|
||||
|
||||
google_analytics:
|
||||
# Web Analytics Settings
|
||||
analytics:
|
||||
google:
|
||||
id: # fill in your Google Analytics ID
|
||||
|
||||
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.
|
||||
#
|
||||
|
|
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 %}
|
||||
|
||||
<!-- 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">
|
||||
|
||||
|
|
|
@ -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 %}
|
||||
<!-- 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
_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>
|
||||
</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 -->
|
||||
{% include read-time.html content=content prompt=true lang=lang %}
|
||||
</div>
|
||||
<!-- .d-flex -->
|
||||
</div>
|
||||
<!-- .post-meta -->
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="content">
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
}
|
||||
|
||||
h1 + .post-meta {
|
||||
> span + span::before {
|
||||
span + span::before {
|
||||
@include dot;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue