Add Google Analytics on WordPress

google-analytics-codeGoogle Analytics is a very good tool to track your website’s progress and record visitor statistics. You can improve user experience and click-through rates by analyzing where on your site your visitors are entering and exiting more often.

Analytics also shows the performance of each part of your site. It analyzes which pages load faster and which ones lag behind and are in need of some optimization.

The standard is to put your analytics code inside the head tags of your website.

<head>
    <!-- PUT ANALYTICS CODE HERE. -->
</head>

Analytics Code on Footer?

The Google Analytics Code can actually be placed anywhere on your website similarly with most javascript trackers. It’s standard practice to add it inside the head tags but some would argue that it best be placed on the footer just before the closing body tag. Supposedly, This is done to prevent the script from slowing the page. But this is google we’re talking about. I wouldn’t worry about it.

Get your Google Analytics Code

First off, lets get our google analytics code. I’ll assume you’re already signed-up with Google Analytics, if not then what’s wrong with you? Sign up here: Google Analytics

After you’ve signed-up, click Admin on your analytics dashboard. Click New Account to add your website. Fill out the form then click Get Tracking ID. Now you have your analytics code wrapped inside <script> tags.

Here are a couple of ways to add your analytics code.

Add Google Analytics Code Manually

Open your website’s file that contains the <head> tag. On WordPress, go to the theme’s file editor then open the header.php. Search for the closing head tag </head> then add the analytics code just before that. So it would look like this.

<head>
  <script type='text/javascript'>
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
    _gaq.push(['_trackPageview']);
    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
  </script>
</head>

Add Google Analytics Code Through Functions

Go to your theme’s editor then open functions.php (it’s better if you use a child-theme function file). If your functions.php is empty make sure to add a php opening tag on the very first line like this: <?php then add this code.

function add_analytics() {
?>

   <script type="text/javascript">
     var _gaq = _gaq || [];
     _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
     _gaq.push(['_trackPageview']);
     (function() {
         var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
       ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
     })();
   </script>

<?php
}
add_action( 'wp_head', 'add_analytics' );

Add Google Analytics Code with Plugins

If you’re uncomfortable with fiddling with code then you can opt for a plugin that does the job for you. This would also be ideal if you don’t use a child theme because if you edit the theme files directly to add your codes on the head area, your modifications will be lost when you update your theme.

Some WordPress themes allow users to add their own scripts through an option which makes the process a lot easier and convenient. I personally prefer this way of adding scripts. If a chosen theme doesn’t support this, my second choice would be to add the codes through a function on a child theme.

Note: Analytics are delayed by 24 hours so statistics about your site’s visitor would appear the following day.

Share This Post :

1 Comment

Add a Comment
  1. Today we released a plugin to for Google Analytics Universal (GA’s newest analytics generation). It may be useful for early adopters: http://wordpress.org/extend/plugins/ga-universal/

Leave a Reply

Anything that resembles spam is deleted. Keyword names are also changed or deleted.

Note: When posting code, enclose it in pre and code tags.
e.g. <pre><code> Add code here </code></pre>

ronangelo © 2012 - 2017 Frontier Theme
css.php