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:
<?php
namespace Apptus\ESales\Connector\Report;
use Apptus\ESales\Connector\Time\Duration;
use Apptus\ESales\Connector\Time\TimeInterval;
use Apptus\ESales\Connector\Time\TimePoint;
class SessionStatisticsReport extends Report {
public static function parse($data, \DateTimeZone $tz) {
$root = new \SimpleXMLElement($data);
self::checkElem($root, 'function');
self::checkElem($root->function, 'report');
$report = $root->function->report;
self::checkElem($report, 'resolution');
self::checkElem($report, 'time_interval');
self::checkElem($report, 'timeline');
$res = Duration::parse($report->resolution);
$interval = TimeInterval::parse($report->time_interval);
$timelineElem = $report->timeline;
$timeline = new Timeline();
$len = count($timelineElem->slot);
if ($len > 0) {
$slot = $timelineElem->slot[0];
for ($i = 1; $i < $len; $i++) {
$next = $timelineElem->slot[$i];
self::checkAttr($next, 'start');
$end = TimePoint::parse((string) $next->attributes()->start, $tz)->toMillis();
self::addToTimeline($timeline, $slot, $end, $tz);
$slot = $next;
}
self::checkAttr($slot, 'start');
$end = $interval->end($tz)->toMillis();
self::addToTimeline($timeline, $slot, $end, $tz);
}
return new SessionStatisticsReport($root, $timeline, $interval, $res, $tz);
}
private static function addToTimeline(Timeline $timeline, \SimpleXmlElement $slot, $end, $tz) {
self::checkElem($slot, 'purchases');
self::checkElem($slot, 'visits');
self::checkElem($slot, 'purchases_from_recommendations');
self::checkElem($slot, 'recommendation_displays');
self::checkElem($slot, 'searches');
self::checkElem($slot, 'recommendation_clicks');
self::checkElem($slot, 'abandoned_carts');
self::checkElem($slot, 'purchases_from_searches');
$slotName = '';
if ($slot->attributes()->name !== null) {
$slotName = (string) $slot->attributes()->name;
}
$start = TimePoint::parse((string) $slot->attributes()->start, $tz)->toMillis();
$stats = new SessionStatistics();
$stats->addToPurchases((string) $slot->purchases);
$stats->addToVisits((string) $slot->visits);
$stats->addToPurchasesFromRecommendations((string) $slot->purchases_from_recommendations);
$stats->addToRecommendationDisplays((string) $slot->recommendation_displays);
$stats->addToSearches((string) $slot->searches);
$stats->addToRecommendationClicks((string) $slot->recommendation_clicks);
$stats->addToAbandonedCarts((string) $slot->abandoned_carts);
$stats->addToPurchasesFromSearches((string) $slot->purchases_from_searches);
$timeline->append(new TimeSlot($start, $end, $slotName, $stats));
}
private $timeline;
private $resolution;
public function __construct(\SimpleXMLElement $asXml, Timeline $timeline, TimeInterval $interval, Duration $resolution, \DateTimeZone $tz) {
parent::__construct($interval, $asXml, $tz);
$this->timeline = $timeline;
$this->resolution = $resolution;
}
public function resolution() {
return $this->resolution;
}
public function timeline() {
return $this->timeline;
}
public function exportToExcel($file) {
$w = new ExcelWriter($file);
$w->cell('Session Statistics')->nextRow();
$w->cell('Resolution')->cell((string) $this->resolution)->nextRow();
$w->cell('Time interval');
$w->cell($this->isoDateTimeFormat($this->timeInterval()->start($this->tz)->toDateTime($this->tz)));
$w->cell($this->isoDateTimeFormat($this->timeInterval()->end($this->tz)->toDateTime($this->tz)))->nextRow();
$w->nextRow();
$w->cell('Time slot')
->cell('Visits')
->cell('Purchases')
->cell('Searches')
->cell('Purchases from Searches')
->cell('Recommendation displays')
->cell('Recommendation clicks')
->cell('Purchases from recommendations')
->cell('Abandoned carts')->nextRow();
foreach ($this->timeline as $s) {
$stats = $s->value();
$w->cell($s->slotStartTime())
->cell($stats->visits())
->cell($stats->purchases())
->cell($stats->searches())
->cell($stats->purchasesFromSearches())
->cell($stats->recommendationDisplays())
->cell($stats->recommendationClicks())
->cell($stats->purchasesFromRecommendations())
->cell($stats->abandonedCarts());
$w->nextRow();
}
$w->flush();
$w->close();
}
}