fix: ensure pageviews are fetched after DOM is loaded (#2071)
This commit is contained in:
parent
2966fc174d
commit
b4019f3517
1 changed files with 8 additions and 6 deletions
|
@ -1,12 +1,13 @@
|
||||||
<!-- Display GoatCounter pageviews -->
|
<!-- Display GoatCounter pageviews -->
|
||||||
<script>
|
<script>
|
||||||
let pv = document.getElementById('pageviews');
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
const pv = document.getElementById('pageviews');
|
||||||
|
|
||||||
if (pv !== null) {
|
if (pv !== null) {
|
||||||
const uri = location.pathname.replace(/\/$/, '');
|
const uri = location.pathname.replace(/\/$/, '');
|
||||||
const url = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`;
|
const url = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`;
|
||||||
|
|
||||||
fetch(url)
|
fetch(url)
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
const count = data.count.replace(/\s/g, '');
|
const count = data.count.replace(/\s/g, '');
|
||||||
|
@ -15,5 +16,6 @@
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
pv.innerText = '1';
|
pv.innerText = '1';
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Reference in a new issue