关于javascript:如何使用PHP在重定向页面上使用Google Analytics跟踪?

How to track with Google Analytics on a redirection page with PHP?

我有一个网站,我想跟踪谁点击了特定的链接与GA。

假设我有这个页面:/index.php?ID=32

在这个页面上,我运行了一些基于id变量的查询(在本例中是32),并从数据库中获取32 id项的URL来重定向访问者。

我使用的是php函数:header('location:http://www.example.com');。在我重定向用户之前,我希望谷歌捕获访问者的信息,然后再重定向到所需的网页。

我尝试粘贴GA代码并在重定向之前对其进行回显,但是它不起作用。如何用GA跟踪此类页面?


一般来说

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.

(参见https://support.google.com/analytics/answer/1009614?HL= EN)

你的具体情况

由于php是在任何javascript之前呈现和执行的,所以google analytics tracker没有机会向其服务器发送数据。

< BR>

解决

考虑到无法跟踪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< BR>

在进入php重定向页面之前,列表中的最后两项在初始页面上的各个链接上实现。


我建议如下。它使用HitCallback进行重定向,这样事件将被发送到GA,然后重定向将发生。它还设置了一个超时,以便在analytics.js不可用(无论出于什么原因)时,查看器仍然(最终)被发送到正确的位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        example
       
            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
            });
       
    </head>
    <body>
    </body>
</html>

跟踪代码和URL当然应该更新以匹配您的。

参考文献:

  • 处理超时
  • 命中回调


如果你查看你的错误日志,你会看到一个错误,粗略地说"不能发送重定向头,头已经发送"。如果在php代码段之前已经有了html/javascript输出,则不可能通过location header进行重定向(因为此时您已经隐式地发送了http头)。

可以使用谷歌测量协议创建一个服务器端解决方案,在重定向之前跟踪页面。在这种情况下,您需要自己创建一个客户机ID,如果您重定向的属性是您自己的,请将其附加到跨域跟踪的重定向URL。

如果这不是问题,您可以使用curl或fopen通过以下URL向ga发送呼叫:

https://www.google-analytics.com/collect?v=1&tid=UA-XXXX-Y&cid=XXXXXXXXt=pageview&dp=%2redirect

其中,ua-XXXX-Y是来自Google的帐户ID,cid是您生成的客户机ID,/redirect是您页面的名称。如果要识别重复出现的用户,则需要将客户机ID保存到cookie中,并检查每次访问是否存在cookie,如果存在,则检索ID。


由于发送访问者到重定向的页面也在您的控制之下,我建议使用一种完全不同的方法:在源页面上使用事件跟踪。

在拥有/index.php?id=32链接的页面上,通过将onClick处理程序附加到链接并执行类似的操作来测量此链接上的所有单击。

1
2
3
4
5
ga('send', 'event', {
  eventCategory: 'Redirect Link',
  eventAction: 'click',
  eventLabel: event.target.href
});

如果禁用重定向并检查页面,是否在网络选项卡中看到对GA的调用?我发现有时候推送事件在GA中不会出现24小时。

如果你看到它确实在发送给谷歌,那么我会让它保持启用状态,然后等一天再检查GA,看看是否有任何记录。