Filter user specific data from Google Analytics tracking data

Published by Lennart Van Vaerenbergh on April 7, 2015

For companies in which user data has to remain private and can't be recorded anywhere else. Sometimes this even means that Google Analytics is not allowed to record user IDs or user names in for example a tracked URI. Basically, we don't want to register URIs like /user/10/notifications in which 10 is the user ID. But we do want to track visits on these pages in an anonymous way, so instead we record the URI /user/notifications (or /user/uid/notifications) by using some kind of altering or filtering. Another reason could be that we don't want to pollute our analysis with user URIs. When we have 100 different users, the tracking of a profile view page could show us 100 different URIs in Google Analytics. That's a lot of noise.
In case we don't want to track these user related pages at all, we could just exclude them in through the Google Analytics module's UI and this post wouldn't be interesting anymore.

Option 1: Add a custom filter in your GA account

The idea is that incoming data in GA such as URIs are filtered and altered before storing them.
  1. Sign in to your Google Analytics account.
  2. Select the Admin tab and navigate to the account in which you want to create the filter.
  3. In the ACCOUNT column, click All Filters.
  4. Click + New Filter.
  5. Select Create new Filter.
  6. Enter a name for the filter.
  7. Filter type: custom > Search and replace
  8. Search: ^\/user\/(\d+)\/
  9. Replace: /user/
  10. Apply filters to Views: All Web Site Data

Option 2: Alter the tracking code in your Drupal website.

Say you have multiple Drupal websites having this same privacy issue concerning GA tracking data, you could opt for a reusable module.
I will generaly explain the idea, but I have no exact code available (yet).
  1. Once google_analytics is installed, you'll need a hook which can alter the tracking code. Bummer, it doesn't exist.
  2. An effort has been made in this tread to make a patch which makes hook_google_analytics_tracker_alter() available. Yey! (didn't test it yet though..)
  3. Create a custom module that uses this hook to alter the URI before printing the javascript tracking code on you web pages. You can alter other things too like page titles and so on.

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
CAPTCHA
This challenge is for testing whether or not you are a human visitor and to prevent automated spam submissions.