Derek Dysart

Derek Dysart


Periodic notes from an independent Sitecore Developer/Architect

February 2017
M T W T F S S
« Nov    
 12345
6789101112
13141516171819
20212223242526
2728  

Categories


Derek Dysart

Disabling the Sitecore Analytics Cookie

Derek DysartDerek Dysart

A while back my colleague Nick pointed out this message on Twitter:

We’d done this for a client of ours and I thought I’d help out. Basically for this, you need to tap the ASP.NET request process and hook the HttpContent::EndRequest event. For this we created an HttpModule we specified in the web.config:

 
<configuration>
  <system.webServer>
    <modules>
      <add type="ClearCookieHttpModule, AssemblyName" name="ClearCookieHttpModule"/>
    </modules>
  </system.webServer>
</configuration>

The class then looks like this:

 
public class ClearCookieHttpModule : IHttpModule
{
        public void Dispose() { }

        public void Init(HttpApplication context)
        {
                //hook end request
                context.EndRequest += new EventHandler(OnEndRequest);
        }

        void OnEndRequest(object sender, EventArgs e)
        {
                if (clearCookies)
                {
                        HttpApplication app = (HttpApplication)sender;
                        var request = app.Request;
                      
                        foreach (var cookieName in response.Cookies.AllKeys)
                        {
                                if (cookieName == "SC_ANALYTICS_GLOBAL_COOKIE" ||
                                        "SC_ANALYTICS_SESSION_COOKIE")
                                {
                                        response.Cookies.Remove(cookie);
                                }
                        }
                }
        }
}

Basically, if the clearCookies boolean is set, we remove the Sitecore Analyitcs cookies from the cookie collection.

I originally shared this code via pastebin, but realized it probably deserved a better home (that didn’t expire.)

Independent Sitecore developer. I also blog about other stuff from time to time.