Extracting Keyword information from GA cookie

Morning all,

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?

Cheers,

Do you have an example of the data contained within the cookie available for posting?

Hi Anthony - thanks for the quick reply.

Here’s an example of the __utmz cookie from Google’s support files:

The keyword is stored in the utmctr value at the end of the string.

Does that help at all?

Quick and dirty, but it does the job. Just pass the function the utmz cookie string.

:slight_smile:

Let me know if you have any issues.


<?php
function parse_google_cookie($__utmz){
    $matches = array();
    preg_match_all(
        '~(ut[^=]+)=([^\\|]+)~i',
        ' __utmz=XXXX.TTTTT.V.C.utmccn=<campaign>|utmcsr=<source>|utmcmd=<medium>|utmctr=<keyword>',
        $matches
    );
    return array_combine((array)$matches[1], (array)$matches[2]);
}

print_r(
    parse_google_cookie(' __utmz=XXXX.TTTTT.V.C.utmccn=<campaign>|utmcsr=<source>|utmcmd=<medium>|utmctr=<keyword>')
);

/*
    Array
    (
        [utmz] => XXXX.TTTTT.V.C.utmccn=<campaign>
        [utmcsr] => <source>
        [utmcmd] => <medium>
        [utmctr] => <keyword>
    )
*/
?>

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?

Bugger, I noticed the first value is concatenated, I’ll work on that.

Yep. :slight_smile:


<?php
function parse_google_cookie($__utmz){
    $matches = array();
    preg_match_all(
        '~(ut[^=]+)=([^\\|]+)~i',
        ' __utmz=XXXX.TTTTT.V.C.utmccn=<campaign>|utmcsr=<source>|utmcmd=<medium>|utmctr=<keyword>',
        $matches
    );
    return array_combine((array)$matches[1], (array)$matches[2]);
}

$google_cookie = parse_google_cookie(' __utmz=XXXX.TTTTT.V.C.utmccn=<campaign>|utmcsr=<source>|utmcmd=<medium>|utmctr=<keyword>');

echo $google_cookie['utmctr']; #<keyword>
?>

Good job as usual. Regex will be the death of me.

Still messy, but fixed. :smiley:


<?php
function parse_google_cookie($__utmz){
    $data = array();
    foreach((array)preg_split('~([.|])(?=ut)~', $__utmz) as $pair){
        list($key, $value) = explode('=', $pair);
        $data[$key] = $value;
    }
    return $data;
}

print_r(
    parse_google_cookie(
        '__utmz=XXXX.TTTTT.V.C.utmccn=<campaign>|utmcsr=<source>|utmcmd=<medium>|utmctr=<keyword>'
    )
);

/*
    Array
    (
        [__utmz] => XXXX.TTTTT.V.C
        [utmccn] => <campaign>
        [utmcsr] => <source>
        [utmcmd] => <medium>
        [utmctr] => <keyword>
    )
*/
?>

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! (:

I am! :cool:

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!


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
)
*/

Nice. Very nice.

I really do appreciate the time you take to post Peter, I always learn something new.

Thanks again.

Anthony.

Thanks from me too Peter.

Really appreciate the alternative code.

Looks like I’ve still got some way to go with my own PHP skills, but stuff like this makes it a lot easier to learn!

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. :blush: