How to set up Google Analytics in WordPress

If you just created your WordPress website, you might want to track your visitors. But do you know how to set up google analytics in WordPress?

The Google Analytics module has become a standard nowadays because it provides useful information such as the number of users who visited a certain article, how much time they spent on the page, demographic information, etc.?

A Google Account is required to connect a site created in WordPress to the Google Analytics service. The Google Analytics service will generate a code for you that must be integrated into WordPress to record the information. In this tutorial, you will learn how to integrate Google Analytics into your WordPress site.

In 2020, Google announced the new Google Analytics 4, which offers a great user interface and many new features and user experience improvements. Below, I will show you how to create and install your Google Analytics tracking code and cover some of the new features implemented into GA4.

How to add a property to Google Analytics?

A Google Analytics account is required to add a property to Google Analytics. If you already have a Google Analytics account, you can skip the step below.

You do not have a Google Analytics account.

If you don’t already have a Google Analytics account, the first step is to create one. For this, you need to go to the Google Analytics website and click on the “Create Account” link and select one of the two options: for myself or to manage my business.

In the next window, we enter all the data requested by Google.

After completing the required fields, click on “Next” to go to the next step. In the next window, Google will ask you to confirm your email address.

Check your email and click on the link sent by Google. You will immediately be redirected to the account confirmation page. From this page, go to the Google Analytics dashboard.

You have a Google Analytics account.

If you already have a Google Account or have just created one for the next step, it is now necessary to add the website to your Google Analytics account and obtain the traffic tracking code.

The next step is to set up the service and get the traffic tracking code. The first thing you need to do is enter the account name. It is best to have the site name for your convenience to know which site you are tracking with this account.

Below, you need to check what data you want to share with other Google services. You can check or uncheck according to your preferences.

The next thing you need to do is add the property (website), choose the desired reporting timezone, and choose the appropriate currency. The currency is chosen to be able to track various actions in the case of eCommerce sites.

The last step is adding ownership in defining the details of the business. You have to select the niche, the number of employees, and how you want to use Google Analytics (you can check several options).

The Google Analytics account was created, and the property was added. Now all you have to do is copy the tracking code from the middle of the display and integrate it into your site. I will describe this in the next part of the article.

Obtaining the Google Tracking Analytics Code.

To connect Google Analytics to your site, you need to get the Google Analytics tracking code. You will find the tracking code in the menu Admin / Property / Property settings / Setup Assistant / Tag Installation.

If you use a plugin and need the property ID only, you can get it from the Admin / Property / Property settings section.

How to set up Google Analytics in WordPress?

There are several methods through which you can implement your Google Analytics code into your WordPress website.

Using a plugin

There are several plugins out there that you can use to add your analytics code to your WordPress website. Some of them offer detailed reports, while others add the tracking code to your website.

Here is a list of my favorite analytics plugins:

Using the functions.php file

You can add your Google Analytics code to your WordPress website by adding the code below to your functions.php file. Please make sure that you know what you are doing because working with PHP can cause fatal errors to your website.

Where do I put the Google Analytics code?

To access the functions.php file, you have to log in to your admin backend and go to the Appearance / Theme Editor section. From the right side of the screen, select the functions.php file (Theme Functions).

If you don’t have any custom functions implemented, your functions.php file should be empty or have a few commented lines.

add_action( 'wp_head', 'my_google_analytics', 20 );
function my_google_analytics() { ?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="*******************"></script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '*******************');

You can copy and paste the code above and replace the * symbols with your Google Analytics Property ID.

You can also replace the entire script code starting from the <!– to the </script> and add the code you get from Google Analytics.

Once you are done, click the Update File and make sure you don’t get any errors on your website.

If you visit your website, you can track your visit to your Google Analytics Real-Time dashboard. If your visit shows up, then you are all set.


If you have a WordPress or any website, you most likely want to track your audience and see how many people visit your website.

For WordPress websites, this can be slightly different from a static website that does not use a CMS. This step-by-step guide will help you learn how to set up Google Analytics in WordPress.

Blogger at and Founder of - I write about startups, SEO, development, and design.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store