Google Analytics is one of those things that every website should have. It tracks your site visitors and gives you an idea of how well your site is performing. This is such a mandatory tool that installing Analytics on your WordPress website should really be a core function of the platform. The OceanWP WordPress theme has a ton of awesome features but an option to easily add Google Analytics code to your site is not one of them.
We are going to explore 3 ways you can get Google Analytics setup on your OceanWP WordPress site. These methods will also work well even if you are not using an OceanWP theme.
Install Google Analytics via functions.php
Installing Google Analytics via your WordPress theme’s functions.php file is probably the best way to go about it. If you are running a child theme you will want to make sure you edit the functions.php file there and not in the main theme.
Copy the following snippet of code and place it in your functions.php file. Be sure to replace the UA code that reads: “UA-5555555-5” with your own unique Google Analytics UA code.
// Add Google Analytics to Site Header
function mwx_google_analytics() { ?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-5555555-5"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-5555555-5');
</script>
<?php
}
add_action( 'wp_head', 'mwx_google_analytics', 10 );
This code will neatly place the Google Analytics tracking code in the header of your OceanWP site. If you use a child theme you don’t have to worry about this getting overwritten during updates.
Add Google Analytics to OceanWP with the Header and Footer Plugin
The Header and Footer plugin by Stefano Lissa is a no frills, easy to use plugin that does just what the title says. This is an easy way to insert your Google Analytics code into your OceanWP website without messing with child themes or functions.php files. It is simple, effective and easy to use.
Download the Header and Footer plugin here and install and activate it. After activate go to “Settings>Header and Footer” and then paste your code into the box that says “<Head> on every page”. Save it and you are done. Pretty simple, right?
Use a Google Analytics plugin
The last way to get Google Analytics working on your OceanWP website is to use a Google Analytics specific plugin. Monster Insights is one of the most popular plugins that will accomplish this and it works well. All you have to do is install, activate and paste your UA code into the plugin and it works.
The downside with Monster Insights is that they try and upsell you at nearly every step. They offer a “premium” option of the plugin which gives you more features. For most people, however, the basic Google Analytics setup is all you need. For that reason we feel that a plugin like this is overkill.
The best way to install Google Analytics on WordPress
We feel the absolute best method of installing Google Analytics on an OceanWP WordPress website is via the functions.php file of a child theme. It is simple, does not add any unnecessary bloat to your site and gets the job done.