Installing Google Analytics to WordPress without a plugin

Alright this going to be a quick one cause everybody is in a rush, but there are some major Caveats to not using a plugin and hard coding it into WordPress. 

I figured that on most WordPress sites, you can just install some plugin and just hope it works and it might but if it doesn’t or breaks, then what?

The newbie and fucking wrong way to do that works (temporarily) is just add it to the head or footer of  the website. In most cases the header.php or footer.php file located in your themes folder within your wp-content folder. It’s not a good idea.

Why is this bad?, well there’s updates and I can probably guess you’ve never used WordPress before so it’s all good, well the original developer (unless its 100% custom) of the theme will bring in  a new update and might flush out all your code with their new version of the theme.  The two other popular places is in the function.php file that you hear about on internet forums and the extras.php file in your inc folder but there’s a good chance of a code flush with a new update and there goes all that shit you just did for Google Analytics and data tracking for SEO and SEM.

The safest place without plugins is inside the custom-header.php in your inc folder of your themes because that’s where most theme developers leave it unchanged however there’s always that one  chance that it might get flush but is less likely to happen than your header.php file or relying on a jenky plugin.

Finally, the most common and reliable approach is to look for a place on their theme editor with a Premium theme.  I would agree that most premium themes have it and they’ll let you place it in the scripts page or they’ll have some for of SEO section in their page,  however, there’s a lot a free themes that won’t give you shit so expect it if you’re being frugal or trying to build a WordPress theme from scratch.  Since this post was written for the purist in mind, the WordPress developers that don’t want to rely on tons of plugins, free themes, or devs that are trying to keep things simple and compact.

You can also add additional link or other styles if you want to quickly copy paste information in the header without relying on plugins. Sometimes, I’ll use the Simple Custom CSS plugin for a lot of websites because these guys are pretty intelligent and make the css editor easy to use with color warnings and other good annotations.

 

function seattle_ninja_google_analytics() { ?>
 
<link rel="author" href="https://plus.google.com/01234567890123456789?rel=author"/> 
 
<script>
 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

 ga('create', 'UA-00000000-1', 'auto');
 ga('send', 'pageview');

</script>


<?php }
add_action( 'wp_head', 'seattle_ninja_google_analytics', 10 );
 

 

Alright good luck and happy coding.

Share on FacebookShare on Google+Pin on PinterestShare on RedditTweet about this on TwitterShare on TumblrShare on LinkedInEmail this to someonePrint this page