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

How to add Google Analytics to WordPress, with or without a plugin

This guide shows you how to connect Google Analytics to WordPress using a plugin, and also by adding the code yourself.

WordPress2 min read19 steps

What is Google Analytics?

Google Analytics is a free tool that counts the people who visit your website. It tells you how many came, which pages they read and where they came from. Think of a notebook that quietly writes down each visitor.

Before you start

  1. Sign in to Google Analytics with a Google account.
  2. Create an account and a property. A property is Google's name for one website you track.
  3. Create a web data stream and type in your website address.
  4. Copy the measurement ID. It looks like G-XXXXXXXXXX.

Method 1: With a plugin

A plugin is an add-on that gives WordPress new features. You do not touch any code.

  1. Log in to your WordPress dashboard.
  2. Go to Plugins, then Add New.
  3. Search for Site Kit by Google or another Analytics plugin you like.
  4. Click Install Now, then Activate.
  5. Follow the setup wizard and sign in with your Google account.
  6. Pick the property you created, or paste the measurement ID if it asks.

Method 2: Without a plugin

Here you place the tracking code in your site's header yourself. The header is the top section of every page. Google gives you a small block of code called the global site tag. It starts with a script line that loads from Google.

Warning: editing theme files can break your site if you make a typing mistake. Take a full backup first.

  1. Copy the tag from the data stream settings in Google Analytics. Look for the option to view tag instructions.
  2. In WordPress, go to Appearance, then Theme File Editor.
  3. Choose your active theme and open header.php.
  4. Find the closing </head> tag.
  5. Paste the tag on the line just above it.
  6. Click Update File.

Another way is to add the code with a function in your child theme's functions.php. A child theme is a copy of your theme that keeps your changes when the main theme updates. Use a child theme if you can, because a normal theme update erases edits in header.php.

Test it

  1. Open your website in a private browser window.
  2. In Google Analytics, go to the Realtime report.
  3. Check that you see one active user.

Data in other reports can take a day to show up. That is normal.

Tip: Do not use both methods together. You would count every visit twice.

Quick recap

  • Create a property in Google Analytics and copy the measurement ID.
  • With a plugin: install, activate and follow the wizard.
  • Without a plugin: paste the tag before </head> in header.php, after a backup.
  • Check the Realtime report to confirm it works.