What will you learn in this post?
Yeap! Another post about Jekyll! :smirk:
How did I get there?
For those just want to know how to make it, just skip to next section.
I wanted to displace how many people have visited the post in my blog. I knew, somehow, Google Analytics could provide me the information. But it really took me a long time to get it done eventually.
I built this site using Jekyll hosted on GitHub, which means I don’t have access to any dynamic page or
_plugins feature of Jekyll. After some searching, I thought of the following alternatives.
Jekyll-ga is a plugin for loading Google Analytics data into Jekyll. One possible alternative is to build the site locally and then upload the generated site to GitHub.
I admit that Jekyll-ga seems to be a nice plugin for those hosted other than GitHub. But for me, I didn’t like this idea much since I had either to leave the source file of my blog untracked or to use two repos for source code and generated code respectively. Either way seems ugly to me. But since my options are quite limited, I tried it, but failed. Considering how ugly this way may be, I quickly moved on before finding out why I failed.
Core Reporting API of Google Analytics
Google Analytics provides sufficient APIs for retrieving information from it.
I think it might work, of course, with great effort. Its drawback is that the pageview will only update when I manually run the script, authenticate it, generate JSON file, and push to GitHub. If I didn’t write blog for some time, I may probably forget to update the JSON file. And of course, authenticating every time is very annoying.
Google Analytics superProxy
Then, someone replied me on my question on StackOverflow and recommend me of Google Analytics superProxy, which is the final solution I would like to talk about in this post.
You may first refer to superProxy README for information. But since Google App Engine (GAE) has upgraded, some UI has new names, which makes it harder to get it done. This video tutorial on YouTube is also of great help.
Next, I’m going to explain the changed part in superProxy README.
We need to create a project at Google Developers Console. My project id is
ovilia-blog-1234, so it will be hosted on
Create Client ID
In Google Developers Console, click
APIS & AUTH>
Create New Client ID.
https://ovilia-blog-1234.appspot.com. (Note that you need to enter the protocal here. )
AUTHORIZED REDIRECT URIto be
xxxwith your project ID. )
Create Client ID.
You can follow the next steps according to superProxy README.
superProxy README said quite clear on that. But I spent much time debugging on some OpenSSL problem (and I still don’t now how to solve it now) when I used Ubuntu. When I switched to Mac, it just worked!
Test your query well on Google Analytics Query Explorer 2.
When it works, you should see something like this.
If you visit the link in the image, you can get the JSON file of pageview information!
Since GAE may not be accessed sometime, I cached the JSON file to GitHub host so that when AJAX call for JSON on GAE fails or has no response in 30 seconds, it will call that from GitHub, but with some acceptable inconsistence.
dimensions. There may be
http://zhangwenli.com/blog/?reference=douban in different rows of data, so I need to sum them up.
Oh. We’re almost complete now. We can make the display of pageview more interesting! :smirk:
I set the refresh rate of superProxy to be 10 minutes. But since you may get the cached version from GitHub, it’s not always accurate. And the
metrics is set to be
users so that when one visit the same page some days later, it will not be counted as another visit.
Come first next time! :yum: