Nice asked:”How to prevent admin to be tracked by Google analytics?”

Nice asked some days ago on WpRecipes how can he’ll be able to prevent the blog admin from being tracked as a normal visitor by Google Analytics or any other tracking code. Here’s the answer to Nice question!

To achieve this recipe, we need the current_user_can() WordPress function. This function checks the given parameter, which is the level of the current user, and returns true if the level of the current user is superior or equals to the given parameter.

The following code will track any visitor or blog contributor exept the admin, if he's logged in, of course.

<?php
if (!current_user_can('level_10')){ ?>
    INSERT ANALYTICS CODE HERE
<?php } ?>

That's all! Easy, isn't it? :)

14 Responses

Nov 22 2008 11:46

Other way is to add filter in Analytics. I have static IP so simple filter by it excludes all traffic from me, even without logging in.

Nov 22 2008 12:47

@Rarst: Thanks for the tip! Though, you need a static IP adress to do this. I don’t have one, for exemple, so this trick may be useful to people with a dynamic IP adress.

Nov 22 2008 15:40

Neat trick.. now where would I need to put that code? In the header?

Nov 22 2008 15:51

Hello Remkus!
If I remeber well, Google Analytics asks you to put their code just before the &lt/body> tag. Sorry for forgetting to tell it in the recipe ;)

Nov 22 2008 18:40

I need this lol… now tracking will be “real”

Nov 22 2008 20:55

At first I thought it didn’t work. It still shows the stat code whether I’m admin or guest
but my blog is 2.3.x so I tested on WordPress 2.6.3 localhost and it works like a charm!

I tried to search codex to find if there is alternative to current_user_can(‘level_10′)
any idea to make it work with 2.3?
(please don’t blame me as I’m not quite ready to upgrade. :P )

Thanks jbj!
p.s. Nice is a girl and she giggle every time she saw your avatar. the white spot looks like (cartoon) cat’s mouth.
Sometimes she visit your twitter just to see the background for lolcat sake.
man I love your and your cat face expression in that pic lol

Nice

Nov 22 2008 21:08

@SE7EN: Didn’t knew it didn’t work on WP 2.3. Sadly, I don’t have a solution for solving that issue, but you should see if there’s isn’t a deprecated function which do the same job as current_user_can().

Glad to see that Nice loves my cat! He’s indeed a very cute boy =^o^=

Nov 23 2008 12:14

Thanks for clearing that up

Nov 26 2008 17:09

Now I know why it doesn’t work with my blog. It’s not related to WordPress version

As I plan to create another WordPress blog in another directory, I use the method mention here
http://www.quickonlinetips.com/archives/2006/03/host-wordpress-in-alternative-directory/

so when I access http://www.se7enize.com, it doesn’t work
it works fine when I access http://www.se7enize.com/nice/ – which is real WordPress address – and other single pages, category archive, etc.

p.s. (Nice is me – the same person as SE7EN, sorry for making you confused. lol :P )

Aug 14 2009 12:55

Shoud level be 10 for me or i shoud change it.

p.s. sorry for bad language.

Aug 16 2009 10:23

Does the code work with two admins?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required