Google Analytics PHP cookie parser

Maio 18, 2010 | Por

Google Analytics collects data using first-party cookies who are stored on our browsers. I’ve programmed a PHP class we can use to read Google Analytics __utma and __utmz cookies. This class can be used to easily integrate this cookie data into proprietary systems like CRM, ERP, Helpdesks, etc.

  • __utma (expires 2 years after being defined) – visitor dataThis cookie is written on your first visit to the website. In case you erase it its created again. Its used for the Unique Visitors calculation and is updated on every pageview.
  • __utmz (expires 6 months after being defined) – campaign dataThis cookie stores informations on how the user got to our website: referrer, direct (none), organic or a campaign such as a newsletter. (since you tag it correctly using the URL Builder). This cookie is overwritten every time you visit the website.

The Google Analytics Cookie Parser allows you to obtain some data contained in this cookies in a human readable format. For example, you can see how you got here by visiting: http://joaocorreia.pt/example.php.

  • Campaign source
  • Campaign name
  • Campaign medium
  • Campaign content
  • Campaign term
  • Date of first visit
  • Date of previous visit
  • Date of current visit
  • Times visited
  • Pages viewed

Download  versão 1.2 (21 April 2012) class.GAParse.zip (class.gaparser.php and example.php)

example.php

<?

require("class.gaparse.php");

$aux = new GA_Parse($_COOKIE);

echo "Campaign source: ".$aux->campaign_source."<br />";
echo "Campaign name: ".$aux->campaign_name."<br />";
echo "Campaign medium: ".$aux->campaign_medium."<br />";
echo "Campaign content: ".$aux->campaign_content."<br />";
echo "Campaign term: ".$aux->campaign_term."<br />";

echo "Date of first visit: ".$aux->first_visit."<br />";
echo "Date of previous visit: ".$aux->previous_visit."<br />";
echo "Date of current visit: ".$aux->current_visit_started."<br />";
echo "Times visited: ".$aux->times_visited."<br />";
echo "Pages viewed current visit: ".$aux->pages_viewed."<br />";
?>

class.gaparse.php

<?php
////////////////////////////////////////////////////
// GA_Parse - PHP Google Analytics Parser Class
//
// Version 1.0 - Date: 17 September 2009
// Version 1.1 - Date: 25 January 2012
// Version 1.2 - Date: 21 April 2012
//
// Define a PHP class that can be used to parse
// Google Analytics cookies currently with support
// for __utmz (campaign data) and __utma (visitor data)
//
// Author: Joao Correia - http://joaocorreia.pt
//
// License: LGPL
//
////////////////////////////////////////////////////

class GA_Parse
{

var $campaign_source; // Campaign Source
 var $campaign_name; // Campaign Name
 var $campaign_medium; // Campaign Medium
 var $campaign_content; // Campaign Content
 var $campaign_term; // Campaign Term

var $first_visit; // Date of first visit
 var $previous_visit; // Date of previous visit
 var $current_visit_started; // Current visit started at
 var $times_visited; // Times visited
 var $pages_viewed; // Pages viewed in current session

 function __construct($_COOKIE) {
 // If we have the cookies we can go ahead and parse them.
 if (isset($_COOKIE["__utma"]) and isset($_COOKIE["__utmz"])) {
 $this->ParseCookies();
 }

 }

function ParseCookies(){

// Parse __utmz cookie
 list($domain_hash,$timestamp, $session_number, $campaign_numer, $campaign_data) = split('[\.]', $_COOKIE["__utmz"],5);

// Parse the campaign data
 $campaign_data = parse_str(strtr($campaign_data, "|", "&"));

$this->campaign_source = $utmcsr;
 $this->campaign_name = $utmccn;
 $this->campaign_medium = $utmcmd;
 if (isset($utmctr)) $this->campaign_term = $utmctr;
 if (isset($utmcct)) $this->campaign_content = $utmcct;

// You should tag you campaigns manually to have a full view
 // of your adwords campaigns data.
 // The same happens with Urchin, tag manually to have your campaign data parsed properly.

 if (isset($utmgclid)) {
 $this->campaign_source = "google";
 $this->campaign_name = "";
 $this->campaign_medium = "cpc";
 $this->campaign_content = "";
 $this->campaign_term = $utmctr;
 }

// Parse the __utma Cookie
 list($domain_hash,$random_id,$time_initial_visit,$time_beginning_previous_visit,$time_beginning_current_visit,$session_counter) = split('[\.]', $_COOKIE["__utma"]);

$this->first_visit = date("d M Y - H:i",$time_initial_visit);
 $this->previous_visit = date("d M Y - H:i",$time_beginning_previous_visit);
 $this->current_visit_started = date("d M Y - H:i",$time_beginning_current_visit);
 $this->times_visited = $session_counter;

// Parse the __utmb Cookie

list($domain_hash,$pages_viewed,$garbage,$time_beginning_current_session) = split('[\.]', $_COOKIE["__utmb"]);
 $this->pages_viewed = $pages_viewed;
 // End ParseCookies
 }

// End GA_Parse
}

?>
  • Pingback: Avaliar o sucesso da minha campanha de marketing online - Google Analytics | João Correia

  • Pingback: Avaliar o sucesso das campanhas marketing online | Marketing Online Portugal

  • Deyan Kyosev

    Hi, this is a great article, but where $utmcsr, $utmccn, $utmcmd, $utmctr, $utmcct are initialized?

  • http://www.facebook.com/joaolcorreia João Correia

    Hello Deyan, 
    I get those variables from parse_str on line 43, I don’t initialize them.

  • Pingback: Avaliar o sucesso das campanhas de marketing online | Marketing Online Portugal

  • Joe

    Has anybody figure out how to make this work with Google Adwords.

  • http://joaocorreia.pt João Correia

    Hello Joe, If you want to use the GA cookie parser you have to manually tag your URLs with URL Builder.
    http://www.google.com/support/analytics/bin/answer.py?answer=55578

  • Webimerge

    Hi I want to display whether the user came from a paid or organic link can I you give me some advice on this?

  • Anónimo

    Thank you by sharing your code, I used it and worked fine!

  • Nei Rauni Santos

    It would be great if you update your code in order to avoid notices in recent php versions..

    Notice: Undefined variable: utmgclid on /lib/gaparse.class.php on line 59

    Thank you,

  • http://joaocorreia.pt João Correia

    Hello Nei,

    I’ve just fixed this.

    Thanks

  • http://joaocorreia.pt João Correia

    Hello, you can see it right away on Campaign medium. Install the class and test its pretty straightforward !

  • Anónimo

    Thanks for the inspiration!
    Bug report: 
    list($domain_hash,$timestamp, $session_number,$campaign_numer, $campaign_data) = split(‘[.]‘, $this->utmz); 
    should be 
    list($domain_hash,$timestamp, $session_number,$campaign_numer, $campaign_data) = split(‘[.]‘, $this->utmz, 5); 
    because keyword may contain periods.
    Cheers!

  • Dan

    Hello. Thank you for sharing a very nice script. One issue I found is when parsing utmcsr you lose some information. For example: if utmcsr= “answers.yahoo.com”, then result is just “answers”. Otherwise I have to say this works great!

  • http://jeffjudge.com Jeff Judge

    Thanks, this script was very helpful!

  • http://joaocorreia.pt João Correia

    Bug fixed ! I also fixed some error Notices.Thank you! 

  • http://joaocorreia.pt João Correia

    Hello Dan ! Its solved now, thanks to timhas !

  • http://joaocorreia.pt João Correia

    Thanks Jeff. I updated it just now with minor fixes!

  • http://twitter.com/vladblagi Vladimir Blagojevic

    Hi, 

    thanks so much for sharing, it works great. 

    There is a deprecation warning for PHP > 5.3 for the split() function. These lines solve it (replacing split with explode): 

    46:  list($domain_hash,$timestamp, $session_number, $campaign_numer, $campaign_data) = explode(‘.’, $_COOKIE["__utmz"]);
    70:   list($domain_hash,$random_id,$time_initial_visit,$time_beginning_previous_visit,$time_beginning_current_visit,$session_counter) = explode(‘.’, $_COOKIE["__utma"]);
    79:   list($domain_hash,$pages_viewed,$garbage,$time_beginning_current_session) = explode(‘.’, $_COOKIE["__utmb"]);

    Best 
    Vlad

  • http://twitter.com/vladblagi Vladimir Blagojevic

    Hi, 

    Thanks so much for posting this code, very useful. 

    A small question, I’m getting Undefined variable: utmccn. Do you know why this could be?

    Best 
    Vlad 

  • http://joaocorreia.pt João Correia

    Problem is it stops working with . on variables !