Jump to content

Recommended Posts

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:

 

utmzcookie.jpg

 

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

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,

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>';
?>

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:

 

array.jpg

 

It would seem that I'm not getting any keywords in my array, which I'm pretty sure wasn't happening before ...

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().

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.

  • 1 year later...

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.