Here I am going to show you a simple trick which can be used to keep track on every views plus add a like button with various customize options. But first lets just dive into the code.
Instead of going into Layout we are going to paste this code in our Post Itself. Just change Compose View to HTML View & Paste it under your post content.
This Code is for View Counter (Blogspot also has its own widget called Blog's Stats in Add a Gadget option. But it will not show you View Count on Each Post / Pages. But this code will.)
<!-- View Counter BEGIN --> <script> window.onload = function() { var xhr = new XMLHttpRequest(); var url = window.location.href.replace("https://","").replace(/\//g, ""); var urlr = url.substring(0,53); //document.getElementById("demo").innerHTML = urlr; xhr.open("GET", "https://api.countapi.xyz/hit/"+urlr); xhr.responseType = "json"; xhr.onload = function() { document.getElementById("viewcount").innerHTML = this.response.value; } xhr.send(); } </script> <h1><span>Views: <span id="viewcount"></span></span><h1> <p id="demo"></p> <!-- View Counter END -->
So what is happening in this code? What the script does is when some one loads the page it will get the url of the current page using javascript and pass the url to count API which will keep track on exact views on each page by UNIQUE URLS passing response in JSON and display the number as Views.
Note:- Incase the code does not work for you. Un-comment "//document." and save, which will show your unique URL in id="demo". Fix your URL Name / Post Title and comment out "//document." and save.
For Documentation Click HERE
This Code is for Like Button Counter
<!--LikeBtn.com BEGIN--> <span class="likebtn-wrapper" data-theme="padded"></span> <script>(function(d,e,s){if(d.getElementById("likebtn_wjs"))return;a=d.createElement(e);m=d.getElementsByTagName(e)[0];a.async=1;a.id="likebtn_wjs";a.src=s;m.parentNode.insertBefore(a, m)})(document,"script","//w.likebtn.com/js/w/widget.js");</script> <!--LikeBtn.com END-->
For Documentation Click HERE
Comments
Post a Comment