
Originally Posted by
AnthonySterling
Post away.
Your wish, is my command. Since we want to parse a formatted string (which, it appears, is what the cookie value is) then something like the following would generally be my first choice. Not that there's anything wrong with what has already been posted!
PHP Code:
function parse_ga_cookie($cookie)
{
$values = sscanf($cookie, '%d.%d.%d.%d.utmcsr=%[^|]|utmccn=%[^|]|utmcmd=%[^|]|utmctr=%[^|]');
if (count($values) !== 8) {
// return false; trigger_error(...); ... or whatever you like
throw new InvalidArgumentException("Cookie value '$cookie' does not conform to the __utmz pattern");
}
$keys = array('domain', 'timestamp', 'visits', 'sources', 'campaign', 'source', 'medium', 'keyword');
return array_combine($keys, $values);
}
// Cooookkkkkkiiiiiiieeeeeeeeeeee, om nom nom...
print_r(parse_ga_cookie($_COOKIE['__utmz']));
/*
// Result from my Sitepoint cookie
Array
(
[domain] => 1
[timestamp] => 1276242664
[visits] => 248
[sources] => 7
[campaign] => search
[source] => (organic)
[medium] => organic
[keyword] => sticky orange
)
*/
Bookmarks