I hope I’m posting this in the correct section - if not, please feel free to move it.
What I’m trying to do is extract keyword information from the Google Analytics cookie and pass it into the header when a click is generated.
Having hunted around I understand that I need to extract the utmctr value from the __utmz campaign cookie … I’m just not quite sure the best way to go about doing this, so was hoping to get some help.
Can someone either explain to me the best way to access this information using PHP? Or point me in the direction of an article that can?
Also, I’m wary of the fact that users to my site have probably got a load of other GA cookies on their machine.
Do I need to specifically choose the cookie relevant to my site? Or does the fact that I can only access cookies that my site have set mean that I don’t need to worry about that?
You absolute legend - I wasn’t expecting code. Thanks very much!
Just a quick question … am I right in thinking that with the code above, all I’d need to do is echo (utmctr) to print out simply the keyword value stored in the cookie?
There are probably better ways to parse out the cookie value (which, incidentally, wouldn’t be prefixed with __utmz= when PHP sees it) into something more usable or to quickly target just the keywords value that you’re looking for. Would you be open to other code snippets; do you need all the other information?
Off Topic:
My keywords, according to my __utmz for Sitepoint, are sticky orange! (:
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!
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
)
*/
As have I. I’ve found that helping out on forums like this mutually benefit both the questioner (they get their questions answered, or discussed) and answerer (we like puzzles, problems, tasks, and sharing our own experience).
You’re welcome. I spend far, far, far more time not posting and don’t really feel comfortable interjecting oftentimes (but, Anthony, I know you’re more than happy to get perhaps an alternative view point); there are lots of knowledgeable and helpful folks here, I’m often too late to the party.