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…

11 thoughts on “How to parse Google Analytics cookie to track lead source”

  1. This is fantastic, just what I was looking for. One question: how do you display these metrics on the Analytics page?

  2. Jim,

    These metrics are on the individual level, so when you save them to your database you want to associate them to a unique ID. You can then create your own custom views in the admin area of your website by running different queries. For example, you can run a query for all organic search members and also include the keyword that they searched. Or you can just list all members with their lead source, so you can quickly see where new members are coming from.

    I hope this helps – glad you like the post.


  3. Hi Andrew, when I click on AdWords, it doesn’t grab the keyword I entered. Not sure why, and the source field is empty too?

    Also one other question, I’m using this for tracking visitors from 3rd party websites like directories and other websites, but if the visitor has visited my website before lets say via Google or another search engine, the referrer is set to the Search Engine, not the 3rd party advertiser. I recon it’s a google analytics cookie expiring thing? Any idea why?

  4. Andrew, Just a quick question, I added a new Profile to my current Google Analytic code,

    This is the original code.

    _gaq.push([‘_setAccount’, ‘UA-3854930-27’]);

    This is the new code.

    _gaq.push([‘_setAccount’, ‘UA-3854930-27’]);


    As you can see I have added secondTracker to the existing code, and it works fine. Just a quick question, how do I assign $info to the secondTracker COOKIE, and I hope it wont clash with the Original cookie?

    Thanks Andrew, Great Tool!

Leave a Reply

Your email address will not be published. Required fields are marked *