How to add Google Analytics to a WordPress Theme

This tutorial goes over how to add Google Analytics to your WordPress theme without a plugin.

In this case, I don’t feel like it is necessary to add ANOTHER (people can get out of hand with plugins) WordPress plugin just to add some tracking code such as Google Analytics. I also like to add the tracking code with a WordPress hook to keep the header.php file as clean as possible.

Add Google Analytics to WordPress theme with Hook

  1. Create a new file in the /inc/ directory called tracking.php with the following code:


    Be sure you switch out the UA code (UA-XXXXXXXX-X) to your UA code.

  2. In the functions.php file, add a line to the new tracking.php file with the following code:

Now Google Analytics should be added to your WordPress theme!

Mentioned in the Video Tutorial