Add Google Analytics Tracking Code to your WordPress Blog without a Plugin

Posted December 30, 2012 by Chris Luongo in WordPress

WordPress-logo

Plugins can introduce bugs, security issues, and degrade performance. This tech-recipe demonstrates the most simple and secure way to add Google Analytics to your WP blog.

If performing a simple task, do not compromise your site’s security by stacking plugin on-top of plugin. Adding the Google Analytics tracking code to your blog is very easy to do manually.

Since WordPress creates a dynamic site that is always changing with new content, we need a way for the tracking code to appear on every single page and post. All pages generated within WP are built from some variety of the header.php file. Thus, if we put our tracking code in this header file, we can be assured it will trigger on every view of any part of our WP site.

1. Log in to your WordPress Backend and navigate to Appearance -> Editor

Wordpress Appearance Editor menu

2. On the right of the screen you will see all the files used within the theme of your current site. We are looking for the header.php file. If you are using a custom theme, it is possible that it might not be named this exactly. This is rare, however, as most theme designers use common standards to maintain compatibility. Click the file when you find it and it will open in the editor on this page.

Wordpress File Editor

3. Take the tracking code snippet from your Google Analytics admin area. Paste it into the top of your header.php file immediately before the closing </head> tag

Wordpress editing file in dashboard

4. Click the Update File button to save this file. Refresh some of your WordPress generated pages and see if the tracking code appears in the source code.

Wordpress Update file

Remember, with Google Analytics you will not start seeing traffic info for up to 24 to 48 hours.

 

About Chris Luongo

Chris is a self-taught web designer and developer out of Atlanta, GA. This geek enjoys coffee, cold brews, bike riding, and twisting any form of tech into submission.
View more articles by Chris Luongo

The Conversation

Follow the reactions below and share your own thoughts.

  • http://www.wpsyntax.com Rick R. Duncan

    Hey Chris,

    Just want to give you a heads up that your solution will work but I’m pretty sure that when the Twenty Twelve theme gets updated by WordPress you will lose your code changes should you decide to upgrade. Even though a plugin isn’t needed in your solution I don’t believe it’s upgrade safe.

    Cheers!
    -rick

    • http://www.seelooh.com Chris Luongo

      Ah, Correct Rick! Forgot to mention that – if you decide to update the theme the code will be wiped away, so you will need to add it again. Thanks for the reminder Rick!

  • Mr Techtiplib

    I really love this article, so I’ve shared on my blog. Thanks

  • David Browne

    Chris,

    Thanks for the above. I’ve tried this before but analytics only seems to pick up visitors going directly to the homepage and doesn’t records hits going to individual pages. Do I need the code to pages also?

    • David Kirk

      @David – Typically the header file is called in every single page of the site. That’s why it is recommended to put it there. If you theme generates individual pages without using the header file, then it won’t collect that data. I’m assuming you don’t see the google code when you view the source code of those pages?

  • Fabio

    It works. Thank you.