Wednesday 20 February 2013

Post Views Counter Widget For Blogger

Important: All scripts hosted on widcraft.googlecode.com don't work anymore because Google has blocked that SVN repository.

Finally, MBT found a great way to display post view count on Blogger. We will do this by using Firebase. With Firebase you can now create dynamic and data-driven plugins for blogger without worrying about backend development. Firebase's Free plan gives you 10GB Free bandwidth and 5GB Free storage.

The postviews count wont change when you refresh the blog homepage but it will change only when you visit individual posts. This plugin will display views only once pages are visited. It is not synchronized with your Google analytics stats data, therefore it will only display correct views for the new posts that you publish.

Create Your Firebase Account And Database:

First you need to create a Firebase account to use this widget, which is free at this link. Fill up the easy steps sensibly and then once your account has been activated, you must create your first Firebase database.

  • Towards the bottom right side of your account, you will find the following submit box:

  • Inside this box input your desire firebase name. This generated the following firebase URL: https://example.firebaseio.com.

Installing Postviews Plugin:

  • Go To Blogger > Template > Click Edit HTML > Tick the box "Expand widget Templates"
  • Search for ]]></b:skin>
  • Just above it paste the following CSS code:

    /*-------- Post Views  ----------*/
    #views-container {
    width: 75px;
    float: right;
    }

    .mbtloading {
    background: url('https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgc5Wn8iV8T3XKzrW6q_bia_k7TKh0Olhkl8t3snEMxjMLXElRyFILWr8vlzqaQyeMx4CCKXU1yifPts2an9fgbsWvzKelvzfq8AzWREJyE_TMDoipuGqvM5Cnb_Mga2hU4lj93XTxL8BE/s1600/loading.gif') no-repeat left center;
    width: 16px;
    height: 16px;
    }

    .viewscount {
    float: right;
    color: #EE5D06;
    font: bold italic 14px arial;
    }

    .views-text {
    float: left;
    font: bold 12px arial;
    color: #333;
    }

    .views-icon{
    background: url('https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm_xSfbai8v-mrbnf6zQr3oVzBNQOKsIG4lse6IlGh2HQq2IRcMtIo5Ugigv-WFjlqKtmJyOKImqCVGfFlKBqgeMJtIK5UFwe4Mn9LIiQHkFnmxOB2Cgxe3PyP-1cklSbvqVDoiJmgAGE/s1600/postviews.png') no-repeat left;
    border: 0px;
    display: block;
    width: 16px;
    height: 16px;
    float: left;
    padding: 0px 2px;
    }

  • Now search for </body> and just above it paste the following JavaScript code:

<!-- Post Views Script by BWidgets -->
<script type='text/javascript'>
    window.setTimeout(function() {
        document.body.className = document.body.className.replace(&#39;loading&#39;, &#39;&#39;);
      }, 10);
  </script>
<script src='https://cdn.firebase.com/v0/firebase.js' type='text/javascript'/>
<script>
$.each($(&#39;a[name]&#39;), function(i, e) {
var elem = $(e).parent().find(&#39;#postviews&#39;).addClass(&#39;mbtloading&#39;);
var blogStats = new Firebase(&quot;https://example.firebaseio.com/pages/id/&quot; + $(e).attr(&#39;name&#39;));
blogStats.once(&#39;value&#39;, function(snapshot) {
var data = snapshot.val();
var isnew = false;
if(data == null) {
data= {};
data.value = 0;
data.url = window.location.href;
data.id = $(e).attr(&#39;name&#39;);
isnew = true;
}
elem.removeClass(&#39;mbtloading&#39;).text(data.value);
data.value++;
if(window.location.pathname!=&#39;/&#39;)
{
if(isnew)
blogStats.set(data);
else
blogStats.child(&#39;value&#39;).set(data.value);
}
});
});
</script>

  • Replace the example with your firebase name. Make sure you replace it correctly. Time for the final step.  Find this code:

<data:post.body/>

  • Just above it paste the following HTML,

<!-- Post Views Counter by BWidgets-->
<div id='views-container'><span class='views-icon'/><div class='views-text'>Views:</div> <div class='mbtloading viewscount' id='postviews'/></div>

  • Hit save and you are all done!

Having Trouble?

If you are finding multi occurrences of <data:post.body/> code then choose the first occurrence. Also, this widget will only display correct views for the new posts that you publish. Thanks to MBT for this amazing script.
Important: Check our new website TricksPanda.com for WordPress tutorials, plugins and more.
 
Powered by Blogger.