Skip to content

Commit

Permalink
adding stateless track method to handle dealyed ecommerce transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
padams committed Sep 20, 2021
1 parent 067b2bd commit 34a3e99
Showing 1 changed file with 74 additions and 53 deletions.
127 changes: 74 additions & 53 deletions src/Tracker/TrackerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private function setVisitorId( &$event ) {
}

if ( ! $visitor_id ) {
$visitor_id = $event->getSiteSpecificGuid( $this->site_id );
$visitor_id = $event->getSiteSpecificGuid( $this->getSiteId() );
$this->setGlobalEventProperty( 'is_new_visitor', true );
$this->state->set( 'v', 'vid', $visitor_id, 'cookie', true );
}
Expand Down Expand Up @@ -279,61 +279,58 @@ private function setDaysSinceLastSession( &$event ) {
private function setSessionId( &$event ) {

$is_new_session = $this->isNewSession( $event->get( 'timestamp' ), $this->getGlobalEventProperty( 'last_req' ) );
if ( $is_new_session ) {

// if its a new seession based on time or if there is no prior session ID found, make a new session
if ( $is_new_session || ! $this->getSessionId() ) {

sdk::debug("is new session");

//set prior_session_id
$prior_session_id = $this->state->get('s', 'sid');
$prior_session_id = $this->getSessionId();

if ( ! $prior_session_id ) {
$state_store_name = sprintf('%s_%s', $this->getSetting('site_session_param'), $this->site_id);

$state_store_name = sprintf('%s_%s', $this->getSetting('site_session_param'), $this->getSiteId() );

$prior_session_id = $this->state->get($state_store_name, 's');
}
if ($prior_session_id) {

} else {

$this->setGlobalEventProperty( 'prior_session_id', $prior_session_id );
}

$this->resetSessionState();

// generate new session id
$session_id = $event->getSiteSpecificGuid( $this->getSiteId() );

$session_id = $event->getSiteSpecificGuid( $this->site_id );

//mark new session flag on current request
//mark new session flag on current request
$this->setGlobalEventProperty( 'is_new_session', true );
$this->state->set( 's', 'sid', $session_id, 'cookie', true );


} else {

sdk::debug("is existing session");

// Must be an active session so just pull the session id from the state store
$session_id = $this->state->get('s', 'sid');

// support for old style cookie
if ( ! $session_id ) {
$state_store_name = sprintf('%s_%s', $this->getSetting('site_session_param'), $this->site_id);
$session_id = $this->state->get($state_store_name, 's');
$session_id = $this->getSessionId();

}

// fail-safe just in case there is no session_id
if ( ! $session_id ) {
$session_id = $event->getSiteSpecificGuid( $this->site_id );
//mark new session flag on current request
$this->setGlobalEventProperty( 'is_new_session', true );
sdk::debug('setting failsafe session id');
}
}

// set global event property
$this->setGlobalEventProperty( 'session_id', $session_id );
$this->setGlobalEventProperty( 'session_id', $session_id );
// set sid state
$this->state->set( 's', 'sid', $session_id, 'cookie', true );
}

public function getSessionId() {

return $this->state->get('s', 'sid');
}

private function setLastRequestTime( &$event ) {

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

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

/**
* Logs tracking event
* Tracks Event with Full state and globals
*
* This function fires a tracking event that will be processed and then dispatched
* This function processes a tracking event by adding full state and global properties to it
* Typically used by syncronous events such as Pageviews
*
* @param object $event
* @return boolean
*/
public function trackEvent($event) {
public function trackEvent( $event ) {

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

$this->setGlobalEventProperty( 'HTTP_REFERER', $this->getServerParam('HTTP_REFERER') );

$this->setGlobalEventProperty( 'HTTP_USER_AGENT', $this->getServerParam('HTTP_USER_AGENT') );

// needed by helper page tags function so it can append to first hit tag url
if (!$this->getSiteId()) {
$this->setSiteId($event->get('site_id'));
}

// is this needed?
if (!$this->getSiteId()) {
$this->setSiteId( $_GET['site_id'] );
}

// set various state properties.
$this->manageState( $event );

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

// send event to log API for processing.
// log event to server
return $this->logEvent($event->getEventType(), $event);
}


/**
* Tracks Event without state or globals
*
* This function processes a raw tracking event without any state or globals added to it
* Typically used by asyncronous events such as Delayed commerce transactions or programaticly derived events
*
* @param object $event
* @return boolean
*/
public function trackStatelessEvent( $event ) {

$event->set( 'site_id', $this->getSiteId() );

// log event to server.
return $this->logEvent( $event->getEventType(), $event );
}

/**
* Add Global properties to Tracking Event
*
* This function add global properties to the tracking event
*
* @param object $event
* @return $event
*/
public function setAllGlobalEventProperties( $event ) {

if ( ! $event->get('site_id') ) {
Expand Down Expand Up @@ -491,11 +505,10 @@ public function addTransaction(
$this->commerce_event->set( 'country', $country );
$this->commerce_event->set( 'state', $state );
$this->commerce_event->set( 'city', $city );

if ( $session_id ) {

$this->commerce_event->set( 'original_session_id', $session_id );
// tells the client to NOT manage state properties as we are
// going to look them up from the session later.
$this->commerce_event->set( 'is_state_set', true );
}
}

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

if ( ! empty( $this->commerce_event ) ) {
$this->trackEvent( $this->commerce_event );

if ( $this->commerce_event->get( 'original_session_id' ) ) {

$this->trackStatelessEvent( $this->commerce_event );

} else {

$this->trackEvent( $this->commerce_event );
}
$this->commerce_event = '';
}
}
Expand Down Expand Up @@ -1170,7 +1191,7 @@ private function makeEvent() {
return new TrackingEvent;
}

function setSiteId($site_id) {
function setSiteId( $site_id ) {

$this->site_id = $site_id;
}
Expand All @@ -1181,4 +1202,4 @@ function getSiteId() {
}
}

?>
?>

0 comments on commit 34a3e99

Please sign in to comment.