Skip to content

Commit 34a3e99

Browse files
author
padams
committed
adding stateless track method to handle dealyed ecommerce transactions.
1 parent 067b2bd commit 34a3e99

File tree

1 file changed

+74
-53
lines changed

1 file changed

+74
-53
lines changed

src/Tracker/TrackerClient.php

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private function setVisitorId( &$event ) {
201201
}
202202

203203
if ( ! $visitor_id ) {
204-
$visitor_id = $event->getSiteSpecificGuid( $this->site_id );
204+
$visitor_id = $event->getSiteSpecificGuid( $this->getSiteId() );
205205
$this->setGlobalEventProperty( 'is_new_visitor', true );
206206
$this->state->set( 'v', 'vid', $visitor_id, 'cookie', true );
207207
}
@@ -279,61 +279,58 @@ private function setDaysSinceLastSession( &$event ) {
279279
private function setSessionId( &$event ) {
280280

281281
$is_new_session = $this->isNewSession( $event->get( 'timestamp' ), $this->getGlobalEventProperty( 'last_req' ) );
282-
if ( $is_new_session ) {
282+
283+
// if its a new seession based on time or if there is no prior session ID found, make a new session
284+
if ( $is_new_session || ! $this->getSessionId() ) {
285+
283286
sdk::debug("is new session");
287+
284288
//set prior_session_id
285-
$prior_session_id = $this->state->get('s', 'sid');
289+
$prior_session_id = $this->getSessionId();
290+
286291
if ( ! $prior_session_id ) {
287-
$state_store_name = sprintf('%s_%s', $this->getSetting('site_session_param'), $this->site_id);
292+
293+
$state_store_name = sprintf('%s_%s', $this->getSetting('site_session_param'), $this->getSiteId() );
294+
288295
$prior_session_id = $this->state->get($state_store_name, 's');
289-
}
290-
if ($prior_session_id) {
296+
297+
} else {
298+
291299
$this->setGlobalEventProperty( 'prior_session_id', $prior_session_id );
292300
}
293301

294302
$this->resetSessionState();
303+
304+
// generate new session id
305+
$session_id = $event->getSiteSpecificGuid( $this->getSiteId() );
295306

296-
$session_id = $event->getSiteSpecificGuid( $this->site_id );
297-
298-
//mark new session flag on current request
307+
//mark new session flag on current request
299308
$this->setGlobalEventProperty( 'is_new_session', true );
300-
$this->state->set( 's', 'sid', $session_id, 'cookie', true );
301-
309+
302310
} else {
311+
312+
sdk::debug("is existing session");
313+
303314
// Must be an active session so just pull the session id from the state store
304-
$session_id = $this->state->get('s', 'sid');
305-
306-
// support for old style cookie
307-
if ( ! $session_id ) {
308-
$state_store_name = sprintf('%s_%s', $this->getSetting('site_session_param'), $this->site_id);
309-
$session_id = $this->state->get($state_store_name, 's');
315+
$session_id = $this->getSessionId();
310316

311-
}
312-
313-
// fail-safe just in case there is no session_id
314-
if ( ! $session_id ) {
315-
$session_id = $event->getSiteSpecificGuid( $this->site_id );
316-
//mark new session flag on current request
317-
$this->setGlobalEventProperty( 'is_new_session', true );
318-
sdk::debug('setting failsafe session id');
319-
}
320317
}
321318

322319
// set global event property
323-
$this->setGlobalEventProperty( 'session_id', $session_id );
320+
$this->setGlobalEventProperty( 'session_id', $session_id );
324321
// set sid state
325322
$this->state->set( 's', 'sid', $session_id, 'cookie', true );
326323
}
324+
325+
public function getSessionId() {
326+
327+
return $this->state->get('s', 'sid');
328+
}
327329

328330
private function setLastRequestTime( &$event ) {
329331

330332
$last_req = $this->state->get('s', 'last_req');
331333

332-
// suppport for old style cookie
333-
if ( ! $last_req ) {
334-
$state_store_name = sprintf( '%s_%s', $this->getSetting( 'site_session_param' ), $this->site_id );
335-
$last_req = $this->state->get( $state_store_name, 'last_req' );
336-
}
337334
// set property on event object
338335
$this->setGlobalEventProperty( 'last_req', $last_req );
339336
sdk::debug("setting last_req value of $last_req as global event property.");
@@ -367,42 +364,59 @@ private function isNewSession($timestamp = '', $last_req = 0) {
367364
}
368365

369366
/**
370-
* Logs tracking event
367+
* Tracks Event with Full state and globals
371368
*
372-
* This function fires a tracking event that will be processed and then dispatched
369+
* This function processes a tracking event by adding full state and global properties to it
370+
* Typically used by syncronous events such as Pageviews
373371
*
374372
* @param object $event
375373
* @return boolean
376374
*/
377-
public function trackEvent($event) {
375+
public function trackEvent( $event ) {
378376

379377
// do not track anything if user is in overlay mode
380378
if ($this->state->get('overlay')) {
381379
return false;
382380
}
383381

384382
$this->setGlobalEventProperty( 'HTTP_REFERER', $this->getServerParam('HTTP_REFERER') );
383+
385384
$this->setGlobalEventProperty( 'HTTP_USER_AGENT', $this->getServerParam('HTTP_USER_AGENT') );
386385

387-
// needed by helper page tags function so it can append to first hit tag url
388-
if (!$this->getSiteId()) {
389-
$this->setSiteId($event->get('site_id'));
390-
}
391-
392-
// is this needed?
393-
if (!$this->getSiteId()) {
394-
$this->setSiteId( $_GET['site_id'] );
395-
}
396-
397386
// set various state properties.
398387
$this->manageState( $event );
399388

400389
$event = $this->setAllGlobalEventProperties( $event );
401390

402-
// send event to log API for processing.
391+
// log event to server
403392
return $this->logEvent($event->getEventType(), $event);
404393
}
405-
394+
395+
/**
396+
* Tracks Event without state or globals
397+
*
398+
* This function processes a raw tracking event without any state or globals added to it
399+
* Typically used by asyncronous events such as Delayed commerce transactions or programaticly derived events
400+
*
401+
* @param object $event
402+
* @return boolean
403+
*/
404+
public function trackStatelessEvent( $event ) {
405+
406+
$event->set( 'site_id', $this->getSiteId() );
407+
408+
// log event to server.
409+
return $this->logEvent( $event->getEventType(), $event );
410+
}
411+
412+
/**
413+
* Add Global properties to Tracking Event
414+
*
415+
* This function add global properties to the tracking event
416+
*
417+
* @param object $event
418+
* @return $event
419+
*/
406420
public function setAllGlobalEventProperties( $event ) {
407421

408422
if ( ! $event->get('site_id') ) {
@@ -491,11 +505,10 @@ public function addTransaction(
491505
$this->commerce_event->set( 'country', $country );
492506
$this->commerce_event->set( 'state', $state );
493507
$this->commerce_event->set( 'city', $city );
508+
494509
if ( $session_id ) {
510+
495511
$this->commerce_event->set( 'original_session_id', $session_id );
496-
// tells the client to NOT manage state properties as we are
497-
// going to look them up from the session later.
498-
$this->commerce_event->set( 'is_state_set', true );
499512
}
500513
}
501514

@@ -531,7 +544,15 @@ public function addTransactionLineItem($order_id, $sku = '', $product_name = '',
531544
public function trackTransaction() {
532545

533546
if ( ! empty( $this->commerce_event ) ) {
534-
$this->trackEvent( $this->commerce_event );
547+
548+
if ( $this->commerce_event->get( 'original_session_id' ) ) {
549+
550+
$this->trackStatelessEvent( $this->commerce_event );
551+
552+
} else {
553+
554+
$this->trackEvent( $this->commerce_event );
555+
}
535556
$this->commerce_event = '';
536557
}
537558
}
@@ -1170,7 +1191,7 @@ private function makeEvent() {
11701191
return new TrackingEvent;
11711192
}
11721193

1173-
function setSiteId($site_id) {
1194+
function setSiteId( $site_id ) {
11741195

11751196
$this->site_id = $site_id;
11761197
}
@@ -1181,4 +1202,4 @@ function getSiteId() {
11811202
}
11821203
}
11831204

1184-
?>
1205+
?>

0 commit comments

Comments
 (0)