How to Add Google Analytics to WordPress for Any Themes

What is Google Analytics

Google analytics is a freemium service by Google that you can use to track traffic or users that coming into your website. The tool collects hundreds of raw data and visualizes to you in a simple way. Using Google Analytics, you can get a better understanding of your visitors that visited your website.

Why do I need Google Analytics on my website?

As an online marketer, we often make questions and assumptions to improve our marketing strategy such as

  • Which channel is driving the most visitors to our site?
  • Which page is the most visited page?
  • How long do they spend on the website?
  • What makes them leave the website?

These questions are nearly impossible to answer if your website doesn’t have any tracking services installed. Google Analytics provides more than enough data to answer those questions within minutes and you can the answer to leverage your online strategy.

Is it true that adding Google Analytics to a new website can hurt the SEO performance?

A lot of people are worried about installing Google Analytics on new website can cause a penalty or hurt the SEO performance in the long-run. This is a complete misunderstanding. I have built hundreds of clients and myself, one of the things I always do right after the website is live is install Google Analytics. There is no long-term effect in term of SEO performance in any sites. Imagine you’re running a website with no clue how your website is performing and how you will improve it? The answer is NO; Google Analytics will not hurt your new website.

Setup Google Analytics

Before you can add Google Analytics to your WordPress, you need to set up the account first.

Simply heads to Google Analytics website and click Sign up for Free

Sign in with your Google Account or click Create an account if you don’t have one.

Once you’re logged in, you will see a Start in 3 steps page. Click Sign up again

You’ll then redirect to add New Account page. Fill in your website information. 

Account Name and Website Name can be anything. The reason that they are separating these two because One Account can have multiple Website (Tracking Code). Usually I would use the same name if I’m only tracking one website on that account.

Reporting Time Zone is recommended to set according to your visitor’s timezone, that way you won’t be confused by your local time and visitor time. If you’re running an International website, just pick one that makes sense the most.

Once you have filled the form click Get Tracking ID. The Terms of Service Agreement will pop up, read and click I Accept.

Get a Google Analytics Tracking ID & Tracking Code

That’s it! You have successfully setup Google Analytics. Now you will get Tracking ID and a Tracking Code.

Tracking ID (Red Box) – Copy this tracking ID. This will be used for Plugin method and Google Tag Manager Method.

Tracking Code (Blue Box) – In this article, the Tracking code will be used if you want to install it manually.

Add Google Analytics to WordPress using Plugin

In this method, we will add Google Analytics to WordPress without touching any code using the plugin. The pros of using a plugin method are that you won’t need to re-install tracking code everytime you have changed the theme. The cons for this method is that sometimes plugin features can be a little bit overwhelming and put a load on your website. However, if your website traffic isn’t over hundreds of thousands on a daily basis. I’m sure these two plugins won’t slow your website down.

Google Analytics Dashboard for WP (GADWP) and Analytify are two plugins I recommend and use it for all my sites.

How to Setup Google Analytics Dashboard for WP (GADWP) Plugin

GADWP is an all you need a plugin for integrating Google Analytics into your WordPress. It comes with latest tracking code and fully customizable options for both regular and intermediate users. For Developer, it allows further customization, custom dimensions, and events through hooks.

Personally, I use this GADWP plugin for WPExec.

First, you need to install and active the plugin on your site.

Once you activated the plugin, there will be a Google Analytics menu added to the left menu.

Go to Google Analytics -> General Settings 

This plugin requires authorization from your Google Analytics account, and it will automatically link your web profile to WordPress. To do that and protect our Google account, we then need to generate an API that only allows GADWP read our Google Analytics data and nothing else. Click Authorize Plugin and proceed to the next step.

You will redirect to an access code input page. Click on Get Access Code as written in underlined red text.

Now you need to log in to you Google account that you have previously set up a Google Analytics account. It will tell you that GADWP asks for reading data from Google Analytics. Click Allow permissions, and then you will be given a set of character which is your API Key (keep this safe and do not give or show to anyone)Copy the API Key and come back to the Google Analytics Settings page and paste the key in the box and click Save Access Code

If your API is correct, it should be saved and show the screen as below. Verify that GADWP has selected a proper website, if not then you can choose it from the dropdown list near Select View

Click Save Changes and Done. You have linked and installed a Google Analytics tracking code on your WordPress website.

If you have any problem setting up an access code or couldn’t follow the given instructions above, try to watch the video below.

How to Setup Analytify Plugin

 

First Install and Active the Analytify plugin.

The Analytify menu will automatically add to the left menu. Click at Analytify. You will have a choice either to allow Analytify to collect data about your installation (Not your statistic data) or not allow and skip right to the integration process.

Click Connect Analytify with Google Analytics

Login with your Google Account that you have setup your Google Analytics. Then you will be ask for permissions to access the data and redirect back to the page below.

Check Install Google Analytics tracking code to automatically insert it into your theme

Make sure you have selected a collect website profile that you have previously set up in Google analytics in both Profile for posts and Profile for Dashboard

That’s it. You have added a Google Analytics to your WordPress.

Manually Add Google Analytics to Your Theme (No Plugin)

The lightest way to add Google Analyitcs to your WordPress is to do it manually with your theme. This method works with any themes.

First navigate to your current themes folder in wp-content/themes/yourtheme and add the code below to functions.php. This function will add Google Analyitcs to WordPress’s header using the wp_head hook.

/**
* How to add Google Analytics to WordPress (Any Themes)
* Full Guide - https://www.wpexec.com/add-google-analytics-wordpress
**/
add_action( 'wp_head', ‘wpexec_google_analytics’);
function wpexec_google_analytics() { ?>
    <!-- YOUR GOOGLE ANALYTICS CODE -->
<?php }

Clear the cache, refresh homepage and check the source code to verify the changes.

Add Google Analytics with Tag Manager using Plugin

At WPExec, we are a big fan of Google Tag Manager. Google Tag Manager is a service helps you manage and organize multiple tracking codes by only install one tracking code from Google Tag Manager.

You can either install it manually just like what we did with the Google Analytics tracking code or use the GADWP plugin.

Use Google Tag Manager with GADWP

Setup the plugin as usual and go to Google Analytics -> Tracking Code

Select Tracking Type as Tag Manager and insert your Web Container ID from Google Tag Manager Dashboard

Manually Add Google Analytics with Tag Manager to Your Theme (No Plugin)

You can use the same method as we did with Google Analytics to add Google Tag Manager to your WordPress theme by editing the functions.php located in your theme’s folder.

/**
* How to add Google Tag Manager to WordPress (Any Themes)
* Full Guide - https://www.wpexec.com/add-google-analytics-wordpress
**/
add_action( 'wp_head', ‘wpexec_google_tag_manager’);
function wpexec_google_tag_manager() { <?
    <!-- YOUR GOOGLE TAG MANAGER CODE -->
?><?php }
Share this content!
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •