Skip to content

Commit

Permalink
refactor: improve web analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Feb 25, 2024
1 parent 0baa0bb commit 7a31828
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ analytics:

# Pageviews settings
pageviews:
provider: # [google | goatcounter]
provider: # now only supports 'goatcounter'

# Prefer color scheme setting.
#
Expand Down
5 changes: 2 additions & 3 deletions _includes/analytics/goatcounter.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!-- GoatCounter -->

<script
data-goatcounter="https://{{ site.analytics.goatcounter.id }}.goatcounter.com/count"
async
src="https://gc.zgo.at/count.js"
src="//gc.zgo.at/count.js"
data-goatcounter="https://{{ site.analytics.goatcounter.id }}.goatcounter.com/count"
></script>
1 change: 0 additions & 1 deletion _includes/analytics/google.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!-- The GA snippet -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script defer src="https://www.googletagmanager.com/gtag/js?id={{ site.analytics.google.id }}"></script>
<script>
Expand Down
29 changes: 15 additions & 14 deletions _includes/js-selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@
<script id="MathJax-script" async src="{{ site.data.origin[type].mathjax.js | relative_url }}"></script>
{% endif %}

<!-- Pageviews -->
{% if page.layout == 'post' %}
<!-- Pageviews counter -->
{% if site.pageviews.provider != empty and site.pageviews.provider %}
{% case site.pageviews.provider %}
{% assign provider = site.pageviews.provider %}

{% if provider and provider != empty %}
{% case provider %}
{% when 'goatcounter' %}
{% if site.analytics.goatcounter.id != empty and site.analytics.goatcounter.id %}
{% include views-counter/goatcounter.html %}
{% if site.analytics[provider].id != empty and site.analytics[provider].id %}
{% include pageviews/{{ provider }}.html %}
{% endif %}
{% endcase %}
{% endif %}
Expand All @@ -113,13 +115,12 @@
<script defer src="{{ 'app.min.js' | prepend: js_dist | relative_url }}"></script>
{% endif %}

<!-- GA -->
{% if site.analytics.google.id != empty and site.analytics.google.id %}
{% include analytics/google.html %}
{% endif %}

<!-- GoatCounter -->
{% if site.analytics.goatcounter.id != empty and site.analytics.goatcounter.id %}
{% include analytics/goatcounter.html %}
{% endif %}
<!-- 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 changes: 18 additions & 0 deletions _includes/pageviews/goatcounter.html
Original file line number Diff line number Diff line change
@@ -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>
11 changes: 0 additions & 11 deletions _includes/views-counter.html

This file was deleted.

12 changes: 0 additions & 12 deletions _includes/views-counter/goatcounter.html

This file was deleted.

14 changes: 10 additions & 4 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- post-nav
- comments
---

{% include lang.html %}

<article class="px-1">
Expand Down Expand Up @@ -74,16 +75,21 @@ <h1 data-toc-skip>{{ page.title }}</h1>
</span>

<div>
<!-- pageviews counter -->
{% include views-counter.html %}
<!-- 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>
<!-- .d-flex -->
</div>
<!-- .post-meta -->
</header>

<div class="content">
Expand Down

0 comments on commit 7a31828

Please sign in to comment.