Google Analytics And WordPress

Google Analytics is my tool of choice for tracking visits to my websites. WordPress is my CMS of choice (at the moment) for powering my websites. Google Analytics and WordPress, therefore, are a good combination for me. This piece explains how to add your Google Analytics code to your WordPress driven website.

Installing Google Analytics In WordPress

This article assumes that you know how to get your Google Analytics code to add to your WordPress installation. According to Google, you need to copy their javascript code block into every webpage you want to track immediately before the </body>  tag. As WordPress uses templates to construct your pages, it’s an easy job to find the one you need to amend and add the code in the correct place. Then the code will automatically appear on every page, and therefore track visitors to every page of your site.

In your WordPress control panel in the Appearance section, click Editor.

template-editorThis takes you to the Edit Themes page. Over on the right, underneath Theme Files, you will see a list of all the templates used to create your website. Different WordPress themes use different templates, although some are common to all themes,  so what I see in my list may not be the same as what you see in yours. We need to find the template that has the </body> tag in it so that we can insert our Google Analytics code immediately before it. As the </body> tag appears towards the end of a web page, there is a good chance that it may be found in the template called footer.php. In fact, footer.php contains the </body> tag and the </html> tag so this is the file we need to edit.

footer-fileClicking on footer.php loads its code into the code editor for us to amend. Place your cursor just before the </body> tag and press Enter a few times to create some space in which to work in. The blank lines aren’t necessary to make the javascript work, but it does make the code a littl easier to read. Copy and paste your Google Analytics code into the space preceding the </body> tag.

paste-google-analytics-codeClick Update File to apply your changes. With this mouse click you’ve just added your Analytics code to every page on your site. You can easily verify that this is the case by loading your browser with a page on your site and viewing the source. Navigate to the bottom of the source code and you should be able to see your Analytics code. To speed this process up, I usually search for the characters ‘gajshost’ as that variable name uniquely identifies the javascript code.

WordPress Themes That Handle Your Google Analytics Code

Some clever designers out there have made the task even easier (but let’s face it, the above procedure is hardly rocket science) by providing an input box in the WordPress control panel that appears if you use their WordPress theme. Woo Themes, for example, provide a theme specific options page in which you can supply your Google Analytics javascript code and this means that you don’t even need to be in the same room as a template file’s code.