javascript 如何在使用 PHP 的重定向页面上使用 Google Analytics 进行跟踪?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27453830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to track with Google Analytics on a redirection page with PHP?
提问by Radical_Activity
I have a website where I want to track who has clicked on specific links with GA.
我有一个网站,我想跟踪谁点击了 GA 的特定链接。
Let's say I have this page: /index.php?id=32
假设我有这个页面:/index.php?id=32
On this page I run some query based on the ID variable (in this case: 32), and I get the URL of the 32 id item from the Database to redirect the visitor.
在此页面上,我根据 ID 变量(在本例中为:32)运行一些查询,并从数据库中获取 32 id 项的 URL 以重定向访问者。
I'm using a PHP function: header('Location: http://www.example.com');. Before I'm redirecting the user, I want Google to capture the visitor's information and only then redirect to the desired webpage.
我正在使用 PHP 函数:header('Location: http://www.example.com');。在我重定向用户之前,我希望 Google 捕获访问者的信息,然后才重定向到所需的网页。
I have tried to paste the GA code and ECHO it just before the redirection, however it did not work. How is it possible to track these kind of pages with GA?
我试图在重定向之前粘贴 GA 代码并回显它,但是它不起作用。如何使用 GA 跟踪这些类型的页面?
采纳答案by carlodurso
Generally speaking
通常来说,一般来说
If your page uses redirects, the redirecting page becomes the landing page's referrer. For example, if you've changed your site so that index.html now redirects to home.html, then index.html becomes the referrer for home.html. If someone reached your site via a Google search that sent them first to index.html, you won't have any data regarding the Google search.
For this reason, you should place the Google Analytics tracking code on the redirecting page as well as on the landing page. This way, the redirecting page will capture the actual referrer information for your reports.
Note, some browsers may actually redirect before the JavaScript call from the code can be made.
如果您的页面使用重定向,则重定向页面将成为登录页面的引荐来源。例如,如果您更改了站点,以便 index.html 现在重定向到 home.html,那么 index.html 将成为 home.html 的引用。如果有人通过 Google 搜索访问您的网站,并首先将他们发送到 index.html,那么您将不会获得有关 Google 搜索的任何数据。
因此,您应该将 Google Analytics 跟踪代码放置在重定向页面和登录页面上。这样,重定向页面将捕获报告的实际引用信息。
请注意,某些浏览器实际上可能会在从代码调用 JavaScript 之前进行重定向。
(cf. https://support.google.com/analytics/answer/1009614?hl=en)
(参见https://support.google.com/analytics/answer/1009614?hl=en)
Your specific case
您的具体情况
Since PHP is rendered and executed before any Javascript, Google Analytics tracker has no chance to send data to its server.
由于 PHP 在任何 Javascript 之前呈现和执行,因此 Google Analytics 跟踪器没有机会将数据发送到其服务器。
Solutions
解决方案
Considering that you cannot track a PHP redirection page, there are a number of possible alternatives:
考虑到您无法跟踪 PHP 重定向页面,有多种可能的替代方法:
- Javascript redirection: https://stackoverflow.com/a/4745622/1672895
window.location = "http://www.yoururl.com";
- Javascript 重定向:https: //stackoverflow.com/a/4745622/1672895
window.location = "http://www.yoururl.com";
- Meta Refresh: https://stackoverflow.com/a/8692559/1672895
<meta http-equiv="refresh" content="5; url=http://example.com/">
- 元刷新:https: //stackoverflow.com/a/8692559/1672895
<meta http-equiv="refresh" content="5; url=http://example.com/">
- Virtual page tracking: https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
ga('send', 'pageview', '/index.php?id=32');
- 虚拟页面跟踪:https: //developers.google.com/analytics/devguides/collection/analyticsjs/pages
ga('send', 'pageview', '/index.php?id=32');
- Campaign tracking (I wouldn't personally use this method in this specific case.)
/products.php?utm_source=index&utm_medium=redirection-page&utm_campaign=32
- 广告系列跟踪(在这种特定情况下,我不会亲自使用此方法。)
/products.php?utm_source=index&utm_medium=redirection-page&utm_campaign=32
The last two items in the list are implemented on the individual links on the initial page before you get on the PHP redirection page.
在您进入 PHP 重定向页面之前,列表中的最后两项是在初始页面上的各个链接上实现的。
回答by sfosdal
I would suggest something like the following. It uses hitCallback to do the redirect so the event will be sent to GA and thenthe redirect will occur. It also sets a Timeout so that should analytics.js be unavailable (for whatever reason) the viewer is still (eventually) sent to the correct place.
我会建议类似以下内容。它使用 hitCallback 进行重定向,因此事件将被发送到 GA,然后将发生重定向。它还设置了一个超时,以便如果 analytics.js 不可用(无论出于何种原因),查看器仍然(最终)被发送到正确的位置。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>example</title>
<script>
setTimeout(redirect, 2000);
var redirected = false;
function redirect() {
if (!redirected) {
redirected = true;
location.href = 'http://your.website.here.com';
}
}
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-NNNNNNN-N', 'auto');
ga('send', 'event', 'Event Name', 'redirect', {
hitCallback: redirect
});
</script>
</head>
<body>
</body>
</html>
tracking codes and urls should of course be updated to match yours.
跟踪代码和网址当然应该更新以匹配您的。
References:
参考:
回答by Eike Pierstorff
If you would look at your error log you'd see an error that roughly says "Cannot send redirect header, header was already sent". It is not possible to do a redirect via location header when you already have had HTML/Javascript output before your PHP snippet (because at that moment you have already implicitly send your http headers).
如果您查看错误日志,您会看到一条错误消息,大致说“无法发送重定向标头,标头已发送”。当您在 PHP 代码段之前已经有 HTML/Javascript 输出时,不可能通过位置标头进行重定向(因为那时您已经隐式发送了您的 http 标头)。
It would be possible to use Googles measurement protocolto create a server-side solution that tracks the page before redirect. In that case you would need to create a client_id yourself and, in case the properties you redirect are your own, append it to the redirect url for cross-domain tracking.
可以使用 Google 的测量协议来创建服务器端解决方案,在重定向之前跟踪页面。在这种情况下,您需要自己创建一个 client_id,如果您重定向的属性是您自己的,请将其附加到重定向 url 以进行跨域跟踪。
If that's not concern you could use Curl or fopen to send a call to GA via a Url like this:
如果这不是问题,您可以使用 Curl 或 fopen 通过这样的 Url 向 GA 发送呼叫:
https://www.google-analytics.com/collect?v=1&tid=UA-XXXX-Y&cid=XXXXXXXXt=pageview&dp=%2redirect
https://www.google-analytics.com/collect?v=1&tid=UA-XXXX-Y&cid=XXXXXXXXt=pageview&dp=%2redirect
Where UA-XXXX-Y ist the account id from google, cid is the client id you generated and /redirect is the name of your page. If you want to recognize recurring users you need to save the client id to a cookie and check for every visit if the cookie exists and retrieve the id if it does.
其中 UA-XXXX-Y 是来自 google 的帐户 ID,cid 是您生成的客户端 ID,/redirect 是您的页面名称。如果您想识别经常出现的用户,您需要将客户端 ID 保存到 cookie 并检查每次访问是否存在 cookie,如果存在则检索 id。
回答by Peter
Since the page that is sending visitors to the redirect is also under your control, I would suggest a totally different approach: use event tracking on the source page.
由于将访问者发送到重定向的页面也在您的控制之下,我建议采用完全不同的方法:在源页面上使用事件跟踪。
On the page that has the /index.php?id=32
link, measure all clicks on this link by attaching an onClick
handler to the link and executing something like
在具有/index.php?id=32
链接的页面上,通过将onClick
处理程序附加到链接并执行类似的操作来衡量此链接上的所有点击
ga('send', 'event', {
eventCategory: 'Redirect Link',
eventAction: 'click',
eventLabel: event.target.href
});
回答by Shan Robertson
if you disable the redirect, and inspect the page, do you see the call being made to GA in your network tab? I've found that sometimes the push events don't show up in GA for like 24 hours.
如果您禁用重定向并检查页面,您是否在网络选项卡中看到对 GA 的调用?我发现有时推送事件不会在 GA 中显示 24 小时。
If you see that it is indeed sending out to google then I would leave it enabled, and wait a day before checking GA to see if anything has been logged.
如果你看到它确实发送给谷歌,那么我会让它保持启用状态,并在检查 GA 之前等待一天以查看是否已记录任何内容。