Google Analytics Plugin For Profiling Users

This article is about:

  • Learning what a Google Analytics plugin is and an example of how to develop one
  • The plugin will automatically look up any email address you give it and set Job Title, Interests and Gender as custom dimensions within Google Analytics so you can get a better understanding of who is signing up to your website.

Introduction

Wouldn’t it be great to know more about the people visiting your website?

Not just how many pages they viewed or how long they stayed on the site, but what they did for a living, their gender, and what topics they were interested in?

If you collect email addresses from your visitors then I’m going to show you a way to get this information, and then save it to Google Analytics for analysis. Not only that, but everything will happen in real-time automatically – you won’t have to mess about with Data Import or Dimension Widening.

By the end of this post you could have built a report like the one below.

Profiler Plugin for Google Analytics

Profile information about users within Google Analytics

Getting the data

First up, let me say that the information shown above is NOT from the standard demographic reports within Google Analytics. Those reports can show you gender, but do not show anything as specific as job title or topics.

Also note that the demographic information provided by Google is gleaned from a variety of sources (presumably websites and data stores owned by Google), but it isn’t exactly clear where.

The data I’ll be using is from Full Contact, a company that provides gathers publicly available data about people from their social media profiles. The API is free for testing purposes, and relatively low cost in production.

Reviewing the Full Contact API

The part of the Full Contact API we are interested in is the Person API. You give it an email address and it returns as much info as it has about them, including Twitter username, Facebook username, profile pic, gender, interests, job title. It is actually quite scary how much they know.

The request looks like this:

The response looks like this:

This response has many data points. In reality the volume of information returns depends upon the email address. Some have very little, some are like this example.

Requirements

What to save

For larger clients with a proper data warehousing solution it would be beneficial to store all of the information returned from the Full Contact API. But this is a tutorial about Google Analytics, so there are some limitations:

  1. Number of custom dimensions. We are going to save information to multiple custom dimensions. If you are on the free version of Google Analytics, you only get 20 slots. Therefore you can save a maximum of 20 pieces of info. But chances are you are already using some of these slots if you have a solid implementation, and will probably want to use more in the future.
  2. Personally identifiable information. Google restricts what data you can store in Google Analytics. You can’t store anything that would allow us to identify them. That means no Twitter of FaceBook handles, no real names, no email addresses.

Based on these limitations, I decided to save the following:

  1. Job title. Gives us a real insight into who the person is and what they do. Might not be useful for all types of websites, but very useful for a Google Analytics consultants company such as mine. Are people coming to my website other consultants or are they potential clients. If I see the job titles ‘Senior Analyst’ and ‘Head of Digital’ I know which I would rather talk to.
  2. Gender. Not sure how beneficial this one is for my site, but for a retail website it could be crucial.
  3. Topics. The Full Contact API provides a list of topics that the person is thought to be interested in. These include both work and leisure topics and could be useful for segmentation.

I considered tracking Organisation as well. It is entirely possible. In the end I decided not too in case Google saw it as being too intrusive. Might be OK for someone working in a large business, but if the business only had one employee the name would allow be to identify them, hence constituting PII.

How to save

I want this information to be saved whenever I get a new email address, be that via a newsletter or account creation form. When that happens, the site data layer gets populated with that email address in the user object.

If the email address exists in the data layer the call to the Full Contact API must be triggered and the data saved. However, this should only happen once per session to limit the number of calls to the API.

Implementation

I wanted this code to be easy to drop on to any site, and have therefore chosen to implement it as a Google Analytics plugin. So let’s get started.

Creating the dimensions

As already discussed, information will be set as custom dimensions. These need to be created within the Google Analytics interface. I’ve created 3: Gender, Topics and Job Title. Ignore the others, they were there already.

Profile custom dimensions

Profile custom dimensions set up in Google Analytics

Notice they are all set to Session scope, except for Gender which is User scoped. You could argue that they should all be User scoped and I wouldn’t object too much. The only reason I didn’t do this is that someone’s topics and job title might change in the future.

Sign up to Full Contact API

You need to have a Full Contact API account to use the plugin. You can sign up free here.

Writing the plugin

Now for the fun part – developing the plugin. I’m not going to go through this line by line, but have given an overview below. You will need to read the plugin documentation first in order to understand what is going on.

Save the code below in a file called profiler.js and host it on your webserver.

profiler.js is the Google Analytics Plugin code. It is responsible for creating a plugin, registering it with Google Analytics, calling the Full Contact API and setting the custom dimensions.

Here’s a run down of the important parts.

  • Line 1-42: Helper functions that deal with setting and retrieving cookies, ensuring config options are properly set, and registering the plugin with Google Analytics.
  • Line 43-59: The constructor function of the plugin. Here the plugin is initialised with any options passed in to it via the config variable.
  • Line 61-65: A debugging function for testing purposes. The debugger can be enabled by setting the debug value in the config object (more on this later).
  • Line 70-94: The run method is called from your web page. First a check is made to see if a call to the Full Contact API has already been made, done by checking for the existence of a session cookie. If no calls have been made this session the API is called. Once the API function returns the data (it happens asynchronously) the setInfo method is called.
  • Line 96-137: Sets the custom dimensions. At this point we have the data from the API. A few checks are made to ensure no blank values end up in Google Analytics, and the values for each dimensions are set on the tracker. Note how variables gender_index, topic_index and job_index are used to determine which CD slot to set. These are configuration options. Finally an event is sent to Google Analytics to set the dimensions, and a cookie is set so that no more calls are made to the API this session.

You might be wondering why an event is being used here. If you’ve read up on Google Analytics plugins you’ll know that a pageview call is not made until the plugin has finished loading, and therefore these dimensions should already be set when that pageview call is made, eradicating the need for an event.

Sadly things aren’t as simple. The plugin does not wait for the asynchronous call to the Full Contact Person API to return data before triggering the pageview. Therefore in order to get the data into GA an additional event must be sent.

Loading the plugin

Once you’ve got your profiler.js file hosted it’s time to load it. Firstly, include the profile.js script, and secondly alter your Google Analytics tag to load the plugin and set the options. The finished version will look like this:

Here’s a run down:

  1. Line 1: The profiler.js script is included.
  2. Line 10-17: The profiler plugin is created and initialised with various options. On line 18 the profiler is run. Note this must happen BEFORE the pageview call but AFTER the tracker is created.
  3. Line 11: Update with your Full Contact API key.
  4. Line 13: The email address is picked up from a data layer variable. You MUST define your data layer variable above this code.
  5. Line 18-20: This ensures the profiler is only run when a valid email is present.

Configuration Options

Options for configuring the profile plugin. These should be passed to the plugin via the config object.

  • fc_api_key. REQUIRED. Your Full Contact API key.
  • email. REQUIRED. The email address you want to look up info about.
  • gi. REQUIRED. Custom dimension slot number for Gender. Must be numeric.
  • ti. REQUIRED. Custom dimension slot number for Topics. Must be numeric.
  • ji. REQUIRED. Custom dimension slot number for Job Title. Must be numeric.
  • debug. OPTIONAL. Set to true to view logging in console window. Defaults to false if omitted.
  • refresh. OPTIONAL. Set to true to ignore cookie and force a refresh of information every page view. Defaults to false if omitted.

Conclusion

So there it is. An easy to implement Google Analytics plugin that fetches user information based on email address and stores it in GA, allowing you to perform better segmentation. If you liked the post, please share and comment below!

 

Ed Brocklebank is the founder of Metric Mogul, a digital analytics consultancy. He helps business of all sizes become more data-driven through marketing technology. He runs training for General Assembly London on Google Analytics and Google Tag Manager.