In this short tutorial, I’m going to show you 3 quick and easy ways to install Google Analytics on your WordPress website. Before that lets first quickly set up our google analytics account. Head over to google analytics, On your analytics dashboard, click on Admin in the bottom left corner of the screen.
Click on Account, and select “Create New Account”, Here we will enter details of our website, In the Website URL field, I will be using a test website. Copy & Paste this analytics code to a text file. Now that we have our analytics ready, we will proceed to add the code to our website.
The first easiest way is, of course, the plugin method. The benefit of adding your google analytics code through a plugin is simply because, plugins are theme independent, meaning, if you plan to switch to a different WordPress theme, the analytics code will not be affected by any means.
So, I will log in to my test WordPress website. Head over Plugins and then Click Add New. In the Search field, type in google analytics. We will install the first plugin, which is Google Analytics Dashboard for WordPress, GADWP for short.
Head over to Google Analytics and Click on General Settings, Click on Authorize Plugin, Then Click on Get Access Code. You will be asked to log in to your Google Analytics account, The plugin will ask for permission to get access to your analytics account. Click on Allow. Copy the access code that you get from analytics.
Head over to your website and paste the access code. Click on Save Access code You should be able to see your website in the General Settings View. You can click on Lock Selection. Then click on Save Changes.
You can verify the analytics code by going to view site. In the frontend, right click on your site and click on View Page Source. Hit CTRL + F or CMD + F on your PC or Mac to find, and type GADWP. You should be able to see the analytics code successfully implemented
The second easy method also involves utilizing a plugin, but without having to go through access codes or authorization like that in the first method. The second method is simple copy paste job using the ‘Insert headers and footers’ plugin. So, login to your WordPress dashboard, go to Plugins and Click Add New.
In the search field, type in header and footer. Install & Activate the ‘Insert Headers and Footers Plugin’ by WPBeginner. Go to Settings and then click on Insert Headers and Footers. Here you will find two fields. We will be placing our analytics code in the header field. Copy the code from analytics and paste it into the header field. Click Save and You’re done.
To verify, we will go to our homepage. Right-click on the site and click view page source. You should see the code inside the head tags.
Finally, another way to add the analytics is to code. If you’re not interested in programming, you can use either of the first two methods and skip this part. If you’re interested in the coding method, you need to have a basic understanding of PHP and WordPress hooks.
Here we’re going to write few lines of PHP code in functions.php file in the child theme. I will start with a single line comment. Then I will create a function which will contain the analytics code.
Since we want our analytics code inside the head tag for every page, we will use the “add_action” function which will hook our custom function to a WordPress hook, which in this case is ‘wp_head’. Let’s copy our analytics code. And paste it into our custom function.
Let’s add another comment just to make sure. I have uploaded the functions.php to my web host using SFTP. Let’s verify by going to our homepage. Right click on site and click view page source. You should see the code inside the head tag along with that test comment.
A word of caution though. I wouldn’t recommend you placing your analytics code on your WordPress theme. If you switch to a different theme your analytics code will not be used. You might get a warning from Google that your site is not receiving any traffic.
If you’re testing the code for better understanding of themes and hooks, then that’s absolutely fine, but for a serious website, I highly suggest you use the first or the second method.
So, this concludes this short tutorial. I would highly appreciate your feedback and suggestions in the form of comments below.
Till then, thanks for watching.