Piwiki: most viewed pages

I needed to develop a job that was to listen the most clicked questions in a FAQ. The initial solution was to develop some service that would do that, but nowadays some minutes on google is just enough, and so on, my answer was Piwiki.

Piwiki is very powerful analytics solution, and a big deal is that you can use an API provided by this. Better than this, it’s free and very documented. 🙂 That was the dart in the target. If you need something like that, it’s a cake recipe, take a look above.

1. Install Piwiki
2. Create a new website to have the measures
3. Create a new user with grant to view the API methods
4. Put the piwiki code inside your page
5. Create trackpage events on every link that you want
6. And for finish, get the JSON with the most accessed URLs

1. Installing Piwiki

It’s a piece of cake to install Piwiki, just take a look here: http://piwik.org/docs/installation/. Next.

2. Create a new website to have the measures

You can see here: http://piwik.org/docs/manage-websites/#toc-add-a-website

The only consideration here is that if you’re planning to measure URLs that uses hashes in its structure. If you wanna use that, just say yes to the question “Keep Page URLs fragments when track Page URLs”. Don’t you know what I’m saying? This is an example: yousite.com/faq/#first-question.

Keep the website id to be used in the javascript track code.

3. Create a new user with grant to view the API methods

You can see here: http://piwik.org/docs/manage-users/#toc-create-users

Just keep the user’s token_auth to be used in your API request. Next.

4. Put the piwiki code inside your page

It’s very simple. Take a look:

<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);

(function() {
var u=(("https:" == document.location.protocol) ? "https" : "http") + "://YOUR_PIWIKI_URL_HERE/";
_paq.push(["setTrackerUrl", u+"piwik.php"]);
_paq.push(["setSiteId", "YOUR_WEBSITE_ID_HERE"]);
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Piwik Code -->

5. Create trackpage events on every link that you want

An example:

<a href="#first-question" onclick="javascript:_paq.push(['trackPageView', '/#first-question'])">First question: I wanna track this!</a>

6. And for finish, get the JSON with the most accessed URLs

Just keep in mind that you need to realize some clicks in your links to get this report with some information. 🙂

Call this in your Piwiki URL service, don’t forget to replace the TOKEN_AUTH and WEBSITE_ID to get the JSON correctly. The URL was broken in some pieces to be more understandable, just put everything together to get working.

/?module=API
&method=Actions.getPageUrls
&idSite=WEBSITE_ID
&period=month
&date=today
&format=JSON
&token_auth=TOKEN_AUTH
&expanded=1

In this example I set the period to a month, starting with the date “today”.

Advertisements

One thought on “Piwiki: most viewed pages

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s