How to Add Google Analytics Code to Your Website with PHP

The best way to track your website visitors is to use Google Analytics. Google Analytics is a simple tracking program that uses javascript to collect data about your website visitors. Once you create a profile for your website, Google will give you a unique javascript tracking code to install in your website’s footer.

You can simply copy and paste the tracking code to each page of your website, but this process is very inefficient. If Google alters your tracking code, you need to update every single page of your website! A better solution is to use a simple PHP include to call the analytics code for every instance.

Using PHP to Add Google Analytics to Your Website

  1. Create a file named “analytics.php”. Make sure the file is located in the same root directory as your website.
  2. Copy the javascript code from Google Analytics and paste it into the PHP file.
  3. Open up dreamweaver or your preferred HTML editor and paste this code in the footer BEFORE the tag:
    <?php include 'analytics.php'; ?>

For every instance of this code, your server will replace the PHP code with your Google Analytics javascript tracking code. Now you can save tons of time by editing a single file instead of multiple files with this simple PHP hack. Enjoy :)

Related Articles

Comments

Leave a Reply