How to Exclude Yourself Completely from Piwik Statistics in Wordpress
I wrote about how to exclude Wordpress preview pages from Piwik statistics a few weeks ago. Recently, I received an email from a Piwik developer. Naturally, my first reaction was, "OMG! Piwik emailed me!" After calming down, I read the email. He mentioned that it is possible to set a cookie in the Piwik settings menu that will exclude all of my site visits, not just article preview pages. My response was:
I know of the Piwik cookie feature. I found it the day after I wrote the article, and I have been using it ever since. I felt a little dumb that my first thought was to modify my php source code rather than to look in the settings menu.
I mostly research and type up my articles during my lunch break at work. So, I went on to explain:
I still use the php code in addition to the cookie, though. I don't have a permanent PC at work, so I would have to set the cookie every day. I wouldn't mind this, but I forget sometimes. The php code ensures that I won't have preview pages in my site statistics. Preview pages tend to be the only pages I log in to while at work.
I started thinking about that today. Why should I exclude only preview pages? I should exclude myself completely. Naturally, my first instinct was to write code rather than check the setting menu again. I never learn.
The only times I ever go on the site are when I'm checking something I did to it. Because of this, I'm always logged in to the site; Wordpress is user-based even when viewing the site. This means that I can check for my username in the PHP code.
The original code I wrote is:
<?php if (! preg_match('/.*preview=true.*/', $_SERVER['QUERY_STRING'])): ?>
Piwik-pasted code goes here.
<?php endif; ?>
Rather than check the query string, I used the WordPress API to check to see if someone is logged in. If the user is logged in, see if the user is me. For the copy-pasters out there, I replaced my name with "admin" in the code below because that is the WordPress default.
<?php global $current_user; get_currentuserinfo(); ?>
<?php if ( (! is_user_logged_in()) || (is_user_logged_in() && ($current_user->user_login != "admin")) ): ?>
Piwik-pasted code goes here.
<?php endif; ?>
Because this code uses the WordPress API, this code will work only on WordPress run sites. The previous code will work on any site; if "preview=true" is typed into the query string of a URL, regardless if it makes sense or would be used, the Piwik code would not be generated.
After I started typing this article, I realized that I once again forgot to look in the Piwik settings menu. I looked through it and found that there are two ways of excluding a user: the cookie mentioned above or by IP address. Neither option would work very well for me, and I can't exclude by username.
This is OK, though. Piwik can't account for a WordPress user name. It is made to work on any website, not just WordPress sites. I wouldn't expect it to look at a WordPress username. Piwik does have a plug-in library, but I haven't checked those.
If you run a website and still haven't checked out Piwik, I recommend you do. I believe that it's the best web analytic tool out there.