1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | <?php // // Open Web Analytics - An Open Source Web Analytics Framework // // Copyright 2006 Peter Adams. All rights reserved. // // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // $Id$ // if(!class_exists('owa_observer')) { require_once(OWA_DIR.'owa_observer.php'); } /** * Conversion Event handlers * * @author Peter Adams <peter@openwebanalytics.com> * @copyright Copyright © 2006 Peter Adams <peter@openwebanalytics.com> * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 * @category owa * @package owa * @version $Revision$ * @since owa 1.4.0 */ class owa_conversionHandlers extends owa_observer { /** * Notify Event Handler * * @param unknown_type $event * @access public */ function notify($event) { $update = false; $conversion_info = $this->checkForConversion($event); if ($conversion_info) { $s = owa_coreAPI::entityFactory('base.session'); $new_id = $s->generateId(trim( strtolower( $event->get('campaign') ) ) ); $s->getByPk('id', $event->get('session_id')); $id = $s->get('id'); // only record one goal of a particular type per session if ($id) { //record conversion if ( !empty( $conversion_info['conversion'] ) ) { $goal_column = 'goal_'.$conversion_info['conversion']; $already = $s->get( $goal_column ); // see if an existing value has been set goal value $goal_value_column = 'goal_'.$conversion_info['conversion'].'_value'; $existing_value = $s->get( $goal_value_column ); $value = $conversion_info['value']; // determin is we have a conversion event worth updating if ( $already != true ) { // there is a goal conversion $s->set( $goal_column , true ); $update = true; owa_coreAPI::debug( "$goal_column was achieved." ); } else { // goal already happened but check to see if we need to add a value to it. // happens in the case of ecommerce transaction where the value // can come in a secondary request. if no value then return. if ( ! $value ) { owa_coreAPI::debug( 'Not updating session. Goal was already achieved and in same session.' ); return OWA_EHS_EVENT_HANDLED; } } // Allow a value to be set if one has not be set already. // this is needed to support dynamic values passed by commerce transaction events if ( $value && ! $existing_value ) { $s->set( $goal_value_column, owa_lib::prepareCurrencyValue( $value ) ); $update = true; } } //record goal start if ( !empty($conversion_info['start'] ) ) { $goal_start_column = 'goal_'.$conversion_info['start'].'_start'; $already_started = $s->get( $goal_start_column ); if ( $already_started != true ) { $s->set( $goal_start_column, true ); $update = true; owa_coreAPI::debug( "$goal_start_column was started." ); } else { owa_coreAPI::debug( "$goal_start_column was already started." ); } } //update object if ( $update ) { // summarize goal conversions $s->set('num_goals', $this->countGoalConversions($s)); // summarize goal conversion value $s->set('goals_value', $this->sumGoalValues($s)); // summarize goal starts $s->set('num_goal_starts', $this->countGoalStarts($s)); $ret = $s->update(); if ( $ret ) { // create a new_conversion event so that the total conversion // metrics can be resummarized $this->dispatchNewConversionEvent($event); return OWA_EHS_EVENT_HANDLED; } else { return OWA_EHS_EVENT_FAILED; } } else { owa_coreAPI::debug( "nothing about this conversion is worth updating." ); return OWA_EHS_EVENT_HANDLED; } } else { owa_coreAPI::debug("Conversion processing aborted. No session could be found."); return OWA_EHS_EVENT_FAILED; } } else { owa_coreAPI::debug('No goal start or conversion detected.'); return OWA_EHS_EVENT_HANDLED; } } // create a new_conversion event so that the total conversion // metrics can be resummarized function dispatchNewConversionEvent($event) { $dispatch = owa_coreAPI::getEventDispatch(); $ce = $dispatch->makeEvent( 'new_conversion' ); $ce->set( 'session_id', $event->get( 'session_id' ) ); $dispatch->asyncNotify( $ce ); } function checkForConversion($event) { $goal_info = array('conversion' => '', 'value' => '', 'start' => ''); $siteId = $event->get('siteId'); if ( ! $siteId ) { $siteId = $event->get('site_id'); } $gm = owa_coreAPI::supportClassFactory('base', 'goalManager', $siteId); $goals = $gm->getActiveGoals(); owa_coreAPI::debug('active goals: '.print_r($goals, true)); if (empty($goals)) { return; } $is_match = false; foreach ($goals as $num => $goal) { if (!empty($goal)) { if (array_key_exists('goal_status', $goal) && $goal['goal_status'] === 'active') { switch ($goal['goal_type']) { case 'url_destination': $match = $this->checkUrlDestinationGoal($event, $goal); $start = $this->checkGoalStart($event, $goal); break; case 'pages_per_visit': $match = $this->checkPagesPerVisitGoal($event, $goal); break; case 'visit_duration': $match = $this->checkPagesPerVisitGoal($event, $goal); break; } if ($match) { $goal_info['conversion'] = $match; } if ($start) { $goal_info['start'] = $start; } //check for dynamic value from commerce transaction if ($event->get('ct_total')) { $goal_value = $event->get('ct_total'); } else { // else just use the static value if one is set. if ( array_key_exists('goal_value', $goal) ) { $goal_value = $goal['goal_value']; } } $goal_info['value'] = $goal_value; } else { owa_coreAPI::debug("Goal $num not active."); } } } owa_coreAPI::debug('conversion info: '.print_r($goal_info, true)); return $goal_info; } function checkPagesPerVisitGoal($event, $goal) { $num = $event->get('npvs'); if ($num) { $operator = $goal['details']['operator']; $req = $goal['details']['num_pageviews']; switch ($operator) { case '=': if ($num === $req) { |