Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Three ways to add Google Analytics to WordPress

This guide shows three ways to connect Google Analytics to your WordPress website, so you can see how many people visit it.

WordPress2 min read19 steps

What is Google Analytics?

Google Analytics is a free tool from Google. It counts the visitors on your website and shows where they come from and which pages they read. It works like a people counter at a shop door. To use it, you add a small piece of code to your site. Google gives you this code, or an ID that starts with G-, when you create a property. A property is Google's name for one tracked website.

First, sign in at Google Analytics with your Google account, create a property for your site, and copy the measurement ID. It looks like G-XXXXXXXXXX. Google's screens change over time, so look for the data stream details of your website.

Way 1: Use a plugin

A plugin is an add-on that gives WordPress new features. This is the easiest way.

  1. In your WordPress dashboard, go to Plugins, then Add New.
  2. Search for a Google Analytics plugin, such as Site Kit by Google or MonsterInsights.
  3. Click Install Now, then Activate.
  4. Follow the setup screen and sign in to your Google account.
  5. Choose your property, or paste your measurement ID when asked.

Way 2: Add the code to your theme header

A theme controls how your site looks. The header is the top part of every page. Placing the code there makes it load on every page.

  1. Take a backup of your site first. A mistake in theme files can break your site.
  2. Copy the tracking code from Google Analytics.
  3. Go to Appearance, then Theme File Editor. On a block theme this menu may be called Editor and work differently.
  4. Open the file header.php.
  5. Paste the code just before the closing </head> tag.
  6. Click Update File.

If you update your theme, this change can disappear. A child theme avoids this problem. A child theme is a small copy of your theme that keeps your own changes safe.

This is a middle path. It keeps your code safe when the theme updates, and you do not edit files.

  1. Go to Plugins, then Add New.
  2. Search for a plugin such as WPCode (Insert Headers and Footers). Install and activate it.
  3. Open the plugin's settings and find the Header box.
  4. Paste your Google Analytics code there.
  5. Click Save.

Check that it works

  1. Open your website in a new browser window.
  2. In Google Analytics, open the Realtime report.
  3. You should see yourself as one active visitor within a minute or so.
Tip: Use only one method. If you add the code twice, every visit is counted twice.

Quick recap

  • Google Analytics counts visitors. You connect it with a code or a measurement ID.
  • Way 1: an Analytics plugin. Easiest.
  • Way 2: paste the code in header.php. Back up first.
  • Way 3: a header and footer plugin. Safe from theme updates.
  • Test in the Realtime report, and use only one method.