简体   繁体   中英

Where do I add this Google Analytics custom variable code?

I have this custom variable set and ready to go. Is there an exact spot where this needs to go in the tracking code? We use PHP on the site I currently work on, do I need to change this script to PHP format? Or add it in as a new instance in the PHP analytics file?

If anyone has a better way of doing this please let me know...

Here is what I have so far in the PHP file

/* 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"
            );
    }
}

This is the js bit:

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.
]);

Here is the GoogleCode function in the main PHP file.

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;

}

There was a separate gatracking.js code on the server that I was able to add my custom variables into and it seems to be working now, just have not seen the variables show up yet and someone has to login as one of these users below or register for the view to show. Thanks for your help! I will look into updating our GA code ASAP. Here is my final code for anyone who wants to take a look.

/* 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.
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM