Tuesday, April 2, 2013

The Tracking Code Solution

You may already know this: When using a website visitor counter (like StatCounter, Google Analytics, etc.), and are editing the raw code on your website, a snippet of JavaScript is typically required.

But it can be a nightmare if you install the tracking code manually on every page. What happens if you have to reinstall the tracking code for some reason, and you have to go back to every page and install it? That would take a while.

The answer here is a PHP "include" function and a file with the script code. Make a little .php file that contains only the JavaScript snippet, and nothing else. Then use a code like <?php include 'filename.php' ?> to access the file (replace filename with the name of the tracking code file). Insert this code inside every page, so if for some reason you have to reinstall your JavaScript code, you can insert the JavaScript code into the .php file.

Something to note: PHP does not allow you to include a file that is in a directory different from the one with the file containing the "include" function. In short, you can't including files from other site "folders" (directories).

I hope this code tip helps. If you would like to be regularly emailed with things like my latest blog post, quick code tips, and other information, subscribe to my newsletter.

No comments:

Post a Comment

Please keep your comments appropriate.