Wednesday, February 26, 2020

How to Add Google Analytics to WordPress

How to Add Google Analytics to WordPress

In this tutorial you’ll learn three ways to add Google Analytics to your WordPress website. Let’s jump right in!

Add Google Analytics to WordPress

What is Google Analytics?

Google Analytics is a free tool you can use with your Google account to track and generate reports on website traffic. It records information about website visitors, which pages they visited, for how long, from where in the world, how they reached your website in the first place, and a whole lot more besides. It even integrates with Google Ads to couple information about paid acquisition too.

It’s without question the most widely used web analytics service on the web.

How to Connect Google Analytics to WordPress

Let’s say you have a WordPress website; how would you connect Google Analytics to it? I’m going to explain a three of ways to do that, but first thing first: you need to sign up for Google Analytics using your Google account.

1. Sign up for Google Analytics

Visit Google Analytics, and (if you aren’t already) sign in using your Google Account. You’ll be shown the welcome page:

welcome to google analytics

Hit the Start Measuring or Set up for free button and you’ll be shown a screen where you can enter your account’s details.

Then select Web when asked what kind of analytics you want to use.

Lastly, you’ll be asked for the website’s name and its URL, after which you can hit Create.

Get Your Global Site Tag

Having created your analytics account, you’ll be given a Tracking ID (which will be something like UA-1111111111), plus a snippet of code called the Global Site Tag which looks like this:

2. Use the MonsterInsights WordPress Plugin

The first method, and in my opinion the easiest and best method, is to use a plugin called MonsterInsights for WordPress.

In your WordPress admin, go to Plugins > Add New and search for monsterinsights. When you’ve found it, hit Install Now, and then Activate once that’s done.

Use the MonsterInsights WordPress Plugin

Launch the MonsterInsights Setup Wizard

You’ll automatically be shown to a setup wizard where you can answer a bunch of questions to get things working, and you’ll be asked to connect MonsterInsights to your analytics account. You’ll be asked to log into your Google account, then authentication will be taken care of for you.

You’ll be given a selection of other options, but the default settings should be fine. You’ll then be shown some premium upgrades (we don’t need those for now).

You’re Done!

It usually takes around 24 hours for analytics to start pulling in data, but once that’s done you’ll have access to your reports directly from within your WordPress admin (Insights > Reports), or via Google Analytics in the normal way.

3. Use the Insert Headers and Footers WordPress Plugin

If you prefer a slightly more hands-on approach, you can use a different plugin to implement the Global Site Tag yourself. Again, go to Plugins > Add New and search for insert headers.

Use the Insert Headers and Footers WordPress Plugin

Once installed and activated, go to Settings > Insert Headers and Footers and you’ll be given two large text areas in which you can add any snippets you want.

We want to add the Global Site Tag, and if you don’t have it to hand, you can get it by visiting your Google Analytics account and going to Admin > Tracking Info > Tracking Code.

Return to your WordPress admin and paste your tracking code into the Header box on the Insert Headers and Footers page. It will now be included in the <header> of each and every page on your website, tracking user data every time its loaded.

4. Add the Global Site Tag to Your WordPress Theme Files

The next method removes yet another layer of abstraction, by adding the snippet manually to your theme files.

Option 1: Edit header.php

It’s possible to edit files from within the WordPress admin, though you should always make sure you know exactly what you’re doing to avoid breaking anything.

Go to Appearance > Theme Editor and then find header.php in the file list to the right. Select it and its contents will appear in the editor window. 

You can now paste your Global Site Tag immediately after the opening <head> tag and hit Update file. Your snippet will now be loaded into every single page on your website.

Option 2: Edit functions.php

Adding scripts like this to functions.php is often more advisable than working directly on header.php, so open functions.php in the file editor instead.

A the bottom of the file you’d add an action in order to insert the snippet, like this:

This code essentially waits for the wp_head() function to be triggered on your website (when the page loads) at which point it triggers its own function and pastes the code snippet in. The end result is much the same as the other approaches in that your Google Analytics code is added to the page head.

Caveat

It’s worth noting that using this last approach means that if you change the WordPress theme, your snippet won’t be present any more.

Conclusion

Of the three methods to add Google Analytics to WordPress discussed here, I definitely recommend using the first one; using the MonsterInsights plugin. It’s easy, there’s no coding involved, you get analytics reports directly in your WordPress admin, and it’s completely free.

Stay tuned to Tuts+ for more WordPress tutorials, courses, and free videos!


No comments:

Post a Comment