How To Calculate the Dwell Time of a GoToWebinar

CICI Solutions - How to Calculate the Dwell Time of a GoToWebinar

Table of Contents

When dealing with API integration, we come across, often, the need to adapt the obtained results to our program.

In this article, we bring you the example of how to calculate the actual time a user spent in a webinar made with GoToWebinar.

To do this we need to use data from a call to the GoToWebinar API:

The Array that comes from the API call

				
					$dates = array(
	0 => array(
		'st' => '09:50',
		'et' => '10:05'
	),
	1 => array(
		'st' => '10:40',
		'et' => '11:20'
	),
	2 => array(
		'st' => '10:07',
		'et' => '10:45'
	),
	3 => array(
		'st' => '10:00',
		'et' => '10:03'
	),
);

				
			

Classes and Functions

				
					function pre($s){
	echo '
';
	print_r($s);
	echo '
';
}

				
			
				
					function date_to_timestamp($date){
$d = DateTime::createFromFormat('H:i', $date);
if ($d === false) {
die("Incorrect date string");
} else {
return $d->getTimestamp();
}
}

function timestamp_to_date($time){
$hours = floor($time / 60 );

return $hours;
}

class Period
{
var $start = 0;
var $end = 0;
var $total = array();

function begin(){
$this->total[] = array(
'st' => $this->start,
'et' => $this->end
);
}

function extend(){
$i = count($this->total);
$this->total[$i-1]['et'] = $this->end;
}
}
				
			

Loop

				
					pre($dates);
$realstart = date_to_timestamp('09:00');

pre($realstart);
$realend = date_to_timestamp('10:00');

pre($realend);

pre('Convert to timestamp');
$i = 0;
foreach($dates as $date){
	pre($date);
	$dates[$i]['st'] = date_to_timestamp($date['st']);
    $dates[$i]['et'] = date_to_timestamp($date['et']);
	$i++;
}

pre($dates);

pre('Set Right Start and End');
$i = 0;
foreach($dates as $date){
	if($dates[$i]['st'] < $realstart) $dates[$i]['st'] = $realstart; if($dates[$i]['et'] > $realend) $dates[$i]['et'] = $realend;
	$i++;
}
$i = 0;
foreach($dates as $date){
	$dates[$i]['st'] = $dates[$i]['st'] - $realstart;
	$dates[$i]['et'] = $dates[$i]['et'] - $realstart;
	$i++;
}

/*usort($dates, function($a, $b) {
    return $a['st'] <=> $b['st'];
});
*/

usort($dates, function($a, $b) {
    $retval = $a['st'] <=> $b['st'];
    if ($retval == 0) {
        $retval = $a['et'] <=> $b['et'];
    }
    return $retval;
});


pre($dates);
pre('-------------------------------------------');
$total_webinar_time = $realend - $realstart;
pre('total_webinar_time :'.$total_webinar_time);
$p = new Period;
for($i=0;$i<count($dates);$i++){ pre('-------->i: '.$i);
	if(isset($dates[$i-1])){
		// from the second record
		if($dates[$i]['st'] > $dates[$i-1]['et']){
			// the beginning of this record is greater than the beginning of the previous one
			// create new period
			$p->start = $dates[$i]['st'];
			$p->end = $dates[$i]['et'];
			$p->begin();
			pre($p->total);
		} else {
			// the beginning of this record is NOT greater than the beginning of the previous one
			// increment period
			if(($dates[$i]['et']-$dates[$i]['st']) == $total_webinar_time) {
				die('100%');
			} else {
			$p->end = $dates[$i]['et'];
			$p->extend();
			pre($p->total);
			}
		}
	} else {
		// first record
		if(($dates[$i]['et']-$dates[$i]['st']) == $total_webinar_time) {
			die('100% cirulli');
		} else {
			$p->start = $dates[$i]['st'];
			$p->end = $dates[$i]['et'];
			$p->begin();
			pre($p->total);
		}
	}
}

pre('calculate time');
$total_sum = 0 ;
foreach($p->total as $p){
	pre($p);
	$sum = $p['et'] - $p['st'];
	if($sum > 0) $total_sum = $total_sum + $sum ;
}
				
			

Do you need some additional information or want to start a project with us?
Please send us an inquiry​

You may also be interested in: