Changeset 7983
- Timestamp:
- 03/19/08 18:39:41 (5 years ago)
- Files:
-
- plugins/sfGoogleAnalyticsPlugin/trunk/config/config.php (modified) (1 diff)
- plugins/sfGoogleAnalyticsPlugin/trunk/lib/action/sfGoogleAnalyticsActionMixin.class.php (modified) (4 diffs)
- plugins/sfGoogleAnalyticsPlugin/trunk/lib/filter/sfGoogleAnalyticsFilter.class.php (modified) (2 diffs)
- plugins/sfGoogleAnalyticsPlugin/trunk/lib/user (added)
- plugins/sfGoogleAnalyticsPlugin/trunk/lib/user/sfGoogleAnalyticsUserMixin.class.php (added)
- plugins/sfGoogleAnalyticsPlugin/trunk/lib/util/sfGoogleAnalyticsToolkit.class.php (modified) (3 diffs)
- plugins/sfGoogleAnalyticsPlugin/trunk/package.xml (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfGoogleAnalyticsPlugin/trunk/config/config.php
r7895 r7983 5 5 sfMixer::register('sfComponent', array('sfGoogleAnalyticsActionMixin', 'addGoogleAnalyticsCustomVar')); 6 6 sfMixer::register('sfComponent', array('sfGoogleAnalyticsActionMixin', 'addGoogleAnalyticsCustomVarToFlash')); 7 8 sfMixer::register('sfUser', array('sfGoogleAnalyticsUserMixin', 'addGoogleAnalyticsCustomVarToFlash')); plugins/sfGoogleAnalyticsPlugin/trunk/lib/action/sfGoogleAnalyticsActionMixin.class.php
r7895 r7983 18 18 * @param string $utParam 19 19 */ 20 public static function setGoogleAnalyticsParam( $action, $utParam)20 public static function setGoogleAnalyticsParam(sfComponent $action, $utParam) 21 21 { 22 22 sfGoogleAnalyticsToolkit::setParam($utParam); … … 31 31 * @param string $value 32 32 */ 33 public static function addGoogleAnalyticsVar( $action, $name, $value)33 public static function addGoogleAnalyticsVar(sfComponent $action, $name, $value) 34 34 { 35 35 sfGoogleAnalyticsToolkit::addVar($name, $value); … … 43 43 * @param string $var 44 44 */ 45 public static function addGoogleAnalyticsCustomVar( $action, $var)45 public static function addGoogleAnalyticsCustomVar(sfComponent $action, $var) 46 46 { 47 47 sfGoogleAnalyticsToolkit::addCustomVar($var); … … 54 54 * @param sfComponent $action 55 55 * @param string $var 56 * @param bool $persist 57 * @see comment block for sfGoogleAnalyticsUserMixin::addGoogleAnalyticsCustomVarToFlash() 58 * @todo support updated symfony 1.1 flash architecture 56 59 */ 57 public static function addGoogleAnalyticsCustomVarToFlash( $action, $var)60 public static function addGoogleAnalyticsCustomVarToFlash(sfComponent $action, $var, $persist = true) 58 61 { 59 62 $vars = $action->getFlash('google_analytics_custom_vars', array()); 60 63 $vars[] = $var; 61 $action->setFlash('google_analytics_custom_vars', $vars );64 $action->setFlash('google_analytics_custom_vars', $vars, $persist); 62 65 } 63 66 plugins/sfGoogleAnalyticsPlugin/trunk/lib/filter/sfGoogleAnalyticsFilter.class.php
r7895 r7983 114 114 115 115 $oldContent = $response->getContent(); 116 $newContent = preg_replace('/\<body[^\>]*\>/i', "$0 ".$trackingCode, $oldContent, 1);116 $newContent = preg_replace('/\<body[^\>]*\>/i', "$0\n".$trackingCode, $oldContent, 1); 117 117 118 118 if ($oldContent == $newContent) … … 135 135 136 136 $oldContent = $response->getContent(); 137 $newContent = str_ireplace('</body>', $trackingCode. '</body>', $oldContent);137 $newContent = str_ireplace('</body>', $trackingCode."\n</body>", $oldContent); 138 138 139 139 if ($oldContent == $newContent) plugins/sfGoogleAnalyticsPlugin/trunk/lib/util/sfGoogleAnalyticsToolkit.class.php
r7895 r7983 20 20 public static function getHtml() 21 21 { 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() ? 32 25 sfConfig::get('app_google_analytics_usrc_ssl', 'https://ssl.google-analytics.com/urchin.js') : 33 26 sfConfig::get('app_google_analytics_usrc', 'http://www.google-analytics.com/urchin.js'); 34 27 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'])) 57 32 { 58 33 // 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']) 61 36 { 62 37 throw new sfGoogleAnalyticsException('Please add your Google Analytics account number to your app.yml.'); 63 38 } 64 39 } 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) 76 42 { 77 43 if ($key{0} != '_') … … 79 45 $key = '_'.$key; 80 46 } 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)); 102 79 103 80 return $html; … … 174 151 175 152 /** 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 /** 176 209 * Get the Google Analytics configuration for the current action. 177 210 * plugins/sfGoogleAnalyticsPlugin/trunk/package.xml
r7189 r7983 11 11 <active>yes</active> 12 12 </lead> 13 <date>2008-0 1-25</date>13 <date>2008-03-19</date> 14 14 <version> 15 <release>1.0-RC 1</release>16 <api>1.0-RC 1</api>15 <release>1.0-RC2</release> 16 <api>1.0-RC2</api> 17 17 </version> 18 18 <stability> … … 40 40 <dir name="helper"> 41 41 <file role="data" name="GoogleAnalyticsHelper.php"/> 42 </dir> 43 <dir name="user"> 44 <file role="data" name="sfGoogleAnalyticsUserMixin.class.php"/> 42 45 </dir> 43 46 <dir name="util">