Embed Google Analytics in WordPress

You have probably already asked yourself a few questions about Google Analytics

In this tutorial we want to show you how you can integrate Google Analytics in WordPress. The software can give you valuable information about the functions of your website and you can make informed decisions about possible improvements and optimizations based on the data evaluations.

What is Google Analytics and why do you need it?
How does the registration with Google Analytics work?
How do I integrate Google Analytics into my WordPress website?
How do I install Google Analytics with a WordPress plugin?

Well, then let’s get started and clarify the secrets in an understandable way:

What is Google Analytics and why do you need it?

If you’re an absolute newcomer, you may not yet know what that means. So you probably have no idea why you should connect it to your WordPress website? Well, then we’ll give you the right arguments for using Google Analytics.

Google Analytics helps you to get to know your target group.

Google Analytics is a web analytics software that you can use to analyze your web traffic. It is used by over 60 million websites worldwide.

You simply add a JavaScript code to your website that Google can use to track visitor activity on your website. These activities are then collected and processed and displayed in the form of statistics in your Google Analytics account.

JoeWP - WordPress Agency - Embed Google Analytics

You can see various results from the statistics:

– Who visits your site?
Geographic and technical information, where your target audience comes from and what devices they use to access your website.

– What are your visitors doing on your website?
You can see which pages your visitors are on, how high your bounce rate is (e.g. how many visitors left after just one page), how long they stayed on your website and more.

– When do visitors come to your website?
Google Analytics shows you the time (hour, day, week, month) when people come to your website.

– How do visitors find your website?
You get information about whether your target group found your website via search engines (the search engine type is displayed), social media, links to other websites or whether they entered your website address directly into the browser.

– How do visitors interact with the content of your website?
You can also use Google Analytics to understand how people interact with your website. For example, you can see which links they click on and which forms they use on your website.

Use the information to optimize your website.

Well, now you know what information you can get with Google Analytics. But what can you do best with this information? What benefit can you get from it?

You can do a lot of things with it.

– You can use the information to optimize your website in order to provide better performance to your target group. For example, you can optimize technical things on your website for a better user experience. Your users may be looking for another language that you can install now.

– You can optimize the content and content strategy of your website to give your visitors more of the information that interests them.

– You can check marketing campaigns to see if they work well.

– You can find those sites that are performing poorly and take action to improve them.

Your advantage: With web analytics, you are able to make decisions about your website based on facts and data. You are no longer dependent on pure speculation or your gut feeling. Incidentally, Google Analytics is completely free.

How to install and set up Google Analytics:

Step 1. Sign in to Google Analytics:
Before you can connect the software to your site, you need to log in to the Google Analytics homepage.

JoeWP - WordPress agency - Google Analytics registration

If you already have a Google account, sign in with it. Otherwise, create a new account.

Here you will find the process required for registration and everything is actually self-explanatory.

Once set up, click Get Tracking Number to continue. After acceptance, your approval of the Google Analytics terms of use is still required. Then you will see the page with your tracking code and your tracking ID.

JoeWP - WordPress Agency - Google Analytics Tracking Code

The next step is to add the tracking code to your WordPress site. To do this, you need either the code or the tracking ID.

Learn the basics of Google Analytics:

Before we start adding the code to your website, let’s take a quick look at the main interface of Google Analytics. This will give you a better understanding of the type of information you will find there. You can get there by clicking the Home button.

On the right you will find the statistics for your website, on the left you can choose which data should be displayed. An important part for more extensive information is the Reports tab like:

Real time:
Shows you what is currently happening on your website. How many visitors are currently present, where do they come from, what are they looking at and more.

Here you can find information that you can use to learn more about your users, such as the number of visitors in a given period, the average length of stay and the demographic and technical information.

Shows you which sources send users to your website and what information is available about your marketing channels.

Here’s everything you need to know about what your users do after they get to your website, what content they watch, and more.

If you have set goals in Google Analytics, you can see here how well you are achieving them.

Have you read everything so far?

Then you will no longer be curious about the torture and will now find out how adding Google Analytics to your WordPress site works.

Add Google Analytics to WordPress

In order for Google Analytics to track your visitors, the following code must be available on every page of your website on which you want to collect the data. Usually these are all your pages. Here’s how to set it up manually:

Alternative A:
Add the tracking code (Google Analytics code) to your website by inserting it into the header (header.php). This way it will load on every page. However, this should be done in a child theme. Otherwise the settings will be lost when the main theme is updated. So you should consider installing a child theme for future changes and additions to the files.

Alternative B:
Another way to add Google Analytics to WordPress without a plugin is to use the file: functions.php. You can do the same as above. Instead of adding the tracking code directly to header.php, you can add a function to the head area.

Just edit the file: functions.php of your child theme and add the following code (use your own tracking ID instead of the one I specified):

Funktion ns_google_analytics ( ) { ?>   
  < script async src = "https://www.googletagmanager.com/gtag/js?id=UA-123456789-1" > < / script >
  < script >
    window.dataLayer = window.dataLayer || [ ] ;
    Funktion gtag ( ) { dataLayer. push ( Argumente ) ; } 
    gtag ( 'js' , neues Datum ( ) ) ; 
    gtag ( 'config' , 'UA-123456789-1' ) ;
  < / script >
  <? php
add_action ( 'wp_head' , 'ns_google_analytics' , 10 ) ; 

Alternative C:

Use your own custom plugin.

Another way to manually add the tracking code to your site is to create your own plugin. This also gives you the option of switching tracks on and off via the WordPress backend. It also ensures that the Google Analytics tracking code stays on your website even if you change the theme.

If you have never created a plugin yourself, don’t worry, it is not very difficult!

You just create a new PHP file. To do this, use the text editor of your choice, create a new file and save it, for example: joe-google-analytics.php. Then add the following code:

Plugin Name: JoeWP Google Analytics Plugin
Plugin URI: https://joewp.com
Description: Add the Google Analytics tracking code to your site header.
Author: JoeWP
Version: 1.0

 function ns_google_analytics() { ?>
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456789-1"></script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'UA-123456789-1');
  <?php }
add_action( 'wp_head', 'ns_google_analytics', 10 );

Make sure you replace the sample tracking code (everything between [) with your own, otherwise it won’t work. You can adjust the header according to your wishes.

Now just save the file and upload it to the plugins folder of your site via FTP (wp-content -> plugins). You can also create your own subfolder. Then only activate the new plugin via the WordPress backend.

Would you prefer to use an external WordPress plug for the use of Google Analytics?

Here is the explanation e.g. for the MonsterInsights plugin:

In this tutorial we use Google Analytics for WordPress from MonsterInsights. It is the most popular plugin for this purpose with more than two million installations. It is very easy to use and comes with a module so that it is GDPR-compliant (but only in the premium version).

Well, let’s start!

1. Install the plugin
If you want to use a plugin on your WordPress website, you have to install it first. In the WordPress dashboard, simply go to Plugins>Install. You can then search for the name of the plugin (Google Analytics).

In the following list, click on “Install now”. This will download it to your site. Now it only has to be activated.

2. In the next step, connect to Google Analytics

As soon as the plugin is active, you will find a new menu item “Insights” in the dashboard of your site. Click on it to go to the Settings menu.

JoeWP - WordPress Agency - Google Analytics with MonsterInsights

Click on “Authenticate with your Google Account”. This will take you to the Google Analytics page. Here you log in or, if you are already logged in, select the account you want to use.

After logging in, Google Analytics will ask you to give the WordPress plugin permission to access your data.

Then select the profile you want to track. Then you can complete the authentication.

That’s all! You will then be directed back to your site.

2.1 Alternative method

There is another way to add Google Analytics to your WordPress site with this plugin. You can also use your tracking UA code manually. You can find your UA code as a tracking ID in your Google Analytics account. Copy the code and paste it into the corresponding field in the plugin settings. Note: With this method the MonsterInsights reports cannot be used in your WordPress backend.

3. Adjustment of the tracking settings

Once connected, you can click the Tracking tab to change how the app is tracking your visitors.

Let’s go over the most important things:

Options to ignore site users by user role and enable event tracking (click the links there)

Activate demographic and interest reports. Even more important: Here you can activate IP anonymization.

This allows you to control who can view reports and change settings on your website. You can also choose to automatically update the plugin, track usage statistics, and display announcements.

4. Review reports

The cool thing about MonsterInsights is not only that it is easy to use, but also that it pulls Google Analytics data directly into the WordPress dashboard. You can find it under Insights -> Reports.

It always takes a while for Google to display the data. Therefore, the data will only appear in your Google Analytics account after the new setup after a certain period of time. It usually takes up to 24 hours for the data to be displayed for the first time.


Google Analytics is an interesting tool that you can use to control and improve your WordPress site. It can give you a lot of useful information about visitors to your site. This allows you to optimize your website and provide your target group with a better user experience.

Integrating Google Analytics into your site is not complicated. You can do this with the manual method or do it with a WordPress plugin. The first method is certainly gentler with regard to the performance of your website, because additional plugins always bring with it an increase in the loading time. Fast loading times are an absolute “must have” for a good Google ranking: You are therefore always well advised to use as few plugins as possible on your WP installation.

JoeWP WordPress Agency - Request

Would you like us to add “Google Analytics” to your WordPress website?

Do you want to start right now? Discuss your project with us!