[英]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.
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.