/** * Test if all dates and ranges get the same result * * @depends testRangePeriod * @depends testMultipleDates */ public function testDateEquals() { $date = date('Y-m-d', time() - 3600 * 24 * 7); // Range $this->_piwik->setPeriod(Piwik::PERIOD_RANGE); $this->_piwik->setRange($date, $date); $result1 = $this->_piwik->getVisits(); $this->_piwik->reset(); // Single date $this->_piwik->setPeriod(Piwik::PERIOD_DAY); $this->_piwik->setDate($date); $result2 = $this->_piwik->getVisits(); $this->_piwik->reset(); // Multiple dates $this->_piwik->setPeriod(Piwik::PERIOD_DAY); $this->_piwik->setRange($date, $date); $result3 = $this->_piwik->getVisits(); $result3 = $result3->{$date}; $this->_piwik->reset(); // Multiple dates with default range end $this->_piwik->setPeriod(Piwik::PERIOD_DAY); $this->_piwik->setRange($date); $result4 = $this->_piwik->getVisits(); $result4 = $result4->{$date}; // Compare results $this->assertEquals($result1, $result2); $this->assertEquals($result2, $result3); $this->assertEquals($result3, $result4); }
<?php require __DIR__ . '/vendor/autoload.php'; require 'config.php'; use VisualAppeal\Piwik; $piwik = new Piwik(SITE_URL, TOKEN, SITE_ID, Piwik::FORMAT_JSON); $piwik->setLanguage('en'); // $piwik->verifySsl = false; $test = $piwik->getApi(); if ($piwik->hasError()) { echo '<p>Invalid request</p>'; echo '<pre>'; var_dump($piwik->getErrors()); echo '</pre>'; } else { //Default time period: yesterday $visits = $piwik->getVisits(); $visitsU = $piwik->getUniqueVisitors(); $visitsL = $piwik->getSumVisitsLengthPretty(); //Change time period to current year $piwik->setPeriod(Piwik::PERIOD_YEAR); $piwik->setDate(date('Y-m-d')); $visitsYear = $piwik->getVisits(); $visitsUYear = $piwik->getUniqueVisitors(); // To enable see http://piwik.org/faq/how-to/faq_113/ $visitsLYear = $piwik->getSumVisitsLengthPretty(); //Change time period to range $piwik->setPeriod(Piwik::PERIOD_RANGE); $piwik->setRange(date('Y-m-d', mktime(0, 0, 0, 11, 24, 2014)), date('Y-m-d', mktime(0, 0, 0, 11, 31, 2014))); $visitsRange = $piwik->getVisits(); $visitsURange = $piwik->getUniqueVisitors(); // To enable see http://piwik.org/faq/how-to/faq_113/
/** * Get data from Piwik by ajax */ public function data() { if ($this->input->is_ajax_request()) { // get dates from form $from_str = $this->input->post('from'); $to_str = $this->input->post('to'); if (empty($from_str)) { $from_str = $this->dates['from']; } else { $date = DateTime::createFromFormat('m/d/Y', $from_str); $from_str = $date->format($this->date_format); } if (empty($to_str)) { $to_str = $this->dates['to']; } else { $date = DateTime::createFromFormat('m/d/Y', $to_str); $to_str = $date->format($this->date_format); } try { $this->piwik->setPeriod(\VisualAppeal\Piwik::PERIOD_RANGE); $this->piwik->setRange($from_str, $to_str); $this->piwik->setFormat(\VisualAppeal\Piwik::FORMAT_JSON); $this->piwik->setLanguage($this->language_small); $visits = $this->piwik->getVisits(); $unique_visitors = $this->piwik->getUniqueVisitors(); $bounce_count = $this->piwik->getBounceCount(); $bounce_count = $bounce_count ? $bounce_count : 0; $bounce_rate = !$visits ? 0 : $bounce_count / $visits * 100; $bounce_rate = round($bounce_rate, 2); $sum_visit_duration = $this->piwik->getSumVisitsLength(); if (!$sum_visit_duration || !$visits) { $average_visit_duration_pretty = '00:00:00'; } else { $average_visit_duration = $sum_visit_duration / $visits; $seconds = $average_visit_duration % 60; $minutes = $average_visit_duration / 60 % 60; $hours = ($average_visit_duration - $average_visit_duration % 3600) / 3600; if ($hours < 1) { $hours = 0; } $seconds_pretty = $seconds < 10 ? '0' . $seconds : $seconds; $minutes_pretty = $minutes < 10 ? '0' . $minutes : $minutes; $hours_pretty = $hours < 10 ? '0' . $hours : $hours; $average_visit_duration_pretty = $hours_pretty . ':' . $minutes_pretty . ':' . $seconds_pretty; } $data = ['visits' => $visits ? $visits : 0, 'unique_visits' => $unique_visitors ? $unique_visitors : 0, 'bounce_rate' => $bounce_rate . '%', 'average_visit_duration' => $average_visit_duration_pretty]; $this->piwik->reset(); $this->piwik->setPeriod(\VisualAppeal\Piwik::PERIOD_DAY); $this->piwik->setRange($from_str, $to_str); $this->piwik->setFormat(\VisualAppeal\Piwik::FORMAT_JSON); $this->piwik->setLanguage($this->language_small); $data['visits_chart'] = $this->piwik->getVisits(); $result['success'] = TRUE; $result = Arr::merge($result, $data); } catch (Exception $e) { $result['success'] = FALSE; $result['error'] = $e->getMessage(); } $result['dates'] = array('from' => date($this->date_format, strtotime($from_str)), 'to' => date($this->date_format, strtotime($to_str))); exit(json_encode($result)); } }