I’m not in the mood to record any more video tutorials at the moment so I thought I would post some development tips. I’ll start with a really basic one which I think is pretty useful and I use in one way or another whether it be a WordPress site or a CodeIgniter site. Originally I did this in CodeIgniter.
So you are developing a new site and you are in the final stages – you’ve already deployed a good working version of the site to the net but you find that you are still needing to make code changes to fix a few little things or enhance etc. So if you are anything like me you are working on a WAMP server on your PC and the site is something like http://localhost/site-name.
In the footer section of your site though you have already deployed the Google Analytics code snippet. Since I understand (and I might be wrong) that Google doesn’t filter out localhost visitors from the stats – you are ruining your stats! Your options are to manually remember to use an alternate footer.php file or manually edit the footer.php file while you are working. I did this once and then realised after a week that the last upload was the alternative footer file and didn’t include the google code. I wasn’t happy at the time as I was trying to see how my marketing efforts were going on my brand new site!
The CodeIgniter Solution
- Create a new view file called something like “google_analytics.php” and paste in just your google analytics code for your site
- In your basecontroller (recommend you create a basecontroller and extend this for your other controllers) or in your controller(s), put the following code:
// Check if we want to include google analytics code - ie; if this is production or localhost
$this->view_data['include_google_analytics'] = TRUE;
if (strlen(strstr(base_url(), 'localhost')) > 0)
{
// this is development - don't put in the google analytics code
$this->view_data['include_google_analytics'] = FALSE;
}
- In your footer.php view file – or whatever you have called yours put in the following code:
<?php
// check if to include google analytics code
if ($include_google_analytics)
{
include 'system/application/views/google_analytics.php';
}
?>
- Now this footer.php file can go back and forth from development to production and you never have to worry about removing or disabling the Google Analytics code because it will only ever activate when you are not working on localhost
The WordPress Solution
- Create a new view file called something like “google_analytics.php” and paste in just your google analytics code for your site. Put this file in your template directory.
- In your footer.php WordPress theme file, paste the following (this needs to go just before the </body> tag):
<?php
$base_url = $_SERVER['HTTP_HOST'];
if (strlen(strstr($base_url, 'localhost')) == 0)
{
// this isn't development - put in the google analytics code
include( TEMPLATEPATH . '/google_analytics.php' );
}
?>
Of course I probably could have just done the same thing in CodeIgniter but I came up with the CodeIgniter solution many moons ago – well and truly before I ever looked at WordPress.