How to make a blog hit counter on Blogger (Part 1 of 2)

  • 0
 --This article is a bit technical--

As you maybe have seen, we installed last week something to count the views of each article to better analyze our performance. It doesn’t like great (as I always says, honesty is something crucial!) but it counts and that’s its only purpose so it’s enough!

You were maybe laughing when you’ve seen that we’re writing an article on a dummy view counter ; so would I if I wasn’t its creator (who needed on complete day for this counter)!

But, it’s much more complicated than adding a simple widget at the end of post via WordPress or whatever! No, folks, we’re on Blogger and Blogger isn’t written in PHP and is not as flexible as WordPress or the newer platforms are!
It appeared, after a bit of research that there isn’t any Blogger variable which is counting the view per article (or that they cannot be accessed because they’re existing, as we can see on the photo).

It would be too simple to be able to take these numbers directly, isn't it?

So, without any answer to get these variables and no solution on Internet without using a closed-source third party service, I decided to build our own view counter (YOLO!).
I have to say that the only web language that I can fairly use for web development is Python associated with Flask (but if you’re using Bottle, Django or just used to Python, it is very easy to understand!).

So I wrote two basic functions: one for the article from the homepage (where they’re aren’t properly read so I do not add one view in this function) and one for the article that are requested from the article-specific page (where I’m sure they’ve clicked/read; so I’m adding 1). Here is the code:

__author__ = 'laurentmeyer'

from flask import Flask, render_template, abort, request
import pickle

app = Flask(__name__)

DictUrlViews = dict()

def test():
 return str(0)

# This view is called by each article when it is specifically called in his own page (this function adds one view).
def get_count(id):
 DictUrlViews = pickle.load(open( "savefile.txt", "rb" ))
 if id is not None:
  postId = int(id)
  views = DictUrlViews.get(postId, 0)

  # Yeah, you're right that makes no sense to add it and not to display it.
  # You'll have to invert the two lines
  DictUrlViews[postId] = views+1

  pickle.dump(DictUrlViews, open('savefile.txt', 'wb+'))

  return render_template("numberTemplate.html", number = str(DictUrlViews[postId]))

# This function is called by all the articles of the main page to get their view count without adding one each time
def get_count_without_modifying(id):
  DictUrlViews = pickle.load(open( "savefile.txt", "rb" ))
  if id is not None:
  postId = int(id)
  views = DictUrlViews.get(postId, 0)
  return render_template("numberTemplate.html", number = str(views))

def set_count(id):
 DictUrlViews = pickle.load(open( "savefile.txt", "rb" ))
 if id is not None:
  postId = int(id)
  views = request.args.get("views")
  if (views) is not None:
   views = int(views)
   if views >= DictUrlViews.get(postId, 0):
    DictUrlViews[postId] = views
    pickle.dump(DictUrlViews, open('savefile.txt', 'wb+'))
    return str(views)

if __name__ == "__main__":"", debug=True)

You maybe noticed that the pickle function is useless loading on every call; the explanation is quite tricky: I’m using to host this code. It means that I do not have control on the server stops/restarts, which means that I need data persistence (and I find Pickle much better than SQL for so little things); and yeah, I could load only one time the pickle by calling it in the initialization function, however the initialization function is provided by PythonAnywhere (mine won’t be called when deployed, was just for local testing) and is, I think, easily modifiable but I didn’t wanna search further (coder is a lazy machine that transforms coffee into code).

So, I deployed on PythonAnywhere and I searched a way to connect to my blog…

To be continued …

Laurent Meyer, Android Developer