Skip to content

Commit

Permalink
fix: ensure pageviews are fetched after DOM is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfux committed Nov 29, 2024
1 parent 0537196 commit 1d11913
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ $ docker cp cover.webp kungfux.github.io:/workspaces/kungfux.github.io/assets/me
`assets/js/progress.js`, `assets/css/jekyll-theme-chirpy.scss`, `_includes/metadata-hook.html`
- Trigger PWA update automatically
`assets/js/auto-update.js`, `_includes/metadata-hook.html`
- Fix pageviews
`_includes/pageviews/goatcounter.html`
21 changes: 21 additions & 0 deletions _includes/pageviews/goatcounter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Display GoatCounter pageviews -->
<script>
document.addEventListener('DOMContentLoaded', () => {
const 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) => {
const count = data.count.replace(/\s/g, '');
pv.innerText = new Intl.NumberFormat().format(count);
})
.catch((error) => {
pv.innerText = '1';
});
}
});
</script>

0 comments on commit 1d11913

Please sign in to comment.