I’m not a developer and using jekyll for blog hosting.
I’ve a website aquibqureshi26.github.io and I’m trying to build blog hit tracker on the page to show how many users has visited.
I’ve an API hosted outside, when I’m calling the API via the javascript then it just shows loading… and doesn’t fetch the actual page views.
My API provides output in JSON in below format.
{
"page": "/my-postnew",
"hits": 11
}
I’ve modified the _layout and edited the post.html and default.html
Snippet from post.html
<span>
<em id="pv" class="pageviews">
<i class="fa-duotone fa-circle-dot"></i>
</em>
<span id="hitCount">Loading...</span>
</span>
Snippet from default.html
<!doctype html>
{% include origin-type.html %}
{% include lang.html %}
{% if site.theme_mode %}
{% capture prefer_mode %}data-mode="{{ site.theme_mode }}"{% endcapture %}
{% endif %}
<!-- `site.alt_lang` can specify a language different from the UI -->
<html lang="{{ site.alt_lang | default: site.lang }}" {{ prefer_mode }}>
{% include head.html %}
<body>
{% include sidebar.html lang=lang %}
<div id="main-wrapper" class="d-flex justify-content-center">
<div class="container d-flex flex-column px-xxl-5">
{% include topbar.html lang=lang %}
<div class="row flex-grow-1">
<main aria-label="Main Content" class="col-12 col-lg-11 col-xl-9 px-md-4">
{% if layout.refactor or layout.layout == 'default' %}
{% include refactor-content.html content=content lang=lang %}
{% else %}
{{ content }}
{% endif %}
</main>
<!-- panel -->
<aside aria-label="Panel" id="panel-wrapper" class="col-xl-3 ps-2 mb-5 text-muted">
<div class="access">
{% include_cached update-list.html lang=lang %}
{% include_cached trending-tags.html lang=lang %}
</div>
{% for _include in layout.panel_includes %}
{% assign _include_path = _include | append: '.html' %}
{% include {{ _include_path }} lang=lang %}
{% endfor %}
</aside>
</div>
<div class="row">
<!-- tail -->
<div id="tail-wrapper" class="col-12 col-lg-11 col-xl-9 px-md-4">
{% for _include in layout.tail_includes %}
{% assign _include_path = _include | append: '.html' %}
{% include {{ _include_path }} lang=lang %}
{% endfor %}
{% include_cached footer.html lang=lang %}
</div>
</div>
{% include_cached search-results.html lang=lang %}
</div>
<aside aria-label="Scroll to Top">
<button id="back-to-top" type="button" class="btn btn-lg btn-box-shadow">
<i class="fas fa-angle-up"></i>
</button>
</aside>
</div>
<div id="mask"></div>
{% if site.pwa.enabled %}
{% include_cached notification.html lang=lang %}
{% endif %}
<script>
document.addEventListener("DOMContentLoaded", function () {
let pageUrl = encodeURIComponent(window.location.pathname);
let apiUrl = `https://xyz.centralindia-01.azurewebsites.net/api/HttpTrigger1?website=websitename&page=${pageUrl}`;
console.log("Fetching hit count...");
console.log("Page URL:", window.location.pathname);
console.log("Encoded Page URL:", pageUrl);
console.log("API URL:", apiUrl);
fetch(apiUrl)
.then(response => {
console.log("Raw response:", response);
return response.text(); // Read as text first
})
.then(text => {
console.log("Raw response text:", text);
try {
let data = JSON.parse(text); // Convert to JSON
console.log("Parsed JSON data:");
console.table(data); // Display as a table if it's an object or an array
if (data.hits) {
document.getElementById("hitCount").innerText = `Views: ${data.hits}`;
} else {
document.getElementById("hitCount").innerText = "Views: 0";
}
} catch (error) {
console.error("JSON parsing error:", error);
document.getElementById("hitCount").innerText = "Views: Error";
}
})
.catch(error => {
console.error("Error fetching hit count:", error);
document.getElementById("hitCount").innerText = "Views: N/A";
});
});
</script>
<!-- JavaScripts -->
{% include js-selector.html %}
{% if page.mermaid %}
{% include mermaid.html %}
{% endif %}
{% include_cached search-loader.html %}
</body>
</html>
when i goto the console, it just shows. I’m unable to debug the javasccript which is under script
Uncaught SyntaxError: Unexpected end of input
any input or direction would help. thanks in advance
