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.

<?php
//DEFINE COOKIE DOMAIN, ALLOWS SCRIPT TO BE USED ACROSS MULTIPLE DOMAINS
$domain = $_SERVER['SERVER_NAME'];
$domain = "." . ltrim($domain,"www.");

// SET COOKIES FROM GOOLGE-ANALYTICS COOKIE
$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; }
}

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

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

// SET LANDING PAGE
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

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

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…

Lead source analytics every start-up should track

An area of marketing that I have been spending a considerable time studying recently is website analytics. I’ve been asking myself questions like:

  • What information should we be tracking?
  • What information is important to marketing?
  • What information is important to sales?
  • What information is important to executive management?
  • How do we track this information?
  • How do we present this information to marketing/sales in a way that it can easily become actionable?
  • How do we tie all our systems together (SalesForce, HubSpot, ClickTale, etc)?
  • How to we lead nurture and lead score?
  • When should a lead transition from marketing to sales?

This isn’t an easy problem to solve, especially for start-ups with limited resources and tight budgets. However, I would argue that properly tracking leads or new member activity is probably one of the most important things any start-up can do. The information and insight obtained through analytics will answer many questions like:

  • Where are new members/leads coming from?
  • What is my cost per lead? cost per new member? cost per sale? total lifetime value of a customer?
  • Where should I be allocating more advertising dollars? less advertising dollars?
  • What part of my website is converting? not converting?
  • Are social media leads more profitable than webinar leads?
  • For those leads that turned into sales, what common traits/activities do they possess?
  • How do I nurture and educate a lead so they are ready to buy?

Those are just some of the questions you’ll be able to answer and then act on. If you are a start-up that has received VC money, these are the exact questions your board of directors and investors will be asking. They will be very impressed if you can give clear, reliable answers to their questions.

So with that said, where/how do we begin?

I have identified the following lead source details that every start-up should be tracking today (this is just a start). In a follow-up post, I will include a detailed step-by-step description on how to properly track this information. Here are the lead source details:

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

Keep your eye out for my follow-up post on how to track all of the above web analytics and lead source details.