Work around for Excluding by Cookie

Created on 13 May 2010, over 14 years ago
Updated 30 January 2023, almost 2 years ago

I'm not sure where to add this for documentation purposes. The maintainer has already indicated that this feature will not be included in the code, so this is just documenting the work around I created this work around. I don't know if it's good or bad, but it does seem to work.

The issue: Sometimes its good to exclude a few machines by cookie as well as by role, so that you can exclude their traffic even when users are not logged in. However, if you simply do what Google Analytics suggests here: https://www.google.com/support/googleanalytics/bin/answer.py?answer=55481, the Google Analytics module overwrites your exclude cookie with the user's roles. This was all noted in http://drupal.org/node/393070.

The work around: The basic idea is to create you own exclude cookie, and then using the "custom javascript" area, check to see if that's present. If it is, then set the user defined variable to the exclude value you specify in your GA filter.

First create an html page that will create your cookie. Something like this:
<body onLoad="javascript:document.cookie='drupalgaexlcudeworkaround=1;path=/;domain=[mydomain];'";>

Go to this page from each computer you want to exclude. You should be able to see the cookie appear in your cookie list.

Then, in the Google Analytics module settings, find the advanced settings/custom javascript/Code snippet (before): area and put something like this: (Note if you want this totally suppressed from your Analytics information, you could probably modify this to put it in the "Page specific tracking settings", and simply suppress tracking if your cookie is present. But I like to have it included in the traffic flowing to GA allowing me to filter and segment it there.)

//if we have excluded this computer then overwrite the "__utmv" cookie with our exclude value.
NameOfCookie = "drupalgaexlcudeworkaround"; //be sure to match you cookie name from above
if (document.cookie.length > 0){
  begin = document.cookie.indexOf(NameOfCookie+"="); 
  if (begin != -1){
    begin += NameOfCookie.length+1;
    end = document.cookie.indexOf(";", begin);
    if (end == -1){end = document.cookie.length};  
    exclude=document.cookie.substring(begin, end);  
  }
} 

if (exclude=="1"){
pageTracker._setVar("ExcludeThisComputer")};

And in Google Analytics create an exclude filter based on the user defined variable:

Filter Field: User Defined
Exclude Pattern: ExcludeThisComputer //be sure to match the _setVar value from above.

πŸ“Œ Task
Status

Fixed

Version

2.2

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States savageanne

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States w01f

    Just wondering if this workaround still works (or is necessary for this scenario) in the latest stable version (4.0.2) on Drupal 9/10?

Production build 0.71.5 2024