Wednesday, 26 January 2011

Adding a 3rd party cookie using an image as Response.ContentType

I've been working on a bit of tracking code that can be dropped onto websites so that we can record and analyze visitor data and resolve issues regarding conversion origins, particularly regarding PPC vs affiliate traffic. There are a few techniques, but this one in particular uses .net code to perform some logic (database calls etc) prior to placing a cookie and then responding to the browser as though it is an image:
//prepare the cookie
HttpCookie trackingCookie = new HttpCookie(cookie_name, cookie_value);
 trackingCookie.Expires = DateTime.Now.AddMonths(1);

//this line is a 'compact privacy policy' for IE
HttpContext.Current.Response.AddHeader("p3p", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");   

//drop tracking cookie
 Response.Cookies.Add(trackingCookie);
//Write image
string loc = Server.MapPath("/img/track.gif");
Response.WriteFile(loc);
Response.ContentType = "image/gif";

The js code to initiate it, placed in the host website:

document.writeln("");

where the javascript value trackingData is populated by clientside code included (before the above code) like so:



This is some simple javascript that grabs the utm_source, utm_campaign (etc..) values from the URL and puts them into the value of trackingData.

Using this technique we can use specific cookie names and values to provide persistence between visitor sessions and to record and analyze their origins.

No comments:

Post a comment

Comments are moderated, so you'll have to wait a little bit before they appear!