如何在使用 PHP 的重定向页面上使用 Google Analytics 进行跟踪?
我有一个网站,我想跟踪谁点击了 GA 的特定链接.
假设我有这个页面:/index.php?id=32
在此页面上,我根据 ID 变量(在本例中为 32)运行一些查询,并从数据库中获取 32 id 项的 URL 以重定向访问者.
我正在使用 PHP 函数: header('Location: http://www.example.com');.在我重定向用户之前,我希望 Google 捕获访问者的信息,然后才重定向到所需的网页.
我试图在重定向之前粘贴 GA 代码并回显它,但是它没有用.如何使用 GA 跟踪此类页面?
解决方案一般来说
<块引用>如果您的页面使用重定向,则重定向页面将成为着陆页的引荐来源网址.例如,如果您更改了站点,则index.html 现在重定向到 home.html,然后 index.html 变成home.html 的引用.如果有人通过 Google 访问您的网站搜索首先将它们发送到 index.html,您将没有任何数据关于 Google 搜索.
因此,您应该将 Google Analytics 跟踪重定向页面和登陆页面上的代码.这条路,重定向页面将捕获实际的引荐来源信息您的报告.
请注意,某些浏览器实际上可能会在 JavaScript 之前重定向可以从代码调用.
(参见https://support.google.com/analytics/answer/1009614?hl=en)
您的具体情况
由于 PHP 在任何 Javascript 之前呈现和执行,因此 Google Analytics 跟踪器没有机会向其服务器发送数据.
解决方案
考虑到您无法跟踪 PHP 重定向页面,有多种可能的替代方法:
- Javascript 重定向:https://stackoverflow.com/a/4745622/1672895
window.location = "http://www.yoururl.com";
- 元刷新:https://stackoverflow.com/a/8692559/1672895
- 虚拟页面跟踪:https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
ga('send', 'pageview', '/index.php?id=32');
- 广告系列跟踪(在这种特定情况下,我不会亲自使用这种方法.)
/products.php?utm_source=index&utm_medium=redirection-page&utm_campaign=32
在您进入 PHP 重定向页面之前,列表中的最后两项是在初始页面上的各个链接上实现的.
I have a website where I want to track who has clicked on specific links with GA.
Let's say I have this page: /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.
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.
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?
解决方案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.
(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.
Solutions
Considering that you cannot track a PHP redirection page, there are a number of possible alternatives:
- Javascript redirection: 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/">
- Virtual page tracking: 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
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.
相关文章