MortimerJazz Posted January 25, 2011 Share Posted January 25, 2011 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, which looks like this: So here's a short function that I wrote to try and extract the keyword information: function parse_ga_cookie($cookie) { $values = sscanf($cookie, '%d.%d.%d.%d.utmcsr=%[^|]|utmccn=%[^|]|utmcmd=%[^|]|utmctr=%[^|]'); if (count($values) !== { // 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... $keyword=(parse_ga_cookie($_COOKIE['__utmz'])); Unfortunately though when I go to print out the value $keyword I'm just getting the value 'Array' Is anyone able to help me out at all here? I can't see where the problem lies. Thanks a lot for your help ... Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/ Share on other sites More sharing options...
PaulRyan Posted January 25, 2011 Share Posted January 25, 2011 Are you using echo $keyword; or print $keyword;? If so try using print_r($keyword); becuase the function is returning an array from the input. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1165072 Share on other sites More sharing options...
MortimerJazz Posted January 25, 2011 Author Share Posted January 25, 2011 Cheers Paul - I'll give that a shot. Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1165078 Share on other sites More sharing options...
MortimerJazz Posted January 25, 2011 Author Share Posted January 25, 2011 Ok, quick update as this hasn't quite worked. Here's the ammended code (the ammended part is the print_r() function at the end) <? function parse_ga_cookie($cookie) { $values = sscanf($cookie, '%d.%d.%d.%d.utmcsr=%[^|]|utmccn=%[^|]|utmcmd=%[^|]|utmctr=%[^|]'); if (count($values) !== { // 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... $keyword=print_r((parse_ga_cookie($_COOKIE['__utmz']))); ?> The output has changed now, but it's still not quite correct. Instead, I'm getting the value '1' in the $keyword variable now. I'm trying to figure out if that's a string or a value from the array. Can anyone help out at all? Cheers, Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1165171 Share on other sites More sharing options...
Pikachu2000 Posted January 25, 2011 Share Posted January 25, 2011 See what this gives you <?php function parse_ga_cookie($cookie) { $values = sscanf($cookie, '%d.%d.%d.%d.utmcsr=%[^|]|utmccn=%[^|]|utmcmd=%[^|]|utmctr=%[^|]'); if (count($values) !== { // 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... $keyword = parse_ga_cookie($_COOKIE['__utmz']); echo '<pre>'; print_r($keyword); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1165173 Share on other sites More sharing options...
MortimerJazz Posted January 25, 2011 Author Share Posted January 25, 2011 See what this gives you Ok, now I'm a little confused. This is the print out I get on the screen when the script is run: It would seem that I'm not getting any keywords in my array, which I'm pretty sure wasn't happening before ... Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1165213 Share on other sites More sharing options...
Pikachu2000 Posted January 25, 2011 Share Posted January 25, 2011 The only changes I made were in how the data the function returns is accessed and displayed, so anything that isn't there now wasn't there before either. Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1165217 Share on other sites More sharing options...
DavidAM Posted January 25, 2011 Share Posted January 25, 2011 I don't think this is correct: $values = sscanf($cookie, '%d.%d.%d.%d.utmcsr=%[^|]|utmccn=%[^|]|utmcmd=%[^|]|utmctr=%[^|]'); sscanf() parses a string based on placeholders. The "%d" placeholders indicate you are looking for integers there - that's fine. However, "%[^|]" is not a sscanf() placeholder. That looks more like a regular expression, which is not valid in a sscanf() call - well, at least not to my knowledge. You need to either change those, or change the first part and use preg_match(). Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1165220 Share on other sites More sharing options...
MortimerJazz Posted January 26, 2011 Author Share Posted January 26, 2011 Thanks for the response David, but I thought that section of code listed basic wildcard patterns as opposed to Regular Expressions which would be ok for sscanf () Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1165389 Share on other sites More sharing options...
DavidAM Posted January 26, 2011 Share Posted January 26, 2011 Perhaps my previous post was not entirely correct. I rarely use scanf(), but I use sprintf() frequently. The documentation for sscanf() says that it uses the same format specifiers as sprintf(). And those "wildcard" expressions and not part of the sprintf() specification. However, I have just read through all of the user comments on sscanf() and they indicate that such expressions are usable; although I cannot find any documentation on what these "wildcard" specifiers are or how they are interpreted. If your cookie value is what you showed (earlier) in the image, then the print_r() output indicates that the sscanf() format is not being interpreted as you expect it to be. I don't have an environment here at work to do any testing, but I think the sscanf() specification is the culprit here. Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1165560 Share on other sites More sharing options...
gr8webdiva Posted November 30, 2012 Share Posted November 30, 2012 Hi Mortimer, The code worked fine for me as is. However, I used echo statements to display the data based on the array. Not the most elegant way of writing it, and I could have used an foreach statement to echo them, but I was in a rush and this was easiest for me. function parse_ga_cookie($cookie) { $values = sscanf($cookie, '%d.%d.%d.%d.utmcsr=%[^|]|utmccn=%[^|]|utmcmd=%[^|]|utmctr=%[^|]'); if (count($values) !== { // 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... $keyword=(parse_ga_cookie($_COOKIE['__utmz'])); echo 'Domain: ' . $keyword['domain'] . '<br />'; echo 'Timestamp: ' . $keyword['timestamp'] . '<br />'; echo 'Visits: ' . $keyword['visits'] . '<br />'; echo 'Sources: ' . $keyword['sources'] . '<br />'; echo 'Campaign: ' . $keyword['campaign'] . '<br />'; echo 'Source: ' . $keyword['source'] . '<br />'; echo 'Medium: ' . $keyword['medium'] . '<br />'; echo 'Keyword: ' . $keyword['keyword']; Does this help? Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1396513 Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2012 Share Posted November 30, 2012 Probably doesn't help much, being that this thread is nearly a year old . . . Quote Link to comment https://forums.phpfreaks.com/topic/225645-extracting-keyword-information-from-google-cookie/#findComment-1396523 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.