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

时间:2015-12-01 20:12:13

标签: javascript google-analytics tracking

我已经准备好了这个自定义变量。在跟踪代码中是否存在需要的确切位置?我们在目前正在使用的网站上使用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;

}

1 个答案:

答案 0 :(得分:0)

在服务器上有一个单独的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.
]);