JavaScript重定向:Referer标头问题

时间:2022-01-29 14:57:16

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

有人跟着博客链接(比如http:// blog)来我的网站(比如http://mysite/a.php)。

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

所以现在她在页面http://mysite/a.php上,referer设置为http:// blog

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

现在http://mysite/a.php页面上有JavaScript,它执行以下重定向:

    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.

现在,在http://mysite/b.php的请求中,referer设置为http://mysite/a.php。因此(我认为如此),我的Google Analytics会显示来自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.

请建议一个解决方案。注意:JavaScript重定向很关键,无法摆脱它。我也尝试从服务器发送302代码,但没有成功。

2 个解决方案

#1


9  

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.

Google Analytics现在有一个特殊的网址参数utm_referrer,您可以将引荐传递到您要重定向到的网页;这样做将模拟与下面相同的行为,而不需要弄乱cookie或功能。

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.

Google Analytics有一个名为setRefererOverride()的函数。如果您设置它,它将覆盖它跟踪的引用者值到您设置的任何值。

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

执行此操作的最佳方法是,在重定向之前,将实际引荐来源存储在cookie中,如下所示:

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.

然后,读取页面上的cookie值。如果设置了cookie,请在页面浏览之前调用该函数。

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);
}

#2


0  

I think the cleanest (and maybe only) way would be to trigger a GA count in a.php before doing the redirection.

我认为最干净(也许唯一)的方法是在进行重定向之前触发a.php中的GA计数。

If that's not an option, the only way I can see to keep the referrer alive is

如果这不是一个选项,我唯一可以看到保持推荐人活着的方法是

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

(will not work in IE under some circumstances)

(在某些情况下不适用于IE)

But that will still not tell GA to set the referrer accordingly - you would to have to force it upon Google Analytics when counting. I don't know whether that is possible.

但是,这仍然无法告诉GA相应地设置引荐来源 - 您必须在计算时强制它在Google Analytics上。我不知道这是否可能。

#1


9  

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.

Google Analytics现在有一个特殊的网址参数utm_referrer,您可以将引荐传递到您要重定向到的网页;这样做将模拟与下面相同的行为,而不需要弄乱cookie或功能。

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.

Google Analytics有一个名为setRefererOverride()的函数。如果您设置它,它将覆盖它跟踪的引用者值到您设置的任何值。

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

执行此操作的最佳方法是,在重定向之前,将实际引荐来源存储在cookie中,如下所示:

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.

然后,读取页面上的cookie值。如果设置了cookie,请在页面浏览之前调用该函数。

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);
}

#2


0  

I think the cleanest (and maybe only) way would be to trigger a GA count in a.php before doing the redirection.

我认为最干净(也许唯一)的方法是在进行重定向之前触发a.php中的GA计数。

If that's not an option, the only way I can see to keep the referrer alive is

如果这不是一个选项,我唯一可以看到保持推荐人活着的方法是

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

(will not work in IE under some circumstances)

(在某些情况下不适用于IE)

But that will still not tell GA to set the referrer accordingly - you would to have to force it upon Google Analytics when counting. I don't know whether that is possible.

但是,这仍然无法告诉GA相应地设置引荐来源 - 您必须在计算时强制它在Google Analytics上。我不知道这是否可能。