How ClickTracks Uses Cookies
   

clip0027 To use cookies within ClickTracks you must both set the cookie within your website code and record the cookie within the logfile.


If the site sets a persistent cookie then simple new-versus-returning comparisons of visitors can be made. Analysis of visitors with specific cookie values is also possible.

ClickTracks Professional Client

Custom session cookies are supported.

Persistent cookie database tracks unique visitors and extends campaign and conversion tracking to work across long periods of time (latent conversions). See Cookie Tracking



What is a cookie?

A cookie is a piece of information that gets sent to a user's browser by a website. Cookies are typically used to store user information like name, address, online buying patterns, login details, and so on. The website can then use this information to provide useful information as the user surfs through the websites. Typically a session cookie is used only throughout the duration of the user's visit (or session) on the website and is removed when the user leaves. However, there is a second variety of cookie, called a persistent cookie. A persistent cookie is similar to a session cookie except a physical text file is written to the user's machine and stays there for a website-defined amount of time. This is quite useful: Now the same information that was stored before can be accessed every time the user comes back to the website for multiple visits.

How do cookies work with ClickTracks?

Cookies can be very useful in helping you get the most out of your ClickTracks analysis. Two applications of cookies are calculating unique visitors and tracking delayed conversions.

The first application for cookies is calculating unique visitors. By default, ClickTracks deals in visits when running its analysis. This means that the same visitor can come to a website multiple times and will be tracked as a separate visitor each time. Often it is important to see how many distinct (unique) visitors are coming to your website. The only way to know if a visitor has come to your site before (and therefore if the visitor is unique) is to set a cookie for each visitor who comes to your site.

The second application of cookies is tracking delayed conversions. A typical scenario in web marketing is to find out how many visitors who come to your website as a result of pay-per-click (PPC) actually convert into a sale. If a visitor who clicks on a PPC ad that leads to your site purchases within the same visitor session, ClickTracks will log the visitor as a conversion and calculate the numbers with no problem. However, the reality is that many times a visitor will reach your site as a result of a PPC ad, leave your site, think about it for a while, and then come back some days later to purchase. Because the click on the PPC ad and the purchase happened across two visitor sessions, there is no way to tie these two operations together and log a conversion in ClickTracks. The only way to accurately tie these two sessions together is to set a cookie for the visitor. Thus, every time the visitor comes to your site, ClickTracks will treat him or her as a unique visitor and will log the conversion, because it's almost as if the PPC ad click and the purchase happened in the same session, even though this session lasted many days.

How Do I Set Cookies for My Website Visitors?


For ClickTracks to track cookies, we require that you set a persistent cookie for all your visitors. When you set this cookie, you should set its expiry date to never expire. Cookies are set using either your server technology (PHP, ASP, etc.) or your client-side technology (JavaScript, VBScript, etc.). Cookies are usually set using the following name/value scheme:

Name = value

Each cookie that you set must be unique for every visitor. You can come up with any kind of convention that you like. One example is to use a Timestamp followed by a random string. So, for example:

MYCOOKIE=06012004ABCD

This is a sufficient example of a unique value because it is almost impossible to have two requests logged at the same time. Even if this does happens, adding another random string to the end of the value further ensures its uniqueness.

The code that you write on your website would do the following:

1.When the visitor first comes to your website, a persistent cookie is set on the visitor's machine.  
2.This cookie need only be set once. As long as the cookie's expiry date is set to never expire, this cookie will stay with the visitor's machine forever. When a visitor returns to your site, your code should check the visitor's machine for the presence of your cookie and should not set another one if a cookie already exists. The cookie should never be set twice for any given visitor.  
3.You must make sure that this cookie is also getting logged in your web server logs. Please see the help documents in the ClickTracks application on how to configure your web server to log the proper logfile format.  

How Do I Implement Cookies?

There are a variety of ways to implement cookies on your website. It all depends on the website technology you are using. The following are some sample implementations using ASP, PHP, and JavaScript:


ASP (Active Server Pages) - Sample Code

If(Response.Cookies(strCookieName) =  "")
   Response.Cookies(strCookieName) = strCookieValue

   ' This will make the cookie expire in 1000000 days
   Response.Cookies(strCookieName).Expires = Date() + 1000000  


PHP - Sample Code

// Cookie is Valid for 1000000 days. 3rd value in function is the time expiration in seconds. ... 3600 seconds = 1 hour

if(!isset($HTTP_COOKIE_VARS["CLICKTRACKSID"]))
setcookie ("CLICKTRACKSID", "MYVALUE", time() + 86400000000);  



JavaScript - Sample Code

The following is a standard cookie library to save in a .js file and include on your website. For example:

<script language="JavaScript" src="cookie.js"></script>


Inside cookie.js, you should paste the following:


// ---------------------------------------------------------------
//  Cookie Functions - Second Helping  (21-Jan-96)
//  Written by:  Bill Dortch, hIdaho Design <BDORTCH@NETW.COM>
//  The following functions are released to the public domain.
//
//  The Second Helping version of the cookie functions dispenses with
//  my encode and decode functions, in favor of JavaScript's new built-in
//  escape and unescape functions, which do more complete encoding, and
//  which are probably much faster.
//
//  The new version also extends the SetCookie function, though in
//  a backward-compatible manner, so if you used the First Helping of
//  cookie functions as they were written, you will not need to change any
//  code, unless you want to take advantage of the new capabilities.
//
//  The following changes were made to SetCookie:
//
//  1.  The expires parameter is now optional - that is, you can omit
//      it instead of passing it null to expire the cookie at the end
//      of the current session.
//
//  2.  An optional path parameter has been added.
//
//  3.  An optional domain parameter has been added.
//
//  4.  An optional secure parameter has been added.
//
//  For information on the significance of these parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html    
//
//
// "Internal" function to return the decoded value of a cookie
//
    function getCookieVal (offset) {
      var endstr = document.cookie.indexOf (";", offset);
      if (endstr == -1)
        endstr = document.cookie.length;
      return unescape(document.cookie.substring(offset, endstr));
    }

//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
    function GetCookie (name) {
      var arg = name + "=";
      var alen = arg.length;
      var clen = document.cookie.length;
      var i = 0;
      while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
          return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
      }
      return null;
    }

//
//  Function to create or update a cookie.
//    name - String object object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
    function SetCookie (name, value) {
      var argv = SetCookie.arguments;
      var argc = SetCookie.arguments.length;
      var expires = (argc > 2) ? argv[2] : null;
      var path = (argc > 3) ? argv[3] : null;
      var domain = (argc > 4) ? argv[4] : null;
      var secure = (argc > 5) ? argv[5] : false;
      document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
    }

//  Function to delete a cookie. (Sets expiration date to current date/time)
//    name - String object containing the cookie name
//
    function DeleteCookie (name) {
      var exp = new Date();
      exp.setTime (exp.getTime() - 1);  // This cookie is history
      var cval = GetCookie (name);
      document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    }

//---------------------------------------------------------------------------------------------


Once you have this setup, you can use this JavaScript cookie library by executing the following code:

SetCookie( "CLICKTRACKSID", "MYVALUE", expDate);



How Does ClickTracks Start Tracking Cookies?

In the ClickTracks ServerAdmin, find the dataset you want to work with. Click (or double-click) on the Dataset Properties icon.

Select on the Cookie Tracking tab.

Check the "Enable cookie tracking" checkbox.

In the text field for entering persistent cookie names, enter the name of the persistent cookie you are setting on your website, as follows:

CLICKTRACKSID=

Then click OK and close the dialog box. Once you have cookie tracking set up, you will need to reimport your logfiles (if you have already imported your logfiles). This is because cookies in your logfiles must be processed as they are getting imported. It is too late to process cookies once the logfiles are loaded.