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代码和ECHO,但它没有用。如何用GA跟踪这类页面?
4 个解决方案
#1
15
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.
如果您的网页使用重定向,则重定向页面将成为着陆页的引荐来源。例如,如果您更改了网站以便index.html现在重定向到home.html,那么index.html将成为home.html的引荐来源。如果有人通过首先将其发送到index.html的Google搜索到达了您的网站,则您将无法获得有关Google搜索的任何数据。
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.
因此,您应将Google Analytics跟踪代码放在重定向页面以及着陆页上。这样,重定向页面将捕获报告的实际引荐来源信息。
Note, some browsers may actually redirect before the JavaScript call from the code can be made.
请注意,某些浏览器实际上可能会在代码的JavaScript调用之前重定向。
(cf. 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://*.com/a/4745622/1672895
window.location = "http://www.yoururl.com";
window.location =“http://www.yoururl.com”;
Javascript重定向:https://*.com/a/4745622/1672895 window.location =“http://www.yoururl.com”;
- Meta Refresh: https://*.com/a/8692559/1672895
<meta http-equiv="refresh" content="5; url=http://example.com/">
元刷新:https://*.com/a/8692559/1672895
- Virtual page tracking: https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
ga('send', 'pageview', '/index.php?id=32');
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
-
广告系列跟踪(在这种特定情况下我不会亲自使用此方法。)/ product.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重定向页面之前,列表中的最后两项在初始页面上的各个链接上实现。
#2
6
I would suggest something like the following. It uses hitCallback to do the redirect so the event will be sent to GA and then the 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,然后重定向将发生。它还设置了一个Timeout,以便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:
#3
2
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 protocol to 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.
可以使用谷歌测量协议来创建服务器端解决方案,在重定向之前跟踪页面。在这种情况下,您需要自己创建一个client_id,如果您重定向的属性是您自己的属性,请将其附加到重定向网址以进行跨域跟踪。
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
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是来自谷歌的帐户ID,cid是您生成的客户端ID,/ redirect是您的页面名称。如果要识别定期用户,则需要将客户端ID保存到cookie,并检查cookie是否存在时的每次访问,如果存在,则检索id。
#4
0
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以查看是否记录了任何内容。
#1
15
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.
如果您的网页使用重定向,则重定向页面将成为着陆页的引荐来源。例如,如果您更改了网站以便index.html现在重定向到home.html,那么index.html将成为home.html的引荐来源。如果有人通过首先将其发送到index.html的Google搜索到达了您的网站,则您将无法获得有关Google搜索的任何数据。
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.
因此,您应将Google Analytics跟踪代码放在重定向页面以及着陆页上。这样,重定向页面将捕获报告的实际引荐来源信息。
Note, some browsers may actually redirect before the JavaScript call from the code can be made.
请注意,某些浏览器实际上可能会在代码的JavaScript调用之前重定向。
(cf. 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://*.com/a/4745622/1672895
window.location = "http://www.yoururl.com";
window.location =“http://www.yoururl.com”;
Javascript重定向:https://*.com/a/4745622/1672895 window.location =“http://www.yoururl.com”;
- Meta Refresh: https://*.com/a/8692559/1672895
<meta http-equiv="refresh" content="5; url=http://example.com/">
元刷新:https://*.com/a/8692559/1672895
- Virtual page tracking: https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
ga('send', 'pageview', '/index.php?id=32');
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
-
广告系列跟踪(在这种特定情况下我不会亲自使用此方法。)/ product.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重定向页面之前,列表中的最后两项在初始页面上的各个链接上实现。
#2
6
I would suggest something like the following. It uses hitCallback to do the redirect so the event will be sent to GA and then the 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,然后重定向将发生。它还设置了一个Timeout,以便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:
#3
2
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 protocol to 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.
可以使用谷歌测量协议来创建服务器端解决方案,在重定向之前跟踪页面。在这种情况下,您需要自己创建一个client_id,如果您重定向的属性是您自己的属性,请将其附加到重定向网址以进行跨域跟踪。
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
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是来自谷歌的帐户ID,cid是您生成的客户端ID,/ redirect是您的页面名称。如果要识别定期用户,则需要将客户端ID保存到cookie,并检查cookie是否存在时的每次访问,如果存在,则检索id。
#4
0
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以查看是否记录了任何内容。