php - Google Analytics(分析)自定义变量不会跟踪

标签 php google-analytics

好的,一段时间以来,我一直在使用自定义变量感到困惑。我已经搜索并遵循了网络上所有帖子的说明,但无论出于何种原因,它根本不起作用。

这是我正在使用的代码

<script type="text/javascript">
var _gaq = _gaq || [];
var pluginUrl = '//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
_gaq.push(['_setDomainName', 'domain.com']);
_gaq.push(['_setCustomVar',1,'Enquiry Type','e-Notification']);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

这里值得注意的一件事是设置自定义变量的行包含在 PHP IF 语句中。本质上,我只想在提交表单后设置此变量,以便我可以从选择框中捕获值。

归根结底,我要做的就是让人们填写表格并获取选择框的值以传回 Google Analytics。我不希望在每个页面加载时都设置该值,因此一旦 _setCustomVar 已经运行,一旦我破坏了 session 值,就不会再次调用它(当然,除非表单第二次完成)。

这是构造为与 GIF 一起传递的参数(以防它有助于调试)
utmac=UA-xxxxxx-x
utmcc=__utma=247537282.1869531581.1359929325.1365982402.1365984281.5;+__utmz=247537282.1365982402.4.3.utmcsr=domain.com|utmccn=(referral)|utmcmd=referral|utmcct=/;
utmcs=UTF-8
utmdt=Request A Call Back
utme=8(Enquiry Type)9(e-Notification)
utmfl=-
utmhid=1514805008
utmhn=www2.domain.com
utmht=1365984805826
utmje=1
utmn=1234674801
utmp=/request-call-back.php?action=sent&oForm=2
utmr=0
utms=7
utmsc=24-bit
utmsr=1680x1050
utmu=qR~
utmul=en-us
utmvp=1680x672
utmwv=5.4.1

通过我用来绕过 PHP IF 语句的各种测试,我取得了“一些”成功。换句话说,我有时可以将我的测试数据放入自定义变量报告中,但大多数时候我不能。关于为什么虚拟测试有效和无效似乎没有一致的逻辑。

最后一件事,需要使用 _setDomainName,因为帐户上的域是“domain.com”,但包含脚本的站点是“www2.domain.com”。我不认为这对这个问题很重要,而只是另一件事要记住。

有任何想法吗?

最佳答案

你可能错过了第四个参数scope在 _setCustomVar 方法中:

_gaq.push(['_setCustomVar',
      1,                   // This custom var is set to slot #1.  Required parameter.
      'Items Removed',     // The name acts as a kind of category for the user activity.  Required parameter.
      'Yes',               // This value of the custom variable.  Required parameter.
      2                    // Sets the scope to session-level.  Optional parameter.
   ]);

因此,如果您在一个页面上设置了值而在同一页面上根本不设置它(甚至没有设置为“null”),则值可能会停留在最后一级。每次访问和每个用户级别都相同。

您应该将变量视为属于用户(生命周期)、 session (访问)或单个页面 View 的一种“属性”。

根据我的经验,在 90% 的实现中处理这个问题都是错误的。

此外:省略意味着页面级别意味着每次点击都会增加一个值(或不增加一个值)。

备注:我假设,根据您的问题,这应该是 session 级别 (2) 而不是页面级别 (3)。并且只有在状态转换时才应该设置它,这是您已经在 PHP 中实现的。

关于php - Google Analytics(分析)自定义变量不会跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16005910/

相关文章:

php - 如何为用户发布的问题提供唯一的 url/id?

google-analytics - 如何正确使用谷歌分析测量协议(protocol)?

c# - 谷歌身份验证 C#

php - 如何获取 PHP 上传目录?

php - 将 $_session 和 session_start() 与 int 列一起使用

php - 在 PHP 中循环执行 MySQL 查询

php - Laravel 5.1 undefined variable : comments

hadoop - 大数据集用于大数据分析

vb.net - Google OAuth token 错误 - 400 错误请求

jquery - 获取最后一天的访问Google Analytics(分析)