Case Study: Divezone.Net

In our last post, we talked about custom variables and I showed you some simple ways to add custom variables to your Piwik code to track membership and role activity. Another way you can use custom variables is when you want to track a content site. To dig a little deeper with content, we will be using a website about diving called DiveZone.net.

Divezone.net use case: Scuba Diving Content Website

This is a perfect example to show you how you can use custom variables to track a rich content website in your Piwik install.

Why use custom variables for content?
While Piwik does an awesome job of telling you how your individual content is doing, if you have a blog or website, where there are many categories and sub-categories, you may want to know how these perform. The only way (and best way) to do this is via custom variables. In this case, you can have a report in custom variables track the most popular areas for your visitors and potentially where you should create more content. For each custom variable report, you can get to know page views, time on page, bounce rate, which can then help you assessing which ones are popular.

To get your Piwik install to capture category and sub category information is really simple. I will show you how to grab this info in WordPress, but it’s just as easy in any other CMS. In fact, Divezone, does not use the standard WordPress categories, but “custom-types” which shows you there are many ways to get data into your Piwik. For example, a normal page load in Divezone would output the following:


[...]
_paq.push(['setCustomVariable', '1','type','divesite']);
_paq.push(['setCustomVariable', '2','location','Tulamben']);
_paq.push(['setCustomVariable', '4','fishes','Big Potato Grouper, Black Tip Shark, Garden Eel, Jack Fish, Surgeonfish']);
_paq.push(['setCustomVariable', '5','divesite-rating','5']);
[...]

They use the asynchronous Piwik code. For example below, I will use the standard Piwik code, and I will show you how to make it work in WordPress using the internal functionality. Even if you don’t use WordPress, getting category information is relatively easy as most CMS programs use some sort of structure. You just need to find how they call their categories, sub-categories –or use some kind of attribute tag to push tracking info into Piwik.

Let’s capture some data!
So let’s have some fun and keep it relatively simple. For the scope of this post, I will only tackle how we could grab category and sub category, but you can get as creative as you like. For example, at Divezone they capture user ratings too (as you saw in the example above). So for our purposes, if a visitor came to DiveZone and were to visit a page about Diving in Red Sea: divezone.net/diving/red-sea, the code you would want to output would be the following (in the standard code):


[...]
piwikTracker.setCustomVariable(1, "Type", "diving", "page");
piwikTracker.setCustomVariable(2, "Location", "Red Sea", "page");
piwikTracker.trackPageView();
[...]

Let’s look at this closer. Remember you can track up to 5 custom variables per visit and/or up to 5 custom variables per page view. In this case we are using two slots in the scope of Page (index 1 & 2). This means as a user goes to different pages, we will be able to capture which category/sub-categories they visit. If you use the “visit” scope you would lose the custom variable recordings for the previous page views. It will only grab the last one.

After giving an index number and choosing scope of Page, the data in between is the NAME variable and the VALUE variable. To denote we want to know “Type” of page we use the VALUE variable to grab the Category of the page. In the second custom variable we are using NAME to denote “Location” and use the VALUE variable to capture what sub-category (in this case “Red Sea”) the user is looking at.

How is the data captured?
In any CMS, like I mentioned before, there are ways to capture categories. As Divezone is using WordPress, I will show how you to do it with built-in WordPress functions. To the get the category, we will use the get_the_category function. So this is how the code will look for us to know this is a “diving” page:


[...]
<?php
$category = get_the_category();
if (!empty($category[0])) {
   echo 'piwikTracker.setCustomVariable(1, "Type", "'.$category[0]->cat_name.'", "page");';
}
?>
[...]

Next we need to get the location, which in this case is a sub category. All we need to do is to increment category[0] to category [1] and change the code for the next slot. In other words the “Location”, will be grabbed by this code:


[...]
<?php
$category = get_the_category();
if (!empty($category[1])) {
    echo 'piwikTracker.setCustomVariable(2, "Location", "'.$category[1]->cat_name.'", "page");';
}
?>
[...]

So now you have it, to put it all together, this is the code you want just before piwikTracker.enableLinkTracking:


[...]
<?php
$category = get_the_category();
if (!empty($category[0])) {
    echo 'piwikTracker.setCustomVariable(1, "Type", "'.$category[0]->cat_name.'", "page");';
}
if (!empty($category[1])) {
    echo 'piwikTracker.setCustomVariable(2, "Location", "'.$category[1]->cat_name.'", "page");';
}
?>
piwikTracker.trackPageView();
[...]

Time for the data

So after you install your custom variables, you can get a table like you see above. With this data you can tell which category and which sub category is looked at the most. This will let you decide where to spend more time and which areas are popular with your visitors. Not only that, it could help you decide what content to use for promotions or which content you want to feature to get better exposure.

Also at the bottom of your custom variables menu within Piwik, you can choose the icon of a table with a plus sign to get even greater details. Now you can see in our case, for example, which categories got the most time on site, kept users engaged and lead to the most user action.

Wasn’t that simple? Now it’s your turn. What type of custom variables do you track? How has it helped you get better insights? And if you have an idea for a future post, please don’t be shy to send an email and let us know.


Adrian Speyer

With more than ten years experience in a large Ecommerce company as the Web Analytics lead, Adrian is an Expert in Web Analytics. He is an active Piwik Core team member and contributes with Marketing ideas, Analytics blog posts and special projects. Adrian also created the Piwik module for Magento Ecommerce.