@@ -201,7 +201,7 @@ private function setVisitorId( &$event ) {
201
201
}
202
202
203
203
if ( ! $ visitor_id ) {
204
- $ visitor_id = $ event ->getSiteSpecificGuid ( $ this ->site_id );
204
+ $ visitor_id = $ event ->getSiteSpecificGuid ( $ this ->getSiteId () );
205
205
$ this ->setGlobalEventProperty ( 'is_new_visitor ' , true );
206
206
$ this ->state ->set ( 'v ' , 'vid ' , $ visitor_id , 'cookie ' , true );
207
207
}
@@ -279,61 +279,58 @@ private function setDaysSinceLastSession( &$event ) {
279
279
private function setSessionId ( &$ event ) {
280
280
281
281
$ 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
+
283
286
sdk::debug ("is new session " );
287
+
284
288
//set prior_session_id
285
- $ prior_session_id = $ this ->state ->get ('s ' , 'sid ' );
289
+ $ prior_session_id = $ this ->getSessionId ();
290
+
286
291
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
+
288
295
$ prior_session_id = $ this ->state ->get ($ state_store_name , 's ' );
289
- }
290
- if ($ prior_session_id ) {
296
+
297
+ } else {
298
+
291
299
$ this ->setGlobalEventProperty ( 'prior_session_id ' , $ prior_session_id );
292
300
}
293
301
294
302
$ this ->resetSessionState ();
303
+
304
+ // generate new session id
305
+ $ session_id = $ event ->getSiteSpecificGuid ( $ this ->getSiteId () );
295
306
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
299
308
$ this ->setGlobalEventProperty ( 'is_new_session ' , true );
300
- $ this ->state ->set ( 's ' , 'sid ' , $ session_id , 'cookie ' , true );
301
-
309
+
302
310
} else {
311
+
312
+ sdk::debug ("is existing session " );
313
+
303
314
// 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 ();
310
316
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
- }
320
317
}
321
318
322
319
// set global event property
323
- $ this ->setGlobalEventProperty ( 'session_id ' , $ session_id );
320
+ $ this ->setGlobalEventProperty ( 'session_id ' , $ session_id );
324
321
// set sid state
325
322
$ this ->state ->set ( 's ' , 'sid ' , $ session_id , 'cookie ' , true );
326
323
}
324
+
325
+ public function getSessionId () {
326
+
327
+ return $ this ->state ->get ('s ' , 'sid ' );
328
+ }
327
329
328
330
private function setLastRequestTime ( &$ event ) {
329
331
330
332
$ last_req = $ this ->state ->get ('s ' , 'last_req ' );
331
333
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
- }
337
334
// set property on event object
338
335
$ this ->setGlobalEventProperty ( 'last_req ' , $ last_req );
339
336
sdk::debug ("setting last_req value of $ last_req as global event property. " );
@@ -367,42 +364,59 @@ private function isNewSession($timestamp = '', $last_req = 0) {
367
364
}
368
365
369
366
/**
370
- * Logs tracking event
367
+ * Tracks Event with Full state and globals
371
368
*
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
373
371
*
374
372
* @param object $event
375
373
* @return boolean
376
374
*/
377
- public function trackEvent ($ event ) {
375
+ public function trackEvent ( $ event ) {
378
376
379
377
// do not track anything if user is in overlay mode
380
378
if ($ this ->state ->get ('overlay ' )) {
381
379
return false ;
382
380
}
383
381
384
382
$ this ->setGlobalEventProperty ( 'HTTP_REFERER ' , $ this ->getServerParam ('HTTP_REFERER ' ) );
383
+
385
384
$ this ->setGlobalEventProperty ( 'HTTP_USER_AGENT ' , $ this ->getServerParam ('HTTP_USER_AGENT ' ) );
386
385
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
-
397
386
// set various state properties.
398
387
$ this ->manageState ( $ event );
399
388
400
389
$ event = $ this ->setAllGlobalEventProperties ( $ event );
401
390
402
- // send event to log API for processing.
391
+ // log event to server
403
392
return $ this ->logEvent ($ event ->getEventType (), $ event );
404
393
}
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
+ */
406
420
public function setAllGlobalEventProperties ( $ event ) {
407
421
408
422
if ( ! $ event ->get ('site_id ' ) ) {
@@ -491,11 +505,10 @@ public function addTransaction(
491
505
$ this ->commerce_event ->set ( 'country ' , $ country );
492
506
$ this ->commerce_event ->set ( 'state ' , $ state );
493
507
$ this ->commerce_event ->set ( 'city ' , $ city );
508
+
494
509
if ( $ session_id ) {
510
+
495
511
$ 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 );
499
512
}
500
513
}
501
514
@@ -531,7 +544,15 @@ public function addTransactionLineItem($order_id, $sku = '', $product_name = '',
531
544
public function trackTransaction () {
532
545
533
546
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
+ }
535
556
$ this ->commerce_event = '' ;
536
557
}
537
558
}
@@ -1170,7 +1191,7 @@ private function makeEvent() {
1170
1191
return new TrackingEvent ;
1171
1192
}
1172
1193
1173
- function setSiteId ($ site_id ) {
1194
+ function setSiteId ( $ site_id ) {
1174
1195
1175
1196
$ this ->site_id = $ site_id ;
1176
1197
}
@@ -1181,4 +1202,4 @@ function getSiteId() {
1181
1202
}
1182
1203
}
1183
1204
1184
- ?>
1205
+ ?>
0 commit comments