[eluser]tadtoad[/eluser]
[edit... I think i put this in the wrong section]
Hi,
I just thought I'd share this little piece of code. I usually add tracking/analytic scripts to my code as one of the last things before launch. This is to prevent tainting the stats with my localhost testing.
THE PROBLEM: Even after successfully uploading and testing a site after uploading, I usually have little things here and there that I need to edit. The problem is, I sometimes overwrite the live file that contains the tracking code with my localhost version(no tracking code for reason stated above). I usually catch my mistake and re-add the tracking code... but not this time. I lost launch day's analytics because of this... all that advertising and buzz wasted! (yes, i should have backed up... but it won't have made a difference in this case)
MY SOLUTION: Create a way for my tracking code to be added to my localhost without messing with stats or being overwritten. Now, you could easily disable tracking for your browser if you use Statcounter (maybe others too), but when you're testing on multiple browsers and devices it can be a pain.
STEPS
1. Create a folder in you Views folder called "trackers".
2. Inside this folder, create 2 files named: header-scripts.php, footer-scripts.php. Header-scripts.php is for trackers that go in the header of your site, like Google Analytics. Footer-scripts.php is for footer scripts.
3. Paste your trackers in their respective files.
4. Back in your website code, add the following to where you want your code to appear:
Code:
<?php
if(ENVIRONMENT == "production"){
$this->view('trackers/footer-script'); // change to header-script for header
}
?>
5. "ENVIRONMENT" is already defined in your main index.php file. When your website is ready for public use, just change value in the uploaded index.php from
Code:
define('ENVIRONMENT', 'development');
to
Code:
define('ENVIRONMENT', 'production');
. You should already be doing this in the first place though.
6. That's it! Now you can upload/overwrite view files without losing your tracking code. This works for me because I don't have any need to edit any other file outside of controllers,models, views and images.
I hope this helps someone out.
If you have any modifications or see any holes in this method just let me know!