THE DIGITAL BLOG

Disabling Google Analytics for front-end Administrators in Joomla

Stop your Joomla site administrators from being tracked in Google Analytics with this simple snippet of code.

Posted on March 5, 2010 4 comments

More Articles...

A good question, raised by Jack Bremer, prompted the writing of this brief post in case it's of use to somebody else at some point (including myself).

The GA plugin in WordPress has this option, which is great for larger sites with loads of front-end admins all working on the site – you don't want to be diluting your Analytics with all that noise.

So, how can the same be acheived in Joomla? Well, quite easily actually. Read on to find out how…

Google Analytics

Modify the Template

For this to work we will need to make a simple logic change in the site's template index.php file, which you can find in template/your_template directory of your site. First we'll need to get the $user variable loaded into the template, so, near the top, add the following line of PHP:

$user =& JFactory::getUser();

This will load an array of variables into $user that we can use to determine who's currently logged-on to the front-end of your website. The particular value we're looking for is the User ID, contained in $user->gid (the current user's Group ID) – this value is zero (false) if a guest is browsing the site, but will be a specific figure depending on which group they are assigned to when logged-on.

Add the logic code

Next step is to locate your GA tracking code, which should be right at the bottom of your index.php file, just before the tag. We need to wrap this in some PHP test code to selectively load the tracking script based on the outcome of our condition, so first locate your tracking code which will look like this:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-12345678");
pageTracker._trackPageview();
} catch(err) {}</script>

Then add the PHP test to selectively load the scripts, which looks like this:

<?php if (!$user->gid == 25) : ?>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-12345678");
pageTracker._trackPageview();
} catch(err) {}</script>

<?php endif; ?>

This example above will disable the GA tracking code only for Super Administrators logged-on to the front end. You can add your own test conditions depending on your requirements, by using the list below as a reference for the group to test:

  • 18 – Registered
  • 19 – Author
  • 20 – Editor
  • 21 – Publisher
  • 23 – Manager
  • 24 – Administrator
  • 25 – Super Administrator

 

Disable tracking for anyone who isn't a guest user (not logged-on):

if (!$user) :

Disable tracking for all back-end users:

if (!$user == 23 || !$user == 24 || !$user == 25) :

Disable tracking for all registered users:

if (!$user == 18) :

I'm sure you get the gist!

Got a better idea or know of a plugin that can do this for you? Please share it in the comments, we'd love to hear your ideas!

There are 4 awesome comments...

  1. Dan Luton - March 5, 2010 at 3:43 pm

    For those looking for a solution without changing the template:

    http://extensions.joomla.org/extensions/site-management/site-analytics/3356

    It won’t be as lightweight as the above solution, but an alternative nontheless (via @JacquesRentzke).

  2. […] See more here:  Disabling Google Analytics for front-end Administrators in Joomla … […]

  3. real estate Canandaigua Lake - February 10, 2011 at 10:30 pm

    I passed this along to one of our programmer to a membership site and this has greatly improved the stats we are able to get with google analytics. Thanks for the post

  4. auto news - May 29, 2012 at 7:54 am

    Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but instead of that, this is wonderful blog. A great read. I’ll certainly be back.

Leave a comment:

I consent to Toolbox Digital collecting and storing my data from this form, as detailed in the Privacy Notice.

Previous:

Next up: