JavaScript 重定向:Referer 标头问题

发布于 2024-10-02 01:16:03 字数 915 浏览 3 评论 0原文

有人通过博客链接(例如 http://blog)访问我的网站(例如 http://mysite/a.php)。

所以现在她位于 http://mysite/a.php 页面,并且引用者设置为 http://blog

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

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

现在在对 http://mysite/ 的请求中b.php,Referer 设置为 http://mysite/a.php
因此(我认为是这样)我的 Google Analytics 显示了来自 http://mysite/a.php。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好多鱼好多余 2024-10-09 01:16:03

编辑:新的、推荐的方式

Google Analytics 现在有一个特殊的 URL 参数 utm_referrer,您可以在其中将引用者传递到您要重定向到的页面;这样做将模拟与下面相同的行为,而无需弄乱 cookie 或函数。

在你的例子中,你会让你的代码说:

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

旧的,更难但实用的方式

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

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

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

然后,读取页面上的 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);
}

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);
}
殤城〤 2024-10-09 01:16:03

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

如果这不是一个选项,我能看到保持引荐来源网址活着的唯一方法是

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

(在某些情况下,在 IE 中不起作用情况

但这仍然不会告诉GA相应地设置引荐来源网址 - 您必须在计数时将其强制给Google Analytics。我不知道这是否可能。

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

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)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文