views:

1011

answers:

3

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  A: 

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.

Chacha102
I just don't want to do again the same thing as google does :)
Thinker
That is an excuse to not do something the right way. When you use excuses to justify bad programming, things go wrong, like you are discovering.
Chacha102
There is a reason people use APIs to get data. Because they are reliable, and they work correctly 99% of the time.
Chacha102
+3  A: 

There is a detailed presentation at Conversion University Help describing the anatomy of the cookies used by GA. The __umtz cookie contains campaign values and one of those is utmcsr (utm source) that—when visitor comes from a referring site—will store the referral source. Keep it mind, that amongst the visitor session its value does not get updated.

Török Gábor
+1  A: 

Check this Google Analytics Cookie Parser:

http://joaocorreia.pt/google-analytics-scripts/google-analytics-php-cookie-parser/

Have fun Joao

Joao Correia