Overview

Namespaces

  • Apptus
    • ESales
      • Connector
        • Report
        • Time
    • Util
      • Cache
  • PHP
  • Overview
  • Namespace
  • Class
  • Tree
  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;

/**
 * Report for Session statistics on the site.
 *
 * This report can be seen in the graphs in the Site Summary tab and in the Site Details tab on the Reports page in eSales Manager
 * and it is described in the <i>Session statistics</i> section on <a href="http://zone.apptus.com">Apptus Zone</a> (http://zone.apptus.com).
 */
class SessionStatisticsReport extends Report {

    /**
     * Creates an SessionStatisticsReport from an XML document.
     *
     * @param string
     *            A string with the XML document to parse.
     * @param \DateTimeZone
     *            The time zone for this report.
     * @return AdPlacementReport
     *            An AdPlacementReport.
     * @throws FormatException if the XML document does not match the expected format.
     */
    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;

    /**
     * Creates a SessionStatisticsReport. Note that null is illegal for all input parameters.
     *
     * @param \SimpleXMLElement
     *            An XmlTree to be used if the {@see exportToXml()} method is called.
     * @param Timeline
     *            A timeline of {@see SessionStatistics} elements. Each containing common statistics data such as number of displays, clicks, payments, etc.
     * @param TimeInterval
     *            The time range for the data in this report.
     * @param Duration
     *            The resolution for the data in this report.
     * @param \DateTimeZone
     *            The time zone for the data in this report.
     */
    public function __construct(\SimpleXMLElement $asXml, Timeline $timeline, TimeInterval $interval, Duration $resolution, \DateTimeZone $tz) {
        parent::__construct($interval, $asXml, $tz);

        $this->timeline = $timeline;
        $this->resolution = $resolution;
    }

    /**
     * @return Duration
     *            The resolution used for the data in this report.
     */
    public function resolution() {
        return $this->resolution;
    }

    /**
     * @return Timeline
     *            A timeline of session statistics.
     */
    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();
    }
}
Apptus ESales Connector PHP API documentation generated by ApiGen