600
Views

I like to use code when I have access to a WordPress child theme instead of using a plugin. That is if the theme I’m using doesn’t already support adding code to the sites <head>, <body> or <footer> tags. Many themes like Divi and Uncode will include a place to add Google Analytics code to the <head> so refer to your themes documentation to find out. If a child theme isn’t an option and your theme doesn’t support it, you can use a plugin to get the code added.

Code Option

First as mentioned above this should only be added to a child theme for your main WordPress theme. This is because any code added to a theme will be overwritten when updating the theme to a new version. Additionally you should be updating your themes to ensure any and all bug fixes and patches are applied for security and sanity.

We will be using the wp_head hook to add our code and have it load within the <head> tag of our site. You will need to update the the “G-xxxxxxxxx” with your own identifier found on your Google Analytics account.

Option 1

This option closes and re-opens the PHP tags inline which is fine to do but not everyone likes.

Option 2

This option doesn’t close the PHP tags.

No Child Theme

If your theme doesn’t offer access to the the <head> section of your site and you don’t have a child theme, then a plugin might be your only option.

A WordPress plugin like Insert Headers and Footers by WPCode will add the functionality to add code to the <head> tag. It also gives you access to the <body> and <footer> sections. While a great option it also includes a lot of potential “bloat” by including a ton of additional snippets and functionality you don’t need.

Another option would be to use a Google Analytics plugin like MonsterInsights – Google Analytics Dashboard for WordPress that connects directly with Google Analytics and offers a lot of analytic data access directly on the site. However, there is still an additional plugin now installed on the site with more potential “bloat” that you may not need.

Conclusion

In any case, getting the code added is the most important part of the process. I always recommend a child theme no matter what theme you are using. When it comes to plugins added to a WordPress site, my motto is “less is more” as each plugin added is a potential security risk and more to maintain. Don’t get me wrong, plugins are an amazing feature of the WordPress ecosystem and it wouldn’t really be what it is without them but for something as simple as this, it’s like using a sledge hammer to tap in a finishing nail.

Let us know if you have any thoughts on plugins or plugin recommendations in the comments below.

Article Tags:
·
Article Categories:
Coding
SDATIC

Web developer and former photographer by trade...gamer and all around tech enthusiast in his free time. Christoph started sdatic.com as a way to organize his ideas, research, notes and interests. He also found it enjoyable to connect and share with others who parallel his interests. Everything you see here is a resource that he found useful or interesting and he hopes that you will too.

Leave a Reply

Your email address will not be published. Required fields are marked *