A pet peeve of mine — and yes I’m also guilty of it sometimes — is JavaScript on a web page.  There are a number of reasons not to do this, the most important is the SEO benefit of  performance boost of external JavaScript files.  As separate files these resources can be cached by the web browser, allowing for faster loading of pages on your website.  The initial hit on the site will have more HTTP requests, but subsequent page loads will result in less HTTP requests and thus faster load times.

There is some argument to and for this, and I’m not going to get into all that, but for the most part this technique is something that can be done to increase web site performance.

Bring in Google Analytics

I use Google Analytics for statistical dissection on most if not all web sites.  This is a free tool, and there is nothing wrong with Google seeing you using their tools on your site when the Googlebot visits.  Most sites that I work on are also developed on the WordPress platform, so this code is based on what I normally do.

Create a file called google-analytics.js and place this in the scripts folder in my WP theme

Place the following code into your theme’s functions.phpfile:

if(!is_admin()){
$themeURL = get_bloginfo(‘template_directory’);
$gafilepath = $themeURL.’/scripts/google.analytics.js’;
wp_register_script(‘google-analytics’, ($gafilepath), false, ”);
wp_enqueue_script(‘google-analytics’)
;}

The first two lines are simply setting the location of the file, and can be changed based on how you are doing things, but are included for the purposes of this example.

What does this do?

This code, being in the theme’s functions.php will run when the page request is made, and will fire off when the request is made to a non-admin page — !is_admin() — and the wp_register_script() and wp_enqueue_script() functions will safely register the script for use within WordPress and then safely output the reference to the script in either the <head> or in your site footer.