How to parse Google Analytics cookie to track lead source

In my previous post titled “Lead source analytics every start-up should track,” I promised to write a follow-up post that outlines how to track the following lead source details…

  • Medium (organic, PPC, referral, email, webinar, press release, etc)
  • Lead Source (Google, Yahoo, Twitter, Facebook, LinkedIn, etc)
  • Keywords (What keywords did the visitor search?)
  • Referring URL (Where did the visitor come from?)
  • Landing Page (What was the first page the visitor visited on our website?)
  • Form URL (What was the URL of the form that the user filled out where you collected this information?)

We will use a combination of techniques including parsing the cookie set by Google Analytics to save these lead source details into PHP variables. We’ll then be able to pass these variables into our lead capture forms as hidden fields, so that when a lead registers on our website or fills out any type of form, we’ll be able to track these lead source details.

Let’s get started…

First, I’m going to give you the code. This code should be included in the header of every page of your website. Hopefully your website has one PHP file that makes up the header of every page of your website. This file is usually called header.php. If you have this file, you can dump this code right into that file.

$domain = $_SERVER['SERVER_NAME'];
$domain = "." . ltrim($domain,"www.");

$info = $_COOKIE['__utmz'];
// Get rid of id stuff
$holder = split("u", $info, 2);
$string = "u" . $holder[1];
// Parse String
$ga_vars = split("|", $string);
foreach ($ga_vars as $var) {
list($key,$value) = split("=",$var);
if ($key == "utmcmd") { setcookie("Medium", $value, time()+100000000, "/", $domain); $medium = $value; }
if ($key == "utmctr") { setcookie("Keywords", $value, time()+100000000, "/", $domain); $keywords = $value; }
if ($key == "utmcsr") { setcookie("GAsource", $value, time()+100000000, "/", $domain); $gasource = $value; }

if (!isset($_COOKIE['Referer'])) {
setcookie("Referer", $_SERVER[‘HTTP_REFERER’], time()+10000000, "/", $domain);
$referurl = $_SERVER['HTTP_REFERER'];
} else { $referurl = $_COOKIE['Referer']; }

$formurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

if (!isset($_COOKIE['LandingPage'])) {
setcookie("LandingPage", "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], time()+10000000, "/", $domain);
$landingpage = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
} else { $landingpage = $_COOKIE['LandingPage']; }

Here is a description of each variable and how they are tracked and saved…


Medium is parsed from the Google Analytics cookie. You will need to have Google Analytics installed on every page of your website in order for this to be tracked. When a visitor comes to your website, Google will drop a cookie that contains important information about that visitor. One of the variables saved in this cookie is Medium, so we parse the Google Analytics cookie and save the Medium into a variable called $medium.

Lead Source

Lead Source is also parsed from the Google Analytics cookie just like Medium. It is saved into a variable called $gasource.


Keywords are also parsed from the Google Analytics cookie. This variable will not always be set, but if the visitor comes to your website via a search engine, it will record which keywords they used to visit your website. This variable works with all the major search engines – not just Google.

Referring URL

This code records the URL that the visitor clicked through to your website with. The code saves the Referring URL in a cookie, so the original Referring URL is preserved. It is saved into a variable called $referurl.

Landing Page

This is the first page that the visitor visits on your website. Like the Referring URL, a cookie is also used to save this URL so that it is preserved. It is saved into a variable called $landingpage.

Form URL

This is the URL of the page that the visitor finally is on when they fill out the lead capture form. This variable will update as the visitor navigates your website. A cookie is not used. It is saved into a variable called $formurl.

That pretty much sums up the script. In another follow-up post, I will write about how to pass these variables into a form as a hidden field and save it into your database. Look for this post shortly…