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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | <?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$ // require_once( OWA_BASE_CLASSES_DIR . 'owa_caller.php' ); /** * OWA Client Class * * Abstract Client Class for use in php based applications * * @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_client extends owa_caller { var $commerce_event; var $pageview_event; var $global_event_properties = array(); var $stateInit; // set one traffic has been attributed. var $isTrafficAttributed; public function __construct($config = null) { $this->pageview_event = $this->makeEvent(); $this->pageview_event->setEventType('base.page_request'); return parent::__construct($config); } public function setPageTitle($value) { $this->pageview_event->set('page_title', $value); } public function setPageType($value) { $this->pageview_event->set('page_type', $value); } public function setProperty($name, $value) { $this->setGlobalEventProperty($name, $value); } private function setGlobalEventProperty($name, $value) { $this->global_event_properties[$name] = $value; } private function getGlobalEventProperty($name) { if ( array_key_exists($name, $this->global_event_properties) ) { return $this->global_event_properties[$name]; } } private function manageState( &$event ) { if ( ! $this->stateInit ) { $this->setVisitorId( $event ); $this->setFirstSessionTimestamp( $event ); $this->setLastRequestTime( $event ); $this->setSessionId( $event ); $this->setNumberPriorSessions( $event ); $this->setTrafficAttribution( $event ); // clear old style session cookie $session_store_name = sprintf('%s_%s', owa_coreAPI::getSetting('base', 'site_session_param'), $this->site_id); owa_coreAPI::clearState( $session_store_name ); $this->stateInit = true; } } private function setVisitorId( &$event ) { $visitor_id = owa_coreAPI::getStateParam( 'v', 'vid' ); if ( ! $visitor_id ) { $visitor_id = owa_coreAPI::getStateParam( 'v' ); owa_coreAPI::clearState( 'v' ); owa_coreAPI::setState( 'v', 'vid', $visitor_id, 'cookie', true ); } if ( ! $visitor_id ) { $visitor_id = $event->getSiteSpecificGuid( $this->site_id ); $this->setGlobalEventProperty( 'is_new_visitor', true ); owa_coreAPI::setState( 'v', 'vid', $visitor_id, 'cookie', true ); } // set property on event object $this->setGlobalEventProperty( 'visitor_id', $visitor_id ); } private function setNumberPriorSessions( &$event ) { // if check for nps value in vistor cookie. $nps = owa_coreAPI::getStateParam('v', 'nps'); // set value to 0 if not found. if (!$nps) { $nps = 0; } // if new session, increment visit count and persist to state store if ( $this->getGlobalEventProperty('is_new_session' ) ) { owa_coreAPI::setState('v', 'nps', $nps + 1, 'cookie', true); } // set property on the event object $this->setGlobalEventProperty('num_prior_sessions', $nps); } private function setFirstSessionTimestamp( &$event ) { $fsts = owa_coreAPI::getStateParam( 'v', 'fsts' ); if ( ! $fsts ) { $fsts = $event->get('timestamp'); owa_coreAPI::setState(owa_coreAPI::getSetting('base', 'visitor_param'), 'fsts', $fsts , 'cookie', true); } $this->setGlobalEventProperty( 'fsts', $fsts ); } private function setSessionId( &$event ) { $is_new_session = $this->isNewSession( $event->get( 'timestamp' ), $this->getGlobalEventProperty( 'last_req' ) ); if ( $is_new_session ) { //set prior_session_id $prior_session_id = owa_coreAPI::getStateParam('s', 'sid'); if ( ! $prior_session_id ) { $state_store_name = sprintf('%s_%s', owa_coreAPI::getSetting('base', 'site_session_param'), $this->site_id); $prior_session_id = owa_coreAPI::getStateParam($state_store_name, 's'); } if ($prior_session_id) { $this->setGlobalEventProperty( 'prior_session_id', $prior_session_id ); } $session_id = $event->getSiteSpecificGuid( $this->site_id ); // it's a new session. generate new session ID $this->setGlobalEventProperty( 'session_id', $session_id ); //mark new session flag on current request $this->setGlobalEventProperty( 'is_new_session', true ); owa_coreAPI::setState( 's', 'sid', $session_id, 'cookie', true ); } else { // Must be an active session so just pull the session id from the state store $session_id = owa_coreAPI::getStateParam('s', 'sid'); // support for old style cookie if ( ! $session_id ) { $state_store_name = sprintf('%s_%s', owa_coreAPI::getSetting('base', 'site_session_param'), $this->site_id); $session_id = owa_coreAPI::getStateParam($state_store_name, 's'); owa_coreAPI::setState( 's', 'sid', $session_id, 'cookie', true ); } $this->setGlobalEventProperty('session_id', $session_id); } // fail-safe just in case there is no session_id if ( ! $this->getGlobalEventProperty( 'session_id' ) ) { $session_id = $event->getSiteSpecificGuid( $this->site_id ); $this->setGlobalEventProperty( 'session_id', $session_id ); //mark new session flag on current request $this->setGlobalEventProperty( 'is_new_session', true ); owa_coreAPI::debug('hello from failsafe'); owa_coreAPI::setState( 's', 'sid', $session_id, 'cookie', true ); } } private function setLastRequestTime( &$event ) { $last_req = owa_coreAPI::getStateParam('s', 'last_req'); // suppport for old style cookie if ( ! $last_req ) { $state_store_name = sprintf( '%s_%s', owa_coreAPI::getSetting( 'base', 'site_session_param' ), $this->site_id ); $last_req = owa_coreAPI::getStateParam( $state_store_name, 'last_req' ); } // set property on event object $this->setGlobalEventProperty( 'last_req', $last_req ); // store new state value owa_coreAPI::setState( 's', 'last_req', $event->get( 'timestamp' ), 'cookie', true ); } /** * Check to see if request is a new or active session * * @return boolean */ private function isNewSession($timestamp = '', $last_req = 0) { $is_new_session = false; if ( ! $timestamp ) { $timestamp = time(); } $time_since_lastreq = $timestamp - $last_req; $len = owa_coreAPI::getSetting( 'base', 'session_length' ); if ( $time_since_lastreq < $len ) { owa_coreAPI::debug("This request is part of a active session."); return false; } else { //NEW SESSION. prev session expired, because no requests since some time. owa_coreAPI::debug("This request is the start of a new session. Prior session expired."); return true; } } /** * Logs tracking event from url params taken from request scope. * Takes event type from url. * * @return unknown */ function logEventFromUrl($manage_state = false) { // keeps php executing even if the client closes the connection ignore_user_abort(true); $service = &owa_coreAPI::serviceSingleton(); $service->request->decodeRequestParams(); $event = owa_coreAPI::supportClassFactory('base', 'event'); $event->setEventType(owa_coreAPI::getRequestParam('event_type')); $event->setProperties($service->request->getAllOwaParams()); // check for third party cookie mode. $mode = owa_coreAPI::getRequestParam('thirdParty'); if ( $mode ) { return $this->trackEvent($event); } else { return owa_coreAPI::logEvent($event->getEventType(), $event); } } /** * Logs tracking event * * This function fires a tracking event that will be processed and then dispatched * * @param object $event * @return boolean */ public function trackEvent($event) { // do not track anything if user is in overlay mode if (owa_coreAPI::getStateParam('overlay')) { return false; } // needed by helper page tags function so it can append to first hit tag url if (!$this->getSiteId()) { $this->setSiteId($event->get('site_id')); } if (!$this->getSiteId()) { $this->setSiteId(owa_coreAPI::getRequestParam('site_id')); } // set various state properties. $this->manageState( $event ); $event = $this->setAllGlobalEventProperties( $event ); // send event to log API for processing. return owa_coreAPI::logEvent($event->getEventType(), |