在哪里添加此 Google Analytics 自定义变量代码?
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;
}
服务器上有一个单独的 gatracking.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.
]);
我已设置此自定义变量并准备就绪。在跟踪代码中是否有一个确切的位置?我们在我当前工作的站点上使用 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;
}
服务器上有一个单独的 gatracking.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.
]);