Podcast Episode

463 – Create a Simple Plugin to Embed Google Analytics Code


Is there a plugin for that?

With more than 50,000 plugins in the WordPress repository, it’s hard to find the perfect one. Each week, I will highlight an interesting plugin form the repository.

For more great plugins, download my 50 Most Useful Plugins eBook.

Slides is a tool that will help you create slides for your next presentation.

Create a Simple Plugin to Embed Google Analytics Code

This plugin can be used to add any type of tracking pixel to your site or any content that you want to appear in the header tag of every page on your website.

What You will Need

  • WordPress
  • Access to your site via FTP
  • Code or text editor

Here’s the code necessary to create a plugin:

Plugin Name: Simple Google Analytics Plugin
Plugin URI: https://YourWebsiteEngineer.com
Description: Adds a Google analytics tracking code to the <head> of your theme.
Author: Dustin Hartzler
Version: 1.0

And the full plugin can be found here:

Plugin Name: Simple Google Analytics Plugin
Plugin URI: https://YourWebsiteEngineer.com
Description: Adds a Google analytics tracking code to the <head> of your theme.
Author: Dustin Hartzler
Version: 1.0

function dh_google_analytics() { ?> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-XX"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-XXXXXXXX-XX'); </script> <?php } add_action( 'wp_head', 'dh_google_analytics', 10 );


Once you add this file to your wp-content/plugins folder on your server, you can activate the plugin in the plugin section of your WordPress Dashboard.

Congratulations! Now you know how to create a plugin for Google Analytics and WordPress.

Thank You!

Thank you to those who use my affiliate links. As you know I make a small commission when someone uses my link and I want to say thank you to the following people. For all my recommended resources, go to my Resources Page

Full Transcript

Business Transcription is provided by GMR Transcription.

On today’s episode, we are going to talk about a super simple plugin that you can create today to add some sort of tracking code to your website. We’ll talk about how to do Google Analytics today, but it could be used for Facebook and so much more. Right here on Your Website Engineer podcast, episode No. 463.

Hello and welcome to another episode of Your Website Engineer podcast. My name is Dustin Hartzler and I’m excited to be here with you today, because as always, we are here talking about WordPress. The things that are happening in the WordPress base and some of the cool things you can do with WordPress. I’m just really excited to share it with you today a simple plugin and this is one that each and every one of us can create when – if you just need to add like I said, tracking code or tracking information to a specific website and just showing the overall basics of setting up a plugin. I’ve talked about this in years past, I mean, this podcast has been going on for almost 10 years. So, I do have to recycle topics every once in a while.

This one came up to me, because I was trying to figure out when my wife was – when we were working on her website, how to add a Facebook tracking pixel to her website. Then I started thinking about it and I was like, “Hey, how could I do this, what would make it easier?” Then just to make it clean and simple. Some people don’t use the Facebook tracking pixel, but a lot of people like to see Google Analytics. So, I thought we would talk about Google Analytics and we’ll do that in just a minute.

The first thing I wanna talk about is two different releases of WordPress that are coming up and that have come up in the past. The first one is WordPress 5.2.4 security release is now available. This was released just yesterday or a couple days ago, October 14th and it is a new version that you can upgrade from 5.2.3 and it had some bugs that were fixed. There was some security updates, it’s mainly a security release so there was one, two, three, four, five, six, seven different security features that have been released. All of the reporters did a private disclosing of the vulnerabilities, which gave them time to fix the attack or fix them before WordPress could be attacked.

So, it’s a short cycle security release and it is urgent for everyone to update all of their sites to WordPress 5.2.4. The other thing that I want to share today is about WordPress 5.3, the next version of WordPress, the release candidate version is available. What does release candidate mean? This means that everything has been tested and no more features are going to added. I guess, I shouldn’t say everything’s been tested, everything’s gone through it’s initial test. All of the new features have been added, all of the code has been written.

Now it’s just a matter of the time to go in and make sure that they’re testing things and they’re making sure they tweak bugs. Fix anything that could have gone array when it comes to the new features of WordPress 5.3. Like, I said in the last couple episode when we talked about the update that this now includes a refinements to the block editor that was released in 5.0. It has new blocks, there are new intuitive interactions, improved accessibility. This also includes the 2020 theme, giving users more design flexibility integration with the block editor.

So, it you are a plugin developer or a theme developer or anyone in the sorts, or even have some sort of software as a service. I recommend going in and downloading the Beta, working through that and making sure that your plugin works perfectly with the latest version of code. You can go to the – you can just download it from the WordPress repository, the latest version or you can use the plugin, the Beta tester plugin and then turn on the bleeding edge nightlies. That will give you the latest and greatest features when it comes to WordPress 5.3.

All right, the plugin that I want to share with you this week is a plugin that you probably won’t use on any active site. It’s more of a tool that you can use inside of WordPress and this is called Slides and you can find it by searching for Slides in the WordPress repository. But it basically creates a custom post type as a presentation, and then it renders as a presentation with the fun of using reveal.js. You might have seen this before with some of the developers or someone that’s really into giving presentations instead of using PowerPoint or Keno by Apple, they’ve used some sort of online system. Where they just can arrow back and forth between slides or up and down and whatnot. That is what Slides is doing and this is their reveal.js.

So, basically you can go in like a WordPress post or page and you can go and create a template or a presentation. From there, you can go ahead and reveal that, and it will make that page or post turn out to be like a full-blown presentation. You can import fonts by using the @import, you can site background colors, you can do some slide notes with every slide and there’s more options coming. It’s brand new, it’s something that has just been released, but it’s someone that is using WordPress in a different way.

I think it’s really exciting, it’s something interesting to think about. So, that’s something that we will have to keep and eye on and make sure that’s not just running websites, but creating presentation with WordPress, the possibilities are limitless. All right today let’s go ahead and dive in and talk a little bit about creating a sample or a simple WordPress plugin. WordPress plugins are very, very simple, they just take a couple lines of code. You can open up a text editor on your website or on your computer or you can use a full-blow IDE, which is a development environment for – integrated development environment for creating plugins and all kinds of things, software development and whatnot.

But today we’re gonna talk about some, a little bit about how to add some sort of tracking code to your website. So, this could be done with Google Analytics on like I said, Facebook. If you want to put a Facebook tracking pixel, maybe you need some sort of fonts in Google Fonts. If you have some sort of code that could run on every single page then this is the podcast I guess, for you. You could create this simple plugin and then that will run the same code in the header part of your website.

Instead of going in, you don’t have to do it old school ways, when it said add your Google tracking code and you had a website that had 50 pages. You had to go and edit every one of the 50 pages to make sure that tracking code was on every single page. Now we can do it with a simple plugin. So, that’s what we’re gonna talk about today, you need a couple things to get going. You need a WordPress installation running any theme that you like; you need access to your site with via FTP or you can do it right from a local installation. Maybe you’re using Local by Flywheel or some sort of other software to run a local configuration and you need some sort of text editor or code editor. Those are the three things that you need and it’s pretty simple to get started from there.

So, you want to – basically kind of the step-by-step process is we’re gonna just make sure that our theme has an active wp_head hook. If you’re using a theme in the WordPress repository that’s absolutely there. If you’re using a theme by a premium company, maybe like Divi or Elementor or something like that, it’s going to be there. It’s only if you’ve created your own theme and maybe you didn’t include the wp_head function. But otherwise, almost every single theme has that. We’re gonna create a new file in your plugins folder then we’re gonna get the analytics code from our Google Analytics site. We’ll add some code in the tracking code, we’ll add that into the plugin, then we’ll activate the plugin and you’ll be good to go.

So, in order to check to make sure that you have the wp_head hook inside your WordPress theme is you just have to go into the header.php file. You’ll find this under wp-content/themes/ and then the name of your theme. Then in there, there will be a header content or a header.php page. As long as you can go and you scroll all the way to the second to last line in that file or close to the bottom of it, I guess. It’s right before the closing of the header tag and it’ll say usually, php wp_head();. So, you just want to make sure that you’re looking for that, most the time this is just a cautionary, a precaution just to make sure that you’re not gonna run into any issues later.

If you remember a few weeks ago when I was talking about the theme that I developed for my wife, I actually forgot this. All kinds of things weren’t working properly and that was the exact reason, the wp_head function was not there. Okay, so once we know that that’s there, the next that we’re gonna wanna do is we’re gonna want to go into our Google Analytics account and we’re going to get the tracking code. You can click along there, it’s not super intuitive to find, but you basically are looking for your tracking code.

It is going to be giving you some sort of script, it’s going to be wrapped in script tags and it’s going to have maybe six, eight, 10 lines worth of code. You can copy this; it basically has a ua-something and that’s the unique identifier. You grab everything in that script and then you’re copy that and we’re gonna use that in just a little bit.

Next, we’re gonna wanna make a plugin and making a plugin is pretty simple. We’ll navigate into the plugins folders. So, before we were in the themes folder under wp-content, now we want to go into the plugins folder under wp-content. Then there is a little bit of code that you can add, and I’ll put this in the show notes, so you can just copy and paste it. I’ll also include a full plugin with all the code just so you can see what that is. But what you do is just paste in a couple lines that basically is telling WordPress the name of the plugin. We’re telling it the author of the plugin; we’re telling them the version number of the plugin. It’s basically a comment section at the top of the header and it’s just giving us the information so that WordPress knows that it’s a plugin.

Then once that’s there, we’ll add a little bit of code, we’ll create a function. Maybe it’s just called Dustins_Google_Analytics function, you name it whatever you like. Then you do what’s called add an action to it. Adding an action will allow it to tie into something that’s loading inside of WordPress, that’s kinda the basic way to put it. So, we’ll add an action to whenever it sees the wp_head it is going to run this function. As soon as that happens then it’s going to run, it’s going to add all of the code that we’ve previously added with the UA code. Then that’s all we need to do; we just need to paste that code inside of the function itself.

Again, you can see what this function looks like right inside of the show notes for episode No. 463. Now, you can save your plugin file. Again, you can do this locally or you can do this right on your server. You will see the name of the plugin, whatever you gave the name, maybe you called it simple Google Analytics plugin or Dustin’s Google Analytics plugin or whatever. You’ll see that plugin show up right there in your WordPress dashboard under plugins. Then we can activate it and we can go ahead and then we can refresh our page and we can view the source code. We’ll see that it’s actually there.

Sometimes you might have to go into Google Analytics and verify that the code has been put into the right place. You can go ahead, and you can do that on the Google Analytics side, because now all the code is running. It’s in the right place and it’s doing what it needs to. So, why would you want to do this? Sometimes why don’t you just go out there and install the Google Analytics Plus plugin or Monsterinsights for Google Analytics? Sometimes you might wanna just to keep things simple or just insert only the tracking code and so everything else on Google Analytics. Maybe you don’t want to have a plugin that’s got 100 features, when you just need this simple tracking code. Maybe it’s a simple site and you’re not really gonna do anything, but you just wanna see how many people show up to the site. That would be a reason for a simple plugin like this.

Let’s see, what else? The other cool part that you can do. So, we’ve put this plugin inside of the wp-content/plugins folder and there’s another section inside of WordPress that is called the Must Use plugins area. It’s not there by default, but you have to create the folder itself. So, inside of wp-content you’ll want to create a new folder called mu-plugins and then you can add your code right to there. I like doing this once I verify that it works, you wanna make sure. If you’re testing it you wanna be able to activate and deactivate, activate and deactivate, make sure the toggle works, make everything else works perfectly.

Then once you’re happy with, “Okay, Google recognizes the codes there. I can see it when I'm looking at the source code for the website.” Once you’ve verified all of those things, then you can put it in the Must Use folder. You can just take that file; you can move it from plugins into the mu-plugins folder. What that does is that’s going to make it a Must Use plugin, which means that it always is used within WordPress itself. That it can never be deactivated, so this is really good for clients. So, if you want to, to make sure that your client never turns off a plugin this is the way you would do it. You would put it in the mu-plugin folder and then it’s always on, it can never be turned off and it’s going to continue to run.

The other good thing about making into a plugin, you could go into maybe your theme settings and add your Google Analytics codes there. But what happens if you change themes some day? The themes are changed and now all of a sudden you forgot to add the code back. It’s best to have something like this, which is a tracking piece of software. We wanna make sure that is not theme dependent. So, when you change themes someday or you do some troubleshooting and you have to change themes, or whatever that looks like in the future. You don’t have to worry about losing that Google Analytics code, it’s going to continue to load, it’s going to continue to work. It just exactly as you like, even if you change themes, even if you monkey around with some of the settings and make some changes there.

So, that’s what I wanted to share with you today, just a simple plugin. Again, go to the show notes and you can see all of the details there. You can see exactly what the plugin is, it’s a super easy plugin. You can even just copy it and then download it and install it on your WordPress site. Basically, if you take it from the show notes you just grab it and put it in text at it and save it as a .m or save it as a .php file. Then you can upload that to WordPress, and you might have to put it in a folder, and you might have to zip it up to upload it at a plugin. But or you can go ahead and use FTP or move it into the folder itself if you have a local installation of WordPress. So, that’s what I wanted to share with you this week, just a simple step.

Again, use it for a Facebook tracking pixel, use it for Google Analytics, use it for Google Fonts. Use it for anything that you need some sort of external URL or external source or something to load on every single page. That’s what I would use it for and that’s what I use it for on my website. That’s all I got for you this week, take care and we’ll talk again soon, bye-bye. For more great WordPress information, head on over to yourwebsitewngineer.com