feat(analytics)!: add post pageviews for GoatCounter (#1543)

---------

Co-authored-by: Cotes Chung <11371340+cotes2020@users.noreply.github.com>
This commit is contained in:
Alexander Fuks 2024-02-25 22:50:41 +04:00 committed by GitHub
parent 5dbda0c09f
commit b641b3f1f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 84 additions and 41 deletions

View file

@ -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
id: # fill in your Google Analytics ID analytics:
google:
id: # fill in your Google Analytics ID
goatcounter:
id: # fill in your GoatCounter ID
goatcounter: # Pageviews settings
id: # fill in your Goatcounter ID pageviews:
provider: # now only supports 'goatcounter'
# Prefer color scheme setting. # Prefer color scheme setting.
# #

View 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>

View 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>

View file

@ -1,8 +0,0 @@
<!-- GoatCounter -->
<script
data-goatcounter="https://{{ site.goatcounter.id }}.goatcounter.com/count"
async
src="https://gc.zgo.at/count.js"
></script>

View file

@ -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>

View file

@ -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">

View file

@ -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 %} {% endif %}
{% include goatcounter.html %} {% endfor %}
{% endif %}
{% endif %} {% endif %}

View 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>

View file

@ -74,12 +74,22 @@ tail_includes:
</em> </em>
</span> </span>
<!-- read time --> <div>
{% include read-time.html content=content prompt=true lang=lang %} <!-- 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> </div>
<!-- .d-flex -->
</div> </div>
<!-- .post-meta -->
</header> </header>
<div class="content"> <div class="content">

View file

@ -15,7 +15,7 @@
} }
h1 + .post-meta { h1 + .post-meta {
> span + span::before { span + span::before {
@include dot; @include dot;
} }