我在哪里添加这个谷歌分析自定义变量代码
Where do I add this Google Analytics custom variable code?
我有了这个自定义变量集,可以开始了。跟踪代码中是否有一个确切的位置?我们在我目前工作的网站上使用PHP,我需要将这个脚本更改为PHP格式吗?或者将其作为新实例添加到PHP分析文件中?
如果有人有更好的方法,请告诉我。。。
以下是我目前在PHP文件中的内容
/* Injects GA tracking code & adds a external JS file
* to track user types */
public function GoogleAnalyticsUserTypes() {
if(DEFINED('GaTrackingCode')) {
$gacode = 'var _gaq = _gaq||[];' . $this->GoogleCode();
$gacode = $this->Compress($gacode);
Requirements::customScript($gacode);
if (defined('GaTrackingCode'))
Requirements::javascript(
basename(dirname(dirname(__FILE__))) . "/javascript/user-types.js"
);
}
}
这是js位:
var _gaq = _gaq || [];
_gaq.push(['_setCustomVar',
1, // This custom var is set to slot #1. Required parameter.
'User Type', // The name of the custom variable. Required parameter.
'Journalist', // Sets the value. Required parameter.
2 // Sets the scope to session-level. Optional parameter.
]);
这是PHP主文件中的GoogleCode函数。
protected function GoogleCode(){
$statusCode = Controller::curr()->getResponse()->getStatusCode();
$trackingCode = (defined('GaTrackingCode')) ? GaTrackingCode : false;
$SecondaryTrackingCode = (defined('GaTrackingCodeSecondary')) ? GaTrackingCodeSecondary : false;
$tracker = array();
if ($trackingCode) array_push($tracker, '["_setAccount","' . $trackingCode . '"]');
if ($SecondaryTrackingCode) array_push($tracker, '["b._setAccount","' . $SecondaryTrackingCode . '"]');
if ($statusCode == 404 || $statusCode == 500) {
$ecode = ($statusCode == 404) ? 'Page Not Found' : 'Page Error';
if ($trackingCode) array_push($tracker, '["_trackEvent","' . $ecode . '",d.location.pathname + d.location.search, d.referrer]');
if ($SecondaryTrackingCode) array_push($tracker, '["b._trackEvent","' . $ecode . '",d.location.pathname + d.location.search, d.referrer]');
}
else if ($trackingCode) {
if ($trackingCode) array_push($tracker, '["_trackPageview"]');
if ($SecondaryTrackingCode) array_push($tracker, '["b._trackPageview"]');
}
$code = 'var d = document; _gaq.push(' . implode($tracker, ',').');';
$code .= ($SecondaryTrackingCode) ? '_gaq2=!0;' : '_gaq2=!1;';
$gacode = '
(function(){
var ga = d.createElement("script"); ga.type = "text/javascript"; ga.async = true;
ga.src = ("https:" == d.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
var s = d.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga,s);
})();';
/* Only add GA JavaScript if live */
if (Director::isLive() && !$this->isIgnored())
$code .= $gacode;
return $code;
}
服务器上有一个单独的gatrackin.js代码,我可以将我的自定义变量添加到其中,现在它似乎正在工作,只是还没有看到变量显示,必须有人在下面以这些用户之一的身份登录或注册视图才能显示。谢谢你的帮助!我会考虑尽快更新我们的GA代码。这是我的最后一个代码,供任何想看一看的人使用。
/* Attach tracking to all download & external links */
var _gaq = _gaq || [];
_gaq.push(['_setCustomVar',
1, // This custom var is set to Key #1 in Google Analytics under Audience > Custom > Custom Variables. Required parameter.
'User Type', // The name of the custom variable. Required parameter.
'Journalist', // Sets the value of "User Type" to ' ' depending on status. Required parameter.
2 // Sets the scope to session-level. Optional parameter.
]);
_gaq.push(['_setCustomVar',
2, // This custom var is set to Key #2 in Google Analytics under Audience > Custom > Custom Variables. Required parameter.
'User Type', // The name of the custom variable. Required parameter.
'Academic', // Sets the value of "User Type" to ' ' depending on status. Required parameter.
2 // Sets the scope to session-level. Optional parameter.
]);
_gaq.push(['_setCustomVar',
3, // This custom var is set to Key #3 in Google Analytics under Audience > Custom > Custom Variables. Required parameter.
'User Type', // The name of the custom variable. Required parameter.
'Advocate', // Sets the value of "User Type" to ' ' depending on status. Required parameter.
2 // Sets the scope to session-level. Optional parameter.
]);
_gaq.push(['_setCustomVar',
4, // This custom var is set to Key #4 in Google Analytics under Audience > Custom > Custom Variables. Required parameter.
'User Type', // The name of the custom variable. Required parameter.
'Government', // Sets the value of "User Type" to ' ' depending on status. Required parameter.
2 // Sets the scope to session-level. Optional parameter.
]);
_gaq.push(['_setCustomVar',
5, // This custom var is set to Key #5 in Google Analytics under Audience > Custom > Custom Variables. Required parameter.
'User Type', // The name of the custom variable. Required parameter.
'Other', // Sets the value of "User Type" to ' ' depending on status. Required parameter.
2 // Sets the scope to session-level. Optional parameter.
]);
相关文章:
- 像createComment这样的各种自定义变量名在内联javascript中被覆盖,但在外部js中没有.为什么?
- 清除 Javascript 中窗口对象中的自定义变量
- 谷歌分析自定义变量使用情况
- 是否可以在使用电子商务扩展时将自定义变量发送到谷歌分析
- 将“this”引用到自定义变量
- 将自定义变量传递给匿名 jQuery 点击函数
- 您可以将自定义变量传递到Google Analytics事件跟踪中吗?
- 在Google Analytics中使用自定义变量对事件进行分组
- JS-无法将AJAX的响应分配为自定义变量
- 无法使Google Analytics自定义变量工作
- 我在哪里添加这个谷歌分析自定义变量代码
- 如何发送'自定义变量'测量协议(GA)中的参数
- Javascript,异步,发送自定义变量
- 如何在新的谷歌分析代码中放置自定义变量
- 如何创建包含自定义变量的函数
- 获取或填充自定义变量与谷歌地图方向的响应
- 谷歌分析AJAX页面浏览量和自定义变量
- 添加一个自定义变量作为JSON属性
- 向对象添加自定义变量
- 谷歌分析;对事件使用自定义变量