Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing __utmz tracking cookie to get referral

I use Google Analytics on my site, and I want to read __umtz cookie to get referring link. I made some research and I wrote such code:

$refer=explode('utmcsr=',$_COOKIE['__utmz']);
if(count($refer)>1) $refer=explode('|',$refer[1]);
$refer=addslashes($refer[0]);

The problem is, this is not always working, sometimes I get junk as result. What I am doing wrong? Maybe someone have a good description of this cookie?

like image 828
Thinker Avatar asked Jul 31 '09 18:07

Thinker


2 Answers

Check my Google Analytics Cookie Parser.

Google Analytics PHP Cookie Parser is a PHP Class that you can use to obtain data from GA cookies such as campaign, source, medium, etc. You can use this parser to get this data on your contact forms or CRM.

Just updated to version 1.2 with minor bugfixes and more info, number of pages viewed in current visit.

like image 171
Joao Correia Avatar answered Sep 25 '22 02:09

Joao Correia


You could use $_SERVER['HTTP_REFERER'] to get the Referer.

Overall it is a bad idea to use other's people's cookies to get data unless you know exactly how they work, and when they update, or you use an API that THEY have made available.

Lets say the Google decides to revamp the cookie altogether so that the Referer information isn't available on the cookie, your system would break. It is best to get data directly from your own sources rather than someone else's.

like image 38
Tyler Carter Avatar answered Sep 23 '22 02:09

Tyler Carter