
There are at least three solid plugins to add Google Analytics to your WordPress website, but what if you want to get your hands dirty with some code and skip a plugin for this altogether?
Before that let me mention the plugins first if you’re interested:
Google Analytics by Monster Insights
It has over a million active installs. But over 75 – 1 star reviews so that’s something to think about.
Google Analytics Dashboard for WP
Around 800,000 active installs and 22 – 1 star reviews, so maybe better, you can be the judge.
Google Analyticator
And finally the last GA plugin I’ll mention with around 400,000 active installs and a 3.6 rating in the plugin directory.
I don’t know about you but I don’t need a plugin for this kind of thing and I’ll show you just how simple it is to get working without one.
Ok let’s start
First things first: FTP into your WordPress host and navigate to your theme and then your theme’s function file. Don’t know how to do that? That’s fine, you can also log into your WordPress admin and then go to Appearance > Editor
then click theme functions.
Once there, we’ve got some code to paste. You’ll notice the UA-11111111-1
, make sure that this is your unique code that Google Analytics gives you for your website. Or better yet, GA will give you the full script and you can replace everything from the opening and closing script tags below.
/** * Add google analytics to head */ add_action( 'wp_head', 'sf_child_ga_to_head', 999 ); function sf_child_ga_to_head() { echo " <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','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-11111111-1', 'auto'); ga('send', 'pageview'); </script> "; }