Select Page
Written By Connor Bearse

The Correct Way To Add Google Analytics to Divi (Or Any WordPress Installation)

Last Updated: September 06, 2020

In this guide we are going to walk through how to add Google Analytics to a WordPress website using the Divi theme (or any other WordPress theme). This is the implementation I personally use at Birch River Design Group when building out a website for clients.

We are going to use Google Tag Manager to implement Google Analytics with a custom variable that check whether or not a WordPress user is logged in. If a user is logged in, Google Analytics will not track the user. This way the writers or web developers on your team will not mess with the Google Analytic numbers when working on the website.

In this guide, I am going to be using live screenshots from the website ApproachableOutdoors.com. If you’re in Colorado and love hiking, I highly recommend checking it out!

Step One:
Install Google Tag Manager on Your Website

If you do not have Google Tag Manager setup yet, create an account at https://tagmanager.google.com/

To add Google Tag manager to your Divi WordPress website we need to first grab the GTM code. In the GTM dashboard navigate to the Admin tab, and then choose “Install Google Tag Manager”:

Navigating to find Google Tag Manager's code

You will be presented with a screen that shows two sections of code. We will need to copy both of these pieces of code into your Divi WordPress website. 

Google Tag Manager's Code

In your WordPress admin dashboard, navigate to your Divi Integration settings. You can get here by clicking “Theme Options” under the Divi category on the left-hand navigation. Once you are there, click on “Integration”.

The first bit of code Google Tag Manager wants you to copy and paste will go into the first text box area labeled “Add code to the <head> of your blog”.

The second code box will go into the second text box area labeled “Add code to the <body> (good for tracking code such as Google Analytics)”.

In the image below each important item is circled in red.

Divi Theme Option's Integration Settings

Once you have pasted the code into their respective boxes, hit the save button. Google Tag Manager is now officially installed on your Divi WordPress website!

Step two:
Set Google Analytics to Trigger Only For Users Not Logged In

In this section we are going to do a few things:

  • Create a custom variable that detects whether or not a user is logged into WordPress
  • Create a custom variable that holds your Google Analytics tracking ID
  • Setup a trigger that sends a Google Analytics page view only when Google Tag Manager detects that the person loading the webpage is not logged into WordPress

Detect Whether or Not A Visitor Is Logged into WordPress

To detect whether or not a vistor is logged in we are going to create a custom variable in Google Tag Manager.

Navigate to Google Tag Manager, choose Variables on the left hand side, then choose “New” in the User-Defined Variables section: 

Creating a New GTM Variable

Name this variable “WordPress Logged In”:

Naming Your Google Tag Manager Variable

Click the “Variable Configuration” box to open the Variable Type options.  Scroll down and select “DOM Element”:

Selecting Your GTM Variable Type - DOM Element
Configure the DOM Element Variable as follows:

Selection Method: CSS Selector
Element Selector: body.logged-in
Attribute Name: class

Hit save! Let’s move onto setting up the Google Analytics trigger correctly.

Configuring the GTM Variable Dom Element

Create the Google Analytics Tracker ID Variable

If you are setting up Google Analytics through Google Tag Manager for the first time, you will need to setup a “Google Analytics Settings” variable. 

Navigate back to your Variables page and select the “New” button in your User-Defined Variables box. 

Creating Another New Google Tag Manager Variabel

Name your variable “GA Tracking ID”:

Naming Your Google Tag Manager Variable

Click the “Variable Configuration” box and choose the variable type “Google Analytics Settings” under the Utilities category:

Choosing the Google Analytics Settings Variable Type

The only setting you need to configure on this page is the “Tracking ID” box. 

Inputting Your Tracking ID into your GTM Variable

If you’re like most people, you most probably do not know your Google Analytics Tracking ID off hand. You can find it by navigating to the admin settings in Google Analytics, then choosing “Tracking Code” under the “Tracking Info” Property category. The Tracking ID will be labeled accordingly as pictured below:

Finding Your Tracking ID In Google Analytics

Go ahead and hit “Save” back on Google Tag Manger to save the variable. Great! Another step down. Let’s move onto creating the actual trigger!

Setup the Google Analytics Trigger

If you have followed the instructions above, your Google Tag Manager will have two variables setup. One which will look for a class that indicates whether or not a user is logged into WordPress. The other holds the tracking ID for your Google Analytics property.

It is now time to setup the trigger that runs checks the variable we have setup, and if a user is not logged in, sends a Page View to Google Analytics. 

Navigate to the “Tags” section of your Google Tag Manager and choose “New” in the tag box. 

Creating a new Google Tag Manager Variable

Title your trigger something descriptive. In this example I am going to title the trigger “Google Analytics Universal Analytics (WP Not Logged In)”

Naming Your GTM Trigger

Click on the white box labeled “Tag Configuration” and choose “Google Analytics: Universal Analytics”:

Choosing Your GTM Tag Type

Be sure your “Track Type” is selected as “Page View”. For “Google Analytics Settings” choose the “{{GA Tracking ID}}” we setup in a previous step. 

Configuring Your Google Analytics Universal Analytics Tag

When you have the above settings configured properly, click the white box labeled “Triggering” in the second half of the pane. This will open a list of triggers. At the top right there will be a plus sign that allows you to build a new trigger. Go ahead and click it. 

Creating a new GTM Trigger

Building the trigger properly is key here so be sure each section is filled out correctly. Please refer to the picture below to be sure you have each section filled out correctly.

Name: DOM Read - WordPress Not Logged In
Trigger Type: Page View - DOM Ready
This Trigger Fires On: Some DOM Ready Events
Setting Up A GTM Trigger Using a Variable To Check Whether Or Not a WordPress user is logged in

For the section that reads “Fire this trigger when an Event occurs and all of these conditions are true”, be sure it is configured as pictured below. 

WordPress Logged In (The variable we configured) Equals null

The logic for the when the GTM trigger should fire

Once properly configured, save your trigger, and then save the entire tag configuration. You will be back at the main tag screen in Google Tag Manager with a new tag as pictured below. 

Google Tag Manager

Step three:
Publish Your Tag Manager Configuration

With your Google Analytics Tag configured with logic to determine whether or not a user is logged into WordPress, there is only one more thing to do… make it live on the website!

Google Tag Manager changes must be published for it to show up on your website. 

Towards the top right of the Google Tag Manager screen, there will be a button that says “Submit”. Click it to start the publishing process. 

Finding The GTM Submit Button

The next window will ask you to name the update you are about to push out, and to add a detailed description. I have elected to use the following:

Version Name: GA Added
Version Description: Google Analytics added with logic to determine whether or not a WordPress user is logged in. 

Publishing Your Google Tag Manager Container

Hit the “Publish” button at the top right and your changes will be pushed to your website!

Success! You are now tracking only users who are not logged into WordPress. Your Google Analytic statistics will be super accurate as long as you are logged in while working on your website. 

TL;DR

The world moves fast. Don’t fall behind. Weekly updates on technology, digital marketing, and more from an industry professional.
"