Remove logged in userdata from WordPress via Google Tag Manager

When you are working with any kind of data, one important thing is to keep it as clean as possible from data that really doesn’t matter. Or that could twist the important data around for that matter.

No matter what kind of web analytics tool you are using, you should always make sure to exclude your own data. Reason being that what you do on your own site usually differs a lot from what your actual visitors are doing.

Today we’re going to follow up on my previous post where we installed Google Tag Manager to collect data on a site. The next natural step is making sure we clean up our own data from the collection.

Collect what we need via a dataLayer()

There are numerous ways of excluding your own data from Google Analytics, and while my friend and colleague Simo has written a good post on how to exclude your data based on IP and URLs, this post will instead focus on finding out wether or not the visitor is logged in or not and then act upon it.

The following guide applies to sites not running under WordPress too, you just need to make sure that your web developer extract the same kind of data from your site.

Did you install DuracellTomi’s GTM Plugin?

If you followed my last post, I guided you thru how to install one of the best WordPress GTM Plugins available today. If you did, this will be very easy.

Google_Tag_Manager_for_WordPress_settings_‹_dcarlbom_com_—_WordPress

Just make sure to visit the settings page for the plugin (Settings -> Google Tag Manager) and go to the tab named Basic data. What you need to do here to work the magic is to tick the box that says Logged in status.

That’s it. You are now collecting data wether or not the visitor is logged in or not and put it in the actual code of the page.

When applied, it looks something like this.

As you can see, we now have a dataLayer variable called visitorLoginState that will show either logged-in or logged-out depending on the visitor. I assume that a logged in user to your WordPress site is either an Admin or an Editor, hence their visitor data will differ a lot from an ordinary visitor. These two types of visitors act very differently on your site. You want to track the people that don’t work on your site.

Collect user logged in status without a plugin

For many reasons, you might not want to run a plugin and in that case you need to extract the data yourself from WordPress. Thankfully this is very easy, thanks to the WordPress function is_user_logged_in(). All we need to do with the data is to push it into a dataLayer for Google Tag Manager to work with.

Note: The dataLayer() must be declared and populated with data before the GTM Container loads.

The code I use in order to check wether or not a visitor is logged in or not is the following. For the sake of consistency I’ve named the variable exactly the same as the plugin does so that we can continue in the same way no matter how we collect the data.

Make sure to put these lines of code inside the <head> tag of your site, usually residing under header.php.

Take care of the dataLayer variable in GTM

At this point, all we have is a dataLayer variable containing information wether or not the visitor is logged in or not. Next step is to make sure to include this variable inside Google Tag Manager so that we can push it on to Google Analytics.

Google_Analytics - Custom DimensionsFirst of all, we need to create a Custom Dimension on Google Analytics because we need that information inside Google Tag Manager.

Start by going into the Admin section of your Property and then have a click on Custom Definitions followed by Custom Dimensions.

As you already know Google Analytics is filled with a lot of pre-defined metrics (sessions, pageviews, time on site) and dimensions (city, browser, source, etc).

The power of Google Analytics is how easy it is to implement your own custom dimensions and work with that data.

Google_Analytics Edit Custom Dimension

 

Create a new Custom Dimension and give it a proper name, so that you easily know exactly what it does when you see it in your reports. Also, make sure to change the Scope to Session.

Google_Analytics Custom Dimension Index

Take not of the Index of this Custom Dimension you just created. If it’s your first ever, chances are pretty good that its Index will be 1.

Now, back to Google Tag Manager.

Google_Tag_Manager Create Macro

 

The first thing we need to do is to create a Macro. Give the macro a unique name (the name is only used within GTM) and then change the type under Macro Type to Data Layer Variable. Then make sure that you write the Data Layer Variable Name variable exactly as its spelled, capitalizing the correct letters and all.

It will not work otherwise!

Just leave the rest to its default settings and click Save.

What just happened?

We’ve created a macro in where we tell GTM to store the information it receives thru a dataLayer in an internal variable called loggedin. Next step is to make sure to pass this data on to Google Analytics.

Now, head over to your Google Analytics Tag in GTM. If you scroll down a bit you’ll see a section called “More Settings”.

Google_Tag_Manager

 

Expand the field Custom Dimension and enter the Index number from Google Analytics in order to map it correctly. Click on Dimension and select the Macro you just created.

With this we are telling GTM to pass on the information contained in the variable to Google Analytics. As we wrote it earlier, we know that it will contain either “logged-in” or “logged-out”.

Exclude data from logged in users in Google Analytics

All_Traffic_-_Google_Analytics

 

It takes a little while until data starts to be visible in Google Analytics, but you will have a new dimension called LoggedIn that will contain information wether or not the visitor is logged in or not.

I will use this information to create a new View under the same Property in Google Analytics. But this View will have a filter that excludes all sessions that are being registered as “logged-in”.

Before Google Tag Manager you always had to push the data into Google Analytics and do the filtering from there, but nowadays you can to the initial filtering directly from GTM. How great isn’t that?

Filter out logged in users via Google Tag Manager; the new way!

The easiest way to exclude this data is to never even send it to Google Analytics in the first place. The Blocking Rules inside GTM will work our magic for this course.

Google_Tag_Manager Blocking Rules

In the sidebar to the right, just below Firing Rules (that we added before) you’ll see Blocking Rules and begin by clicking on Add.

Google_Tag_Manager Blocking Rules

 

Make sure to create a new rule and use the definitions above if you’ve followed my syntax from before. The important thing here is to select the macro {{loggedin}} and that it should contain the name we assigned to the variable in the dataLayer before; logged-in.

Click Save and publish your container.

This will make sure that the Google Analytics Tracking Code does not fire on a page if the user is logged in to WordPress (or any other CMS if you’ve applied the dataLayer manually).

Filter out logged in users via Google Analytics; the old way!

If you prefer to filter out the data within Google Analytic by setting up a new View containing only External Data this is the way to do it.

Google_Analytics New View

Start by going into the admin section and create a new view to hold this new data.

Google_Analytics Only External Filter

 

Give the View a proper name.

Google_Analytics Create New Filter

 

Then you have to go into the new View and select Filters. This is where all the magic happens, but bear in mind that all filters that you apply will alter the data collection indefinitely so be cautious!

Google_Analytics Exclude Filter

Go from the predefined to Custom filters and make sure that you select Exclude.

Under Filter Field, select the custom dimension you created before, namely LoggedIn. And under filter pattern, type in logged-in to get a match on that variable.

With this, you’ve now made sure to exclude all sessions that includes the variable “logged-in”. Have in mind that it takes some time for the new filter rules to propagate into the wild so give it an hour or so, worst case.

Congratulations, you now have a much cleaner dataset to work with inside Google Analytics.

Tell your friends...Share on Facebook6Tweet about this on TwitterShare on Google+1Share on LinkedIn9