Skip to content

Commit f3e5c5d

Browse files
authored
fix: ensure pageviews are fetched after DOM is loaded (#2071)
1 parent 2d9265e commit f3e5c5d

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

_includes/pageviews/goatcounter.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<!-- Display GoatCounter pageviews -->
22
<script>
3-
let pv = document.getElementById('pageviews');
3+
document.addEventListener('DOMContentLoaded', () => {
4+
const pv = document.getElementById('pageviews');
45

5-
if (pv !== null) {
6-
const uri = location.pathname.replace(/\/$/, '');
7-
const url = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`;
6+
if (pv !== null) {
7+
const uri = location.pathname.replace(/\/$/, '');
8+
const url = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`;
89

9-
fetch(url)
10+
fetch(url)
1011
.then((response) => response.json())
1112
.then((data) => {
1213
const count = data.count.replace(/\s/g, '');
@@ -15,5 +16,6 @@
1516
.catch((error) => {
1617
pv.innerText = '1';
1718
});
18-
}
19+
}
20+
});
1921
</script>

0 commit comments

Comments
 (0)