Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit afb848b

Browse files
committed
Add pageview counter to footer with percentage display
- Display current page views and total site views in footer - Show percentage of page views relative to total - Use elegant single-line format with eye icon - Integrate with existing GoatCounter analytics
1 parent 54e2c26 commit afb848b

File tree

3 files changed

+73
-43
lines changed

3 files changed

+73
-43
lines changed

_includes/footer.html

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,72 @@
3030

3131
<!-- Page views counter -->
3232
{% if site.pageviews.provider and site.analytics[site.pageviews.provider].id %}
33-
<p class="pageviews-display">
34-
Page Views (<span id="footer-pageviews">
35-
<i class="fas fa-spinner fa-spin small"></i>
36-
</span>),
37-
Total Views (<span id="footer-totalviews">
38-
<i class="fas fa-spinner fa-spin small"></i>
39-
</span>)
33+
<p class="pageviews-display text-muted small">
34+
<i class="fas fa-eye fa-fw"></i>
35+
<span id="footer-pageviews">
36+
<i class="fas fa-spinner fa-spin"></i>
37+
</span> /
38+
<span id="footer-totalviews">
39+
<i class="fas fa-spinner fa-spin"></i>
40+
</span>
41+
(<span id="footer-percentage">
42+
<i class="fas fa-spinner fa-spin"></i>
43+
</span>%)
4044
</p>
45+
46+
<!-- Include the pageview counter script -->
47+
<script>
48+
document.addEventListener('DOMContentLoaded', () => {
49+
const pv = document.getElementById('footer-pageviews');
50+
const totalPv = document.getElementById('footer-totalviews');
51+
const percentPv = document.getElementById('footer-percentage');
52+
53+
let pageCount = 0;
54+
let totalCount = 0;
55+
56+
if (pv !== null) {
57+
const uri = location.pathname.replace(/\/$/, '');
58+
const url = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`;
59+
60+
fetch(url)
61+
.then((response) => response.json())
62+
.then((data) => {
63+
pageCount = parseInt(data.count.replace(/\D/g, '')) || 1;
64+
pv.innerText = new Intl.NumberFormat().format(pageCount);
65+
updatePercentage();
66+
})
67+
.catch((error) => {
68+
pageCount = 1;
69+
pv.innerText = '1';
70+
updatePercentage();
71+
});
72+
}
73+
74+
if (totalPv !== null) {
75+
const totalUrl = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/TOTAL.json`;
76+
77+
fetch(totalUrl)
78+
.then((response) => response.json())
79+
.then((data) => {
80+
totalCount = parseInt(data.count.replace(/\D/g, '')) || 1;
81+
totalPv.innerText = new Intl.NumberFormat().format(totalCount);
82+
updatePercentage();
83+
})
84+
.catch((error) => {
85+
totalCount = 1;
86+
totalPv.innerText = '1';
87+
updatePercentage();
88+
});
89+
}
90+
91+
function updatePercentage() {
92+
if (pageCount > 0 && totalCount > 0 && percentPv !== null) {
93+
const percentage = ((pageCount / totalCount) * 100).toFixed(2);
94+
percentPv.innerText = percentage;
95+
}
96+
}
97+
});
98+
</script>
4199
{% endif %}
42100

43101
<p>

_includes/js-selector.html

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,3 @@
8585
{% endif %}
8686
{% endif %}
8787

88-
<!-- Footer Pageviews -->
89-
{% assign provider = site.pageviews.provider %}
90-
{% if provider and provider != empty %}
91-
{% case provider %}
92-
{% when 'goatcounter' %}
93-
{% if site.analytics[provider].id != empty and site.analytics[provider].id %}
94-
{% include pageviews/footer-counter.html %}
95-
{% endif %}
96-
{% endcase %}
97-
{% endif %}

_includes/pageviews/footer-counter.html

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,27 @@
1111
// Fetch page-specific views
1212
const pageUrl = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/${encodeURIComponent(uri)}.json`;
1313

14-
console.log('Fetching page views from:', pageUrl);
15-
1614
fetch(pageUrl)
17-
.then((response) => {
18-
if (!response.ok) {
19-
throw new Error(`HTTP error! status: ${response.status}`);
20-
}
21-
return response.json();
22-
})
15+
.then((response) => response.json())
2316
.then((data) => {
24-
console.log('Page view data:', data);
25-
const count = data.count ? data.count.replace(/\D/g, '') : '0';
26-
footerPv.innerText = count ? new Intl.NumberFormat().format(count) : '0';
17+
const count = data.count.replace(/\D/g, '');
18+
footerPv.innerText = new Intl.NumberFormat().format(count);
2719
})
2820
.catch((error) => {
29-
console.error('Error fetching page views:', error);
30-
footerPv.innerText = '0';
21+
footerPv.innerText = '1';
3122
});
3223

3324
// Fetch total site views
3425
const totalUrl = `https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/TOTAL.json`;
3526

36-
console.log('Fetching total views from:', totalUrl);
37-
3827
fetch(totalUrl)
39-
.then((response) => {
40-
if (!response.ok) {
41-
throw new Error(`HTTP error! status: ${response.status}`);
42-
}
43-
return response.json();
44-
})
28+
.then((response) => response.json())
4529
.then((data) => {
46-
console.log('Total view data:', data);
47-
const count = data.count ? data.count.replace(/\D/g, '') : '0';
48-
footerTotal.innerText = count ? new Intl.NumberFormat().format(count) : '0';
30+
const count = data.count.replace(/\D/g, '');
31+
footerTotal.innerText = new Intl.NumberFormat().format(count);
4932
})
5033
.catch((error) => {
51-
console.error('Error fetching total views:', error);
52-
footerTotal.innerText = '0';
34+
footerTotal.innerText = '1';
5335
});
5436
}
5537
});

0 commit comments

Comments
 (0)