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. It's used for the Unique Visitors calculation and is updated on every pageview.
__utmz (expires 6 months after being defined) – campaign data. This 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.
Download the latest version of Google Analytics PHP cookie parser from Github. https://github.com/joaolcorreia/Google-Analytics-PHP-cookie-parser
<?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 />";
?>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-111111-11']); //Insert your UA
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<?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.io
//
// 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
}
?>