Parsing __utmz tracking cookie to get referral

2019-04-07 13:03发布

问题:

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?

回答1:

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.



回答2:

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.