eBrandz Blog

Use Check Cookie Function to Track Valuable Cookie Information

To write this valuable article we would like to thanks to Brain Clifton and his book “Advanced Web Metrics with Google Analytics – Second Edition” in which a wonderful explanation has been given to “Import Data into the CRM using Javascript with Google Analytics”. Credit also goes to Justin Cutroni who has developed similar function in his blog title “Integrating Google Analytics with a CRM”.
It led us to develop a script which would work with the new version of Google Analytics Asynchronous code.

The script “Check Cookie Function” tracks the Google Analytics campaign cookie values such as medium, source, keywords, campaign and content for websites using Google Analytics tracking Code. It is especially useful for B2B websites to extract valuable visitor referral data in their CRM along with their lead form.

A B2B website is where visitors are asked to fill up some form with some information by which the website owner can later contact them. These informations may include Name, Email, Phone, Physical Location, etc.

By using “check cookie function” we have extracted important information from the _utmz cookie and have them mailed to us with the inquiry forms on the websites. This helps us in identifying the exact combination of source, keyword and medium that helped us in the conversion. It is not necessary that we need a lead form only to send the cookie data with. If someone wants to know the referral information for the visitors that converted, they can use the function on their goal page and implement event tracking to get the data directly into the analytics account. The script can get us all information related to the campaign that brought the converting visitor like source, medium, campaign, keyword and gclid (Google click id parameter in case of Google paid ads). The extraction of the campaign information depends on the individual need. You can record the value on click of a button, on load of a page, on submission of a form or as required.

For those who do not know what a Cookie is, Cookies are small text messages file that a web server transmits to a web browser so that it can keep track of the visitors activity on a website.

Till now, we are adding the normal google analytics tracking code on each web page and also on thank you page to track the goal conversions in google analytcs account.

Below is the sample screenshot of normal google analytics tracking code.

With the above normal google analytics tracking code we cannot track the valuable visitor referral data in CRM with lead form. When visitors to your website view a page, the above google analytics javaScript code executes the tracking operation for analytics. And then it produces a wide variety of data about website pages, visitors, loyalty, AdWords traffic sources, and more.

To extract the valuable visitor referral data in CRM along with lead form; first the normal google analytics code should be installed immediately before the closing </head> tag on lead form page and second check cookie function script needs to be placed in the head section. (To find the check cookie function script please check below)

And at last call the function checkCookie() on the onClick event of the lead form and add the hidden field to the form so that the referral data is sent along with the normal form data.  (Check form field function below)

Code Sample: Check Cookie Function

/* This part is common for all pages from where the visitor data needs to be sent from. Here we have considered sending the data with a lead form. Needs to be placed in the head section. */

<script type=”text/javascript”>

function getCookie(c_name)        // This function fetches the content of the desired cookie.

{

if (document.cookie.length>0)

{

c_start=document.cookie.indexOf(c_name + “=”);

if (c_start!=-1)

{

c_start=c_start + c_name.length+1;

c_end=document.cookie.indexOf(“;”,c_start);

if (c_end==-1) c_end=document.cookie.length;

return unescape(document.cookie.substring(c_start,c_end));

}

}

return “”;

}

/* The below function calls the getCookie() function to fetch the contents of _utmz cookie and then simplifies the content so that it is understandable even by people with less technical knowledge. */

function checkCookie()

{

cookiename=getCookie(‘_utmz’);

if (cookiename!=null && cookiename!=””)

{

var q = new RegExp(“utmgclid=[a-zA-Z0-9\-]*”);

var r = new RegExp(“[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.”);

if(cookiename.match(q))

{

var str = cookiename.replace(r, “”);

var str1 = str.replace(q, “Source= Google”);

var str2 = str1.replace(“|utmccn=”, “<br/>Campaign= “);

var str3 = str2.replace(“|utmcmd=(not set)”, “<br/>Medium= ppc”);

var str4 = str3.replace(“|utmctr=”, “<br/>Keyword= “);

var str5 = str4.replace(“|utmcct=”, “<br/>Content= “);

document.form1.cookievalue.value = str5;

}

else

{

var str = cookiename.replace(r, “”);

var str1 = str.replace(“utmcsr=”, “Source= “);

var str2 = str1.replace(“|utmccn=”, “<br/>Campaign= “);

var str3 = str2.replace(“|utmcmd=”, “<br/>Medium= “);

var str4 = str3.replace(“|utmctr=”, “<br/>Keyword= “);

var str5 = str4.replace(“|utmcct=”, “<br/>Content= “);

document.form1.cookievalue.value = str5;

}

}

}

</script>

Form Field:

/* Now all you need to do is to call the function checkCookie() on the onClick event of the lead form and add the hidden field to the form so that the referral data is sent along with the normal form data. */

<input type=”hidden” name=”cookievalue” id=”cookievalue” value=”” />

After implementing the above script on your B2B website a detailed report with cookie value information will generate. Below is the sample lead report which are received after filling and submitting the inquiry form.

Sample Lead Report 1 – Showing Cookies Value for a paid visit:


Sample Lead Report 2 – Showing Cookies Value for an organic visit:

By using the above functions and a minimal html coding on your contact, enquiry, quote form or on any other lead capturing form pages you can obtain valuable data. With this data you can build your strategy to increase the performance of your business and website.

For Example :

In sample lead report 1 – We have extracted the paid keyword which visitors actually used as a search term on Google and reached the website by clicking on Ad later filling up the form on the website. We can use variations of that keyword in Adwords to achieve more traffic and get good results.

In sample lead report 2 – The form shows that the lead is generated through an organic visit, source is google and the keyword used is Professional SEO. If you are running any paid campaign for example google adwords then you can use the keyword in the paid campaign to generate more leads.

This are just few examples to illustrate the possibilities you can explore using this script on your website.

Using the cookie function you can track-

  • Which source generated the lead?
  • What is the name of the campaign?
  • Which medium is more responsible in generating leads?
  • Which keywords are bringing more converting visitors?

All above information you get along with the lead details. If some of the leads convert in to sales then it becomes easier to get the exact keywords, source and medium which brought the converting visitors to your website. With such accurate information you can optimize your overall internet marketing strategy. Feel free to pass on your feedback!