Development

Changeset 7983

You must first sign up to be able to contribute.

Changeset 7983

Show
Ignore:
Timestamp:
03/19/08 18:39:41 (5 years ago)
Author:
Kris.Wallsmith
Message:

sfGoogleAnalyticsPlugin 1.0-RC2

  • Removed dependency on helpers for building Javascript block.
  • Added template slots to the Javascript block inserted by the filter.
  • Added support for PHP's JSON extension.
  • Mixed in convenience method to sfUser to easily add custom tracking vars to flash.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfGoogleAnalyticsPlugin/trunk/config/config.php

    r7895 r7983  
    55sfMixer::register('sfComponent', array('sfGoogleAnalyticsActionMixin', 'addGoogleAnalyticsCustomVar')); 
    66sfMixer::register('sfComponent', array('sfGoogleAnalyticsActionMixin', 'addGoogleAnalyticsCustomVarToFlash')); 
     7 
     8sfMixer::register('sfUser', array('sfGoogleAnalyticsUserMixin', 'addGoogleAnalyticsCustomVarToFlash')); 
  • plugins/sfGoogleAnalyticsPlugin/trunk/lib/action/sfGoogleAnalyticsActionMixin.class.php

    r7895 r7983  
    1818   * @param   string $utParam 
    1919   */ 
    20   public static function setGoogleAnalyticsParam($action, $utParam) 
     20  public static function setGoogleAnalyticsParam(sfComponent $action, $utParam) 
    2121  { 
    2222    sfGoogleAnalyticsToolkit::setParam($utParam); 
     
    3131   * @param   string $value 
    3232   */ 
    33   public static function addGoogleAnalyticsVar($action, $name, $value) 
     33  public static function addGoogleAnalyticsVar(sfComponent $action, $name, $value) 
    3434  { 
    3535    sfGoogleAnalyticsToolkit::addVar($name, $value); 
     
    4343   * @param   string $var 
    4444   */ 
    45   public static function addGoogleAnalyticsCustomVar($action, $var) 
     45  public static function addGoogleAnalyticsCustomVar(sfComponent $action, $var) 
    4646  { 
    4747    sfGoogleAnalyticsToolkit::addCustomVar($var); 
     
    5454   * @param   sfComponent $action 
    5555   * @param   string $var 
     56   * @param   bool $persist 
     57   * @see     comment block for sfGoogleAnalyticsUserMixin::addGoogleAnalyticsCustomVarToFlash() 
     58   * @todo    support updated symfony 1.1 flash architecture 
    5659   */ 
    57   public static function addGoogleAnalyticsCustomVarToFlash($action, $var
     60  public static function addGoogleAnalyticsCustomVarToFlash(sfComponent $action, $var, $persist = true
    5861  { 
    5962    $vars = $action->getFlash('google_analytics_custom_vars', array()); 
    6063    $vars[] = $var; 
    61     $action->setFlash('google_analytics_custom_vars', $vars); 
     64    $action->setFlash('google_analytics_custom_vars', $vars, $persist); 
    6265  } 
    6366   
  • plugins/sfGoogleAnalyticsPlugin/trunk/lib/filter/sfGoogleAnalyticsFilter.class.php

    r7895 r7983  
    114114     
    115115    $oldContent = $response->getContent(); 
    116     $newContent = preg_replace('/\<body[^\>]*\>/i', "$0".$trackingCode, $oldContent, 1); 
     116    $newContent = preg_replace('/\<body[^\>]*\>/i', "$0\n".$trackingCode, $oldContent, 1); 
    117117     
    118118    if ($oldContent == $newContent) 
     
    135135     
    136136    $oldContent = $response->getContent(); 
    137     $newContent = str_ireplace('</body>', $trackingCode.'</body>', $oldContent); 
     137    $newContent = str_ireplace('</body>', $trackingCode."\n</body>", $oldContent); 
    138138     
    139139    if ($oldContent == $newContent) 
  • plugins/sfGoogleAnalyticsPlugin/trunk/lib/util/sfGoogleAnalyticsToolkit.class.php

    r7895 r7983  
    2020  public static function getHtml() 
    2121  { 
    22     sfLoader::loadHelpers(array('Escaping', 'Asset')); 
    23      
    24     $context = sfContext::getInstance(); 
    25     $request = $context->getRequest(); 
    26     $module  = $context->getModuleName(); 
    27     $action  = $context->getActionName(); 
    28      
    29     $actionConfig = sfConfig::get('mod_'.$module.'_'.$action.'_google_analytics', array()); 
    30      
    31     $usrc = $request->isSecure() ?  
     22    sfLoader::loadHelpers(array('Partial')); 
     23     
     24    $usrc = sfContext::getInstance()->getRequest()->isSecure() ?  
    3225      sfConfig::get('app_google_analytics_usrc_ssl', 'https://ssl.google-analytics.com/urchin.js') :  
    3326      sfConfig::get('app_google_analytics_usrc', 'http://www.google-analytics.com/urchin.js'); 
    3427     
    35     // initial parameter 
    36     $utParam = ''; 
    37     if (isset($actionConfig['ut_param'])) 
    38     { 
    39       $utParam = $actionConfig['ut_param']; 
    40     } 
    41     else 
    42     { 
    43       $utParam = sfConfig::get('app_google_analytics_ut_param'); 
    44       $utParam = sfConfig::get('mod_'.$module.'_google_analytics_ut_param', $utParam); 
    45     } 
    46      
    47     // initialization variables 
    48     $vars = sfConfig::get('app_google_analytics_vars', array()); 
    49     $vars = array_merge($vars, sfConfig::get('mod_'.$module.'_google_analytics_vars', array())); 
    50     if (isset($actionConfig['vars']) && is_array($actionConfig['vars'])) 
    51     { 
    52       $vars = array_merge($vars, $actionConfig['vars']); 
    53     } 
    54      
    55     // account number is required 
    56     if (!isset($vars['uacct']) && !isset($vars['_uacct'])) 
     28    // capture and prepare all javascript variable to be declared before the 
     29    // initial call to urchinTracker() - confirm we have a value for _uacct. 
     30    $varsConfig = self::getFinalConfigValue('vars', true); 
     31    if (!isset($varsConfig['uacct']) && !isset($varsConfig['_uacct'])) 
    5732    { 
    5833      // backwards compatibility 
    59       $vars['uacct'] = sfConfig::get('app_google_analytics_uacct'); 
    60       if (!$vars['uacct']) 
     34      $varsConfig['uacct'] = sfConfig::get('app_google_analytics_uacct'); 
     35      if (!$varsConfig['uacct']) 
    6136      { 
    6237        throw new sfGoogleAnalyticsException('Please add your Google Analytics account number to your app.yml.'); 
    6338      } 
    6439    } 
    65      
    66     // prep the initial parameter 
    67     if ($utParam) 
    68     { 
    69       $utParam = sprintf('"%s"', esc_js_no_entities($utParam)); 
    70       $utParam = str_replace('\\/', '/', $utParam); 
    71     } 
    72      
    73     // build initialization variables 
    74     $jsVars = array(); 
    75     foreach ($vars as $key => $value) 
     40    $vars = array(); 
     41    foreach ($varsConfig as $key => $value) 
    7642    { 
    7743      if ($key{0} != '_') 
     
    7945        $key = '_'.$key; 
    8046      } 
    81       $jsVars[] = sprintf("%s=\"%s\";", $key, esc_js_no_entities($value)); 
    82     } 
    83     $jsVars = join("\n", $jsVars); 
    84      
    85     // custom variables 
    86     $custom = sfConfig::get('app_google_analytics_custom', array()); 
    87     $custom = array_merge($custom, sfConfig::get('mod_'.$module.'_google_analytics_custom', array())); 
    88     if (isset($actionConfig['custom']) && is_array($actionConfig['custom'])) 
    89     { 
    90       $custom = array_merge($custom, $actionConfig['custom']); 
    91     } 
    92      
    93     $jsCustom = array(); 
    94     foreach ($custom as $value) 
    95     { 
    96       $jsCustom[] = sprintf('__utmSetVar("%s");', esc_js_no_entities($value)); 
    97     } 
    98     $jsCustom = join("\n", $jsCustom); 
    99      
    100     $html  = javascript_include_tag($usrc); 
    101     $html .= javascript_tag(sprintf("%s\nurchinTracker(%s);\n%s", $jsVars, $utParam, $jsCustom)); 
     47      $vars[] = $key.'='.self::escape($value); 
     48    } 
     49     
     50    // capture and prepare the parameter for the initial call to urchinTracker() 
     51    $utParam = self::getFinalConfigValue('ut_param'); 
     52    if ($utParam) 
     53    { 
     54      $utParam = self::escape($utParam); 
     55    } 
     56     
     57    // capture and prepare any custom tracking variables that have been  
     58    // configured for this response. 
     59    $customConfig = self::getFinalConfigValue('custom', true); 
     60    $custom = array(); 
     61    foreach ($customConfig as $value) 
     62    { 
     63      $custom[] = sprintf('__utmSetVar(%s);', self::escape($value)); 
     64    } 
     65     
     66    // build the html block for insertion 
     67    $html   = array(''); 
     68    $html[] = sprintf('<script type="text/javascript" src="%s"></script>', $usrc); 
     69    $html[] = '<script type="text/javascript">'; 
     70    $html[] = get_slot('google_analytics_top'); 
     71    $html[] = join("\n", $vars); 
     72    $html[] = sprintf('urchinTracker(%s);', $utParam); 
     73    $html[] = join("\n", $custom); 
     74    $html[] = get_slot('google_analytics_bottom'); 
     75    $html[] = '</script>'; 
     76     
     77    $html = array_unique($html); 
     78    $html = join("\n", array_slice($html, 1)); 
    10279     
    10380    return $html; 
     
    174151   
    175152  /** 
     153   * Escape a string value for Javascript. 
     154   *  
     155   * This method will use the JSON extension if it is available. 
     156   *  
     157   * @author  Kris Wallsmith 
     158   * @param   string $value 
     159   * @return  string 
     160   */ 
     161  protected static function escape($value) 
     162  { 
     163    if (function_exists('json_encode')) 
     164    { 
     165      $escaped = json_encode($value); 
     166    } 
     167    else 
     168    { 
     169      sfLoader::loadHelpers(array('Escaping')); 
     170      $escaped = '"'.esc_js($value).'"'; 
     171    } 
     172     
     173    return $escaped; 
     174  } 
     175   
     176  /** 
     177   * Get a configuration value. 
     178   *  
     179   * Checks application, module and action-level configuration. 
     180   *  
     181   * @author  Kris Wallsmith 
     182   * @param   string $key 
     183   * @param   bool $merge - treat value as array and merge configs 
     184   * @return  mixed 
     185   */ 
     186  protected static function getFinalConfigValue($key, $merge = false) 
     187  { 
     188    $module = sfContext::getInstance()->getModuleName(); 
     189     
     190    $actionConfig = self::getActionConfig(); 
     191    $actionConfig = isset($actionConfig[$key]) ? $actionConfig[$key] : null; 
     192     
     193    $moduleConfig = sfConfig::get('mod_'.$module.'_google_analytics_'.$key); 
     194    $appConfig = sfConfig::get('app_google_analytics_'.$key); 
     195     
     196    if ($merge) 
     197    { 
     198      $value = array_merge((array) $appConfig, (array) $moduleConfig, (array) $actionConfig); 
     199    } 
     200    else 
     201    { 
     202      $value = $actionConfig ? $actionConfig : ($moduleConfig ? $moduleConfig : $appConfig); 
     203    } 
     204     
     205    return $value; 
     206  } 
     207   
     208  /** 
    176209   * Get the Google Analytics configuration for the current action. 
    177210   *  
  • plugins/sfGoogleAnalyticsPlugin/trunk/package.xml

    r7189 r7983  
    1111    <active>yes</active> 
    1212  </lead> 
    13   <date>2008-01-25</date> 
     13  <date>2008-03-19</date> 
    1414  <version> 
    15     <release>1.0-RC1</release> 
    16     <api>1.0-RC1</api> 
     15    <release>1.0-RC2</release> 
     16    <api>1.0-RC2</api> 
    1717  </version> 
    1818  <stability> 
     
    4040        <dir name="helper"> 
    4141          <file role="data" name="GoogleAnalyticsHelper.php"/> 
     42        </dir> 
     43        <dir name="user"> 
     44          <file role="data" name="sfGoogleAnalyticsUserMixin.class.php"/> 
    4245        </dir> 
    4346        <dir name="util">