Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我在哪里添加这个Google Analytics自定义变量代码?_Javascript_Google Analytics_Tracking - Fatal编程技术网

Javascript 我在哪里添加这个Google Analytics自定义变量代码?

Javascript 我在哪里添加这个Google Analytics自定义变量代码?,javascript,google-analytics,tracking,Javascript,Google Analytics,Tracking,我已经设置了这个自定义变量,可以开始了。在跟踪代码中是否有一个精确的位置?我们在我目前工作的网站上使用PHP,我需要将此脚本更改为PHP格式吗?或者将其作为新实例添加到PHP分析文件中 如果有人有更好的方法,请让我知道 以下是我到目前为止在PHP文件中看到的内容 /* Injects GA tracking code & adds a external JS file * to track user types */ public function GoogleAnalyticsUs

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

这需要在创建跟踪器之后和发送交互点击(例如pageview)之前放置(我想这可能在$This->GoogleCode()方法中)。谢谢,我添加了GoogleCode函数。也许它仍然会出现&时间并没有那么长&说它通常需要4个小时或更长时间。但这是我第一次这么做,所以我不知道。如果有人知道我应该在哪里添加这个,我会全神贯注。这将需要一段时间(至少几个小时,最多一天)。您应该考虑将分析代码升级到当前版本(通用分析),并从自定义VARS切换到自定义维度。你正在使用的代码将不允许你使用当前分析版本的所有功能。我猜谷歌会很容易地更新它,类似于其他项目。谢谢你的评论!