Matomo (Piwik) Analytics – Auto Set Ignore Visits Cookie Plugin

Article Updated: 22 May 2018

Piwik new name is Matomo.On January 09, 2018. Piwik.org announced a strange (update: logical, after brand separation) move to change their 10-years old well-known and established brand name from “Piwik” to “Matomo” (word from Japanese, which means “honesty” or “openness” or “directness“).

Piwik.pro will continue to exist as Enterprise solution (read: commercial), and it will have zero connections with former free Piwik project; while Matomo will continue as a stand-alone independent service & product, open source and free (excluding cloud/enterprise solutions, of course). Given the radical changes in Piwik Market we saw last year with the introduction of some pretty expensive plugins, those were the clear signals and this move does not really come much of a shock, as it might appear at first.

While we are not sure what will happen to entire underlying code base in terms of projects, name spaces, class names etc. and will this plugin continue to work after complete re-branding (probably it will through aliases – and another update: according to the authors it will be), but we ought to give you heads-up of what’s going on. While it is trivial to change Piwik to Matomo in below code (search & replace), we’ll keep the code as it is for now. In fact, if you do this kind of replacement now, you will break your Piwik/Matomo installation, since all the code base below is still good-old piwik-based.

Judging by the things at GitHub as of this moment, Piwik (and all other repositories) are still keeping their original titles, which adds even more to the entire confusion. It remains to be seen what will happen… Update: repositories are gradually being renamed to reflect this change.

More info / juicy details about this story here: https://matomo.org/faq/new-to-piwik/faq_21984/

Piwik is a great open-source analytics platform, very solid alternative* to Google Analytics. However, this article is not about how to install and use it, but rather implement one neat feature that can be useful for website masters, owners and administrators.

PIWIK ANALYTICS – AUTO SET IGNORE VISITS COOKIE PLUGIN

Piwik Analytics Logo

Piwik Analytics Logo

Are you using Piwik on a daily basis? Then, you probably know about many of it’s strengths (and weaknesses, so to speak), but one of the most important functions of any analytics software is filtering out (or ignoring) your own visits. Because, you do not wish to distort or inflate website traffic data with your own page requests, while debugging code or working on a new design.

For this reason, inside Piwik you have 2 options at your disposal:

  1. Administration Settings > Personal > Settings > Exclude your visits using a cookie option
  2. Administration Settings > Website > Settings > Global list of Excluded IPs

Both options offer you some advantages and disadvantages one over another. For example, cookie method is the simplest, does not generally require maintenance (as long as you do not accidentally or intentionally delete it), and has a default validity period of 2 years. Once the ignore cookie is set, Piwik will not track your visits across pages you visit from that particular browser/device combination.

That’s the first limitation of the cookie method: it works PER device and PER browser. If you access your website from a different browser and/or device, where you don’t have it set, your visits will be counted and tracked!

IP Address filtering method, also available in Google Analytics Dashboard, is very easy to set: you simply list one IP address per line, and save it. Once you set IP exclusion list, it will ignore ALL visits from it, regardless of browser/device combination.

However, IP lists tend to get obsolete. Depending on your ISP(s), your IP may change as frequently as every 5 minutes or so (if you are behind NAT), or it may (almost) never change, if they use static IPs, or you explicitly request/purchase/rent one.

Also, what if your IP address belongs to many other users, for example in case of corporate networks? You may ignore valid visits from other users, colleagues / coworkers etc. as a negative side-effect.

In this article we are talking about first option – the cookie method, and how we can automate it’s installation for every device we visit/login Piwik Admin Dashboard.

By default, you have to set the cookie manually by visiting Personal settings page inside Piwik Administration section. This is, however, most impractical. First, as a developer, you might clear cache/cookies from your browser too frequently, to be bothered with manual cookie setting. Second, if you happen to be using ISP service that asks ridiculous fees for static IPs, or it just changes too frequently, IP-based filtering is again, out of the question, and you have to fall back to the cookie method.

There is, of course, a 3rd option: to use in-private browsing mode that provide tracking filters as well (available in some browsers, such as Firefox (for Mobile and Desktop), Firefox Focus (for Mobile) (sidenote: note that in our case with Firefox and Focus private modes visits are perfectly tracked in Piwik, maybe it’s a bug or something) and some other less known browser “flavors/brands”. Ghostery privacy browser is the only app that completely blocks both Piwik/Matomo and Google Analytics tracking out-of-the-box known to us today.

Trouble with above methods is that you have to switch to those particular browsers/modes, unless you don’t use them regularly already. If you forget to do so, your visits will be recorded. Also, sometimes they might block things that you do not want to be blocked (e.g. during website testing/development stages), or they may require additional tweaking/customization, to tune what part you wish to block, and which parts not. Oh, and you will have to login with your email/password credentials every time you visit Piwik Dashboard.

Finally, 4th option is to use a privacy desktop browser extensions – e.g. uBlock Origin or Ghostery, but they are not yet available for mobile browser apps. You can use Ghostery browser on mobile.

Auto Set Ignore Cookie – Plugin Code Development

Auto Set Ignore Cookie plugin will automatically, without any intervention from your side, set ignore cookie every time admin logs-in or access any page inside Piwik dashboard. It will take a fraction of time, but it will make your life so much easier.

We aren’t Piwik architecture experts and frankly, never written any Piwik Plugin before this one, so it might not be the 100% bullet-proof and perfect solution. But, it works and fulfills our need.

It is always a good idea to study and analyze some existing plugins, and try to figure things on your own. Piwik’s source code is huge, it has many components and classes, and it is truly overwhelming for a novice programmer to get a grip of it.

Ok, here we go:

  1. We start our code by creating a blank folder AutoSetIgnoreCookie, named exactly as our plugin’s name (without any space or special characters, camelCase is allowed).
  2. Second, we create inside this folder a main plugin’s AutoSetIgnoreCookie.php file. This file may call other files, classes and so on (usually the case in larger projects), but it will hold entire code in our case, because it is very simple, in fact.
  3. Along .php file(s), we can add readme and plugin.json files, used for instruction and plugin standardized information template (plugin manifest). They are less important, but we will add them as well.

And this is it, really. That’s the basic structure of any Piwik Plugin out there.

Now, here’s our code that needs to be placed inside AutoSetIgnoreCookie.php file:

<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\AutoSetIgnoreCookie;

use Piwik\Piwik; // required for user login check
use Piwik\Tracker\IgnoreCookie; // required for IgnoreCookie class

class AutoSetIgnoreCookie extends \Piwik\Plugin
{
	public function registerEvents()
	{
		return array(
			'Platform.initialized' => 'autoSetIgnoreCookie'
		);
	}

	public function autoSetIgnoreCookie()
	{
		// get current user username
		$user = Piwik::getCurrentUserLogin();

		// if user is logged-in => auto set ignore cookie
		if ($user != 'anonymous') {
			$ignoreCookie = IgnoreCookie::getIgnoreCookie();
			$ignoreCookie->set('ignore', '*');
			$ignoreCookie->save();
		}
	}
}

Save everything, upload plugin to:

/path-to-piwik-installation-directory/plugins/

folder, activate it from within:

Website Dashboard > Piwik’s Administration > System > Plugins

section and you are done!

REMINDER

You will not be able to remove ignore visits cookie from Piwik’s admin dashboard, until you deactivate/uninstall this plugin.

Also, if you use Incognito/Private/InPrivate modes in Chrome/Firefox/Explorer/Edge/etc. browsers, your visits WILL be tracked! This is one disadvantage of cookie method we were discussing in the introduction above, where IP-based filtering is far superior.

However, if your IP(s) constantly change on a daily/weekly basis, there is not much you can do, unless you write some local script to remotely change your Piwik’s config/database values, but that falls far beyond the scope of this article.

DOWNLOAD PLUGIN (.zip archive)

Download Piwik (Matomo) AutoSetignoreCookie plugin (version 1.0.1)

 

Comments


Post A Comment

I have read and consent to Privacy Policy and Terms and Conditions