For users of the Thesis Theme for WordPress, this article describes how you can keep your Google Analytics account from tracking your own visits to your site. If you have several thousand visitors per day, this is probably not worth bothering with. However, if you typically have less than 700 per day, as in my case, this can help you to not have skewed statistics. When I am making minor tweaks, which is often, I visit my site often, to test them out … this can skew the statistics of Google Analytics, so I have implemented the following code to keep the stats from being influenced by my visits.
To implement this on your site, you need to put the following code into your custom_functions.php file:
function my_google_analytics() {
if ( !current_user_can('level_10') )
{ ?>
COPY AND PASTE your Google Analytics Code Here (about 8 lines of code)
<?php }?>
<?php
}
add_action('thesis_hook_after_html', 'my_google_analytics');
Note the need to hook thesis_hook_after_html instead of thesis_hook_after_footer … the former is needed to place the code right before the closing body tag.
Also, if you have used the Thesis Admin Panel to previously do the placing of your Google Analytics account, you will need to remove the code from that panel. That is, it should be eliminated from the following panel:
Note that I got the idea for this from the http://www.wprecipes.com/ site. I have tried to provide a direct link to their article, but am continuing to receive a timeout error from the site. Apparently their server is giving them some problems, but they should be credited with showing me the basic formula for keeping Google Analytics from tracking my visits [ specifically, I appreciated their article's reference to using if ( !current_user_can('level_10') ) ].

Or you can just set up a filter in Google Analytics based on your IP address.
In the “overview” screen that shows your various “website profiles,” you will see a link to “filter Manager” beneath the summaries. From that link you can set up all sorts of filters, one being to filter out all traffic coming from your IP address.
If your IP changes a lot, of course that wouldn’t work well. For most people though, I think it would do the trick just fine. Or are you saying that for some reason, that doesn’t work when using the thesis theme? Can’t see why it wouldn’t.
John,
What an excellent point! Frankly I had forgotten that one could do that. It would work fine in my case, but of course for someone who travels a good bit, and blogs from a hotel frequently (and there are several who do that), this method would be better, as their IP address would change.
No, there is nothing ‘wrong” with the Thesis Theme. My method just shows how to do this within Thesis. One could do the same thing with any theme, by just using the code inside the my_google_analytics() function and pasting it right above the closing body tag in their footer.php file. With Thesis, one doesn’t have a footer.php file — instead of messing with such core files, one makes customizations in the custom_functions.php file, centralizing the customizations, and making life a lot easier when the theme is upgraded to a new revision level and all of its core file change.
Anyway, thanks again for the very good catch.
Ah, I see, and yes I’ll bet there are many who blog while on the move. That solution then would take care of the issue no matter what. I’ll keep that code snippet. I have a feeling I’ll be using Thesis some day for a new site that I envision.
I absolutely agree about the use of “custom” files to keep modifications separate. I do that with my ‘Copyblogger” theme, though my custom files are strictly for CSS, and I have modified quite a bit in the php files. Are you saying that you can also keep php changes in separate custom files?
John,
Yes, with Thesis, the only changes you ever have to make are in the custom_functions.php and custom.css files … those will take care of changes you want to make to the sidebar, header, posting section, or whatever. I describe it a bit more in this post on Thesis Hooks, and that article also links to some other, related tutorial-type articles.
I like the way you have customized Copyblogger on your site. But, yes, for any new site, I think you would do well to base it on Thesis. That way, you are assured to have the best typography and most bulletproof XHTML and CSS and PHP coding. It is truly first class. You don’t just get a theme with it, you get a framework.
Thanks Bruce. When I get to doing this I will make a point of going through your articles on thesis. I do like how Chris builds themes. Believe me, I checked out hundreds of themes and found most of them lacking in one department or other. He seems to think of everything, and that is a rare quality.
Best
Top tip Bruce! I should have done this this morning before I implemented my photoblog archive. I have more changes to come though, so this will be nice.
@kristarella, I Love your Photoblog … well, as you know, I love your entire site. Great work. Wish I had half of your design skillz. You rawk!
Thank you much for the comment.
John, unfortunately IP exclusion doesn’t work for everyone. In my case, I have a laptop and use it at work, at home, and on the road with a Verizon card. My home and the Verizon card both have dynamic IP’s, so this is a much simpler solution than trying to keep up with which IP addresses to exclude in Analytics.
Thanks for the tip!
Brandon,
Yes, I was thinking too much from own perspective. My IP stays the same, though it could change; it just hasn’t in years. And I don’t own a laptop. One of these days I’ll have to get with the program and acquire a laptop and all the other high tech tools.
Well, I did get an iPod Nano recently, so I’m getting there. I suppose the next step is a cell phone — and then maybe a laptop
Bruce – this is a great tip. Well done. I’ll be implementing on my Thesis blog. Have you got a tip for making more hours in the day by any chance? I’m currently suffering from paralysis by analysis . . .
Liz, thank you for the feedback. And thank you for the inspiration of your site … tremendously well-designed and great content!
As to more than 24 hours in the day … hahaha … no advice on that unfortunately.
Thanks for the tip – very helpful
Lisa, I love doing this kind of thing because I get to find great blogs that I haven’t seen before, like yours. Thanks for the comment.
I’m very new to blogging, and I’ve been tweaking things a lot. As a result, my stats mostly show…me. This is a HUGE help–now I can see whether anyone is really reading my stuff. Thanks!
Hi Bruce
Thanks for the tutorial — quick query, if my blog has other writers who I also want to keep out of the stats, can I just change user level to say 2?
Presumably all readers are user level 1 or less? (I don’t have a log-in for readers at the moment).
Thanks for thoughts.
@AquaDaily,
I think it’s best to refer to http://codex.wordpress.org/Roles_and_Capabilities#level_10 to determine exactly what you want to do. I would not just substitute 2 for 10 in the above code, though, as that would let anyone who is not level 2 show up in the Google stats (including the admin, level 10). If you are sure all of your other authors are at level 2, you could modify it as follows:
if ( !current_user_can(‘level_10′) && !current_user_can(‘level_2′) )
But, if you have some who are at 3 or 4 level, you will need to modify accordingly.
For multiple user types you probably want something more like,
if ( !(current_user_can(’level_10?) || current_user_can(’level_2?)) )Basically saying “if NOT user level 10 OR user level 2″.
If you used && it might take that as being “if NOT user level 10 AND NOT user level 2″. That might work fine, or it might take it that a user has to be both level 10 and level 2 for the code not to show; no one is two user levels at the same time…
@kristarella, Good catch … definitely should use positive OR logic rather than negative AND logic. Thanks!
Thanks for your thoughts Bruce, much appreciated. I will check out the codex pages as you suggest.
Bruce, thanks for the great post.
I think the mathematicians and those who work with boolean algebra would call it a NOR statement.
Bruce, @kristarella, Actually if you make out the logic with a truth table, both statement give exactly the same result.
Zsuzsanna, cheers, yes, I’ve been finding that there are lots of ways to present conditionals like that. I like the brackets because it’s very obvious. I had a funny thin happen the other day where I had
!foo || !barand the first not cancelled out the second not, so that it was not foo, but bar. Or something like that.So, you’re saying not foo AND not bar is the same as NOT(foo OR bar)?
Pingback: links for 2009-04-01 | girliegeek
Thanks Bruce. That’s the extremely useful tip for the theme. Sometimes I connect from different places and IP filtering does not work in wy case either.
Appreciate your effort sharing the knowledge )
Plugin Ultimate Google Analytics already does this and more. You can set in panel user level of visitor to ignore, so maybe you have group site with editors. Also sets up tracking urls for downloads and external links.
Highly recommended
Hi Michael
The Ultimate Google Analytics Plugin sounds great. But if you’re using Thesis
you don’t need a Google Analytics plugin at all. That’s the point. So this edit to custom_functions.php is completely appropriate for Thesis users.
Liz
Thanks a lot for this!
I am almost completely code-ignorant and currently setting up my first blog, so stuff like this is invaluable to me.
Cheers,
Shane
Glad it helps you Shane. Best wishes with your blog
All for using less plugins however found the Google Analytics plugin from Yoast gives more control as well as being able to stop admin tracking of course.
Thanks for mentioning the Yoast’s plugin, Mark. I don’t like having a lot of plugins and many authors do not keep their plugins up-to-date, but Yoast has a good reputation, so I’ll give it a look.
Yeah I hear you Bruce – we have to be dragged ‘kicking and screaming’ to add a plugin as a general rule but I think the Yoast one is worth checking out. We’ve implemented it on only a few sites so if we see any probs with it I’ll post back here in future
Thanks for the post. Works great as I travel quite a bit and have multiple editors, IP addresses, devices, etc. I do not like loading up on plugins so your code sample was the perfect solution.