How to Add Google Analytics To WordPress Website

Google Analytics is a powerful tool for tracking and analyzing your website’s traffic, helping you make informed decisions based on user behavior and engagement.

Adding Google Analytics to your website can be done in several ways, depending on your site setup and preferences. In this guide, we’ll review three methods of adding the code to your website’s header section:

Using a Plugin for WordPress

For WordPress users, installing a plugin is one of the simplest and most convenient methods to add Google Analytics. Plugins streamline the process, providing an intuitive interface to manage analytics settings without requiring any code.

First, log into your WordPress dashboard and navigate to the Plugins section. Search for a Google Analytics plugin that suits your needs.

Site Kit by Google” and “MonsterInsights” are popular choices that offer comprehensive features for tracking and reporting.

Install and activate the plugin by clicking on the respective buttons. Once activated, the plugin will typically add a new menu item in your dashboard.

Access the plugin’s settings, where you will be prompted to connect your Google Analytics account. This will involve logging into your Google account and authorizing the plugin to access your Google Analytics property data. You will need to provide your tracking ID, which you can find in your Google Analytics account under the Admin section.

Save your settings, and the plugin will handle the rest, including embedding the tracking code into your site’s header.

Adding Code in Theme Site Editor

You can also add the Google Analytics tracking code using the theme full site editor and creating HTML block in the header section of your WordPress site.

Once you obtain and copy your Google Analytics tracking code, go to your WordPress’ Site Editor. You’ll find it on the left side menu under “Appearance.”

Once you obtain and copy your Google Analytics tracking code, go to your WordPress’ Site Editor. You’ll find it on the left side menu under “Appearance.”

Modernizing Tech - Access WordPress Site Editor

In Site Editor, you’ll want to edit a pattern that appears on every page, specifically the Header pattern and add a custom HTML block with your code.

Modernizing Tech - Access WordPress Site Editor Pattern

(If Site Editor is not available for your theme, you’ll need to add a custom block within your theme’s “Customize” page if available, or with a plugin or manual code.)

Verify installation by checking the Real-Time reports in Google Analytics to ensure the code is working, or by viewing your website’s source code or using the Google Tag Assistant browser extension.

Add Code to Theme’s Functions File

You can also manually add the code to your theme’s functions.php file, allowing it to show on every page.

To find and edit the theme functions, navigate to Appearance or Tools > Theme File Editor within the WordPress dashboard, or your web host’s File Manager tool. Find and select the functions.php file from the list of theme files on the right.

Have your Google Analytics tracking code copied or handy. Add the following code snippet to the functions.php file:

function add_google_analytics() { ?>

// REPLACE THIS LINE WITH YOUR ANALYTICS CODE

<?php

}

add_action(‘wp_head’, ‘add_google_analytics’);

Replace the entire line beginning with “//” with your actual Google Analytics tracking code HTML then save your changes.

Verify Installation

As with the previous methods, check your Google Analytics dashboard, website source, or utilize the Google Tag Assistant to ensure that tracking is working.

You’ve successfully integrated Google Analytics into your website, allowing you to gain valuable insights into your site’s performance and user behavior. After verification, go through setup choosing the method that best suits your needs and website setup.