Skip to content

Commit 1d11913

Browse files
committed
fix: ensure pageviews are fetched after DOM is loaded
1 parent 0537196 commit 1d11913

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ $ docker cp cover.webp kungfux.github.io:/workspaces/kungfux.github.io/assets/me
5858
`assets/js/progress.js`, `assets/css/jekyll-theme-chirpy.scss`, `_includes/metadata-hook.html`
5959
- Trigger PWA update automatically
6060
`assets/js/auto-update.js`, `_includes/metadata-hook.html`
61+
- Fix pageviews
62+
`_includes/pageviews/goatcounter.html`

_includes/pageviews/goatcounter.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!-- Display GoatCounter pageviews -->
2+
<script>
3+
document.addEventListener('DOMContentLoaded', () => {
4+
const pv = document.getElementById('pageviews');
5+
6+
if (pv !== null) {
7+
const uri = location.pathname.replace(/\/$/, '');
8+
const url = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`;
9+
10+
fetch(url)
11+
.then((response) => response.json())
12+
.then((data) => {
13+
const count = data.count.replace(/\s/g, '');
14+
pv.innerText = new Intl.NumberFormat().format(count);
15+
})
16+
.catch((error) => {
17+
pv.innerText = '1';
18+
});
19+
}
20+
});
21+
</script>

0 commit comments

Comments
 (0)