Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Redirect: Problem with Referer Header

Somebody follows a blog link(say http://blog) to come to my site (say http://mysite/a.php).

So now she is on page http://mysite/a.php and referer is set to http://blog

Now there is JavaScript on the page http://mysite/a.php which does the following redirection:

    document.location = "http://mysite/b.php;
    //This is executed before any Google Analytics script.

Now in the request to http://mysite/b.php, referer is set as http://mysite/a.php.
Because of which (I think so) my Google Analytics is showing all the traffic coming from http://mysite/a.php.

Suggest a solution please.
Note: JavaScript redirection is critical, can't get rid of it.
Also I tried sending 302 code from server, but no success.

like image 375
Varun Avatar asked Nov 11 '10 13:11

Varun


1 Answers

Edit: New, recommended way

Google Analytics now has a special URL parameter, utm_referrer, where you can pass the referer to the page you're redirecting to; doing so will simulate the same behavior as below without any need to mess with cookies or functions.

In your example, you'd make your code say:

 document.location = "http://mysite/b.php?utm_referrer=" + encodeURIComponent(location.href); ;

Old, harder but functional way

Google Analytics has a function called setRefererOverride(). If you set it, it will override the referrer value that it tracks to whatever value you set.

The best way to do this is to, prior to the redirect, store the real referrer in a cookie, like so:

document.cookie = "realreferrer="+encodeURIComponent(document.referrer)+"; path=/";

Then, read the cookie value on the page. If the cookie is set, call that function before your pageview.

var realreferrer = readCookie("realreferrer"); //using a readCookie function of some kind
if (realreferrer) {
 _gaq.push(['_setReferrerOverride', realreferrer ]);
            //if using the old code, use pageTracker._setReferrerOverride(realreferrer);
}
like image 193
Yahel Avatar answered Oct 16 '22 04:10

Yahel