SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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,

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Do you have an example of the data contained within the cookie available for posting?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quick and dirty, but it does the job. Just pass the function the utmz cookie string.



    Let me know if you have any issues.

    PHP Code:
    <?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>
        )
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Bugger, I noticed the first value is concatenated, I'll work on that.

    Yep.

    PHP Code:
    <?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>
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2006
    Location
    Gold Coast, Australia
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good job as usual. Regex will be the death of me.

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Still messy, but fixed.
    PHP Code:
    <?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>
        )
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    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!
    Salathe
    Software Developer and PHP Manual Author.

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    There are probably better ways to parse out the cookie value 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
    I am!

    Post away.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    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
    )
    */ 
    Salathe
    Software Developer and PHP Manual Author.

  12. #12
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Nice. Very nice.

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

    Thanks again.

    Anthony.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  13. #13
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  14. #14
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simontheak View Post
    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).

    Quote Originally Posted by AnthonySterling View Post
    I really do appreciate the time you take to post Peter, I always learn something new.
    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.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •