我在哪里添加这个谷歌分析自定义变量代码

Where do I add this Google Analytics custom variable code?

本文关键字:自定义 变量 代码 谷歌 在哪里 添加      更新时间:2023-09-26

我有了这个自定义变量集,可以开始了。跟踪代码中是否有一个确切的位置?我们在我目前工作的网站上使用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.
]);