/**
  * Constructor
  *
  * @param AnalyticsOverview $overview
  *
  * @return array
  */
 protected function getTimestamps(AnalyticsOverview &$overview)
 {
     // if yearoverview set the begin time to the first day of this year
     $profileStartDate = explode('T', $this->configHelper->getActiveProfile()['created'])[0];
     if ($overview->getUseYear()) {
         $begin_date = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y')));
         $begin = strtotime($profileStartDate) > strtotime($begin_date) ? date('Y-m-d', strtotime($profileStartDate)) : $begin_date;
     } else {
         // check if timespan is't more than existence of the profile; if so, use the creation time in stead of the timespan time
         $begin = strtotime($profileStartDate) > strtotime('-' . $overview->getTimespan() . ' days') ? date('Y-m-d', strtotime($profileStartDate)) : date('Y-m-d', strtotime('-' . $overview->getTimespan() . ' days'));
     }
     // set the end time
     $end = date('Y-m-d', strtotime('-' . $overview->getStartOffset() . ' days'));
     return array('begin' => $begin, 'end' => $end);
 }
 /**
  * get extra data
  *
  * @return array
  */
 protected function getExtra(AnalyticsOverview $overview)
 {
     $timespan = $overview->getTimespan() - $overview->getStartOffset();
     $extra = parent::getExtra($overview);
     if ($timespan <= 1) {
         $extra['dimensions'] = 'ga:date,ga:hour';
     } else {
         if ($timespan <= 7) {
             $extra['dimensions'] = 'ga:date,ga:hour';
         } else {
             if ($timespan <= 31) {
                 $extra['dimensions'] = 'ga:week,ga:day,ga:date';
             } else {
                 $extra['dimensions'] = 'ga:isoYearIsoWeek';
             }
         }
     }
     return $extra;
 }
 /**
  * Fetch a specific goals
  *
  * @param AnalyticsOverview $overview The overview
  * @param $goalCollection
  */
 private function parseGoals(&$overview, $goalCollection)
 {
     $timespan = $overview->getTimespan() - $overview->getStartOffset();
     if ($overview->getGoals()) {
         // delete existing entries
         foreach ($overview->getGoals() as $goal) {
             $this->em->remove($goal);
         }
         $this->em->flush();
     }
     foreach ($goalCollection as $goalEntry) {
         // create a new goal
         $goal = new AnalyticsGoal();
         $goal->setOverview($overview);
         $goal->setName($goalEntry['name']);
         $goal->setPosition($goalEntry['position']);
         $chartData = array();
         $totalVisits = 0;
         $goalVisits = 0;
         $i = 0;
         // Fill the chartdata array
         foreach ($goalEntry['visits'] as $timestamp => $visits) {
             $totalVisits += $visits;
             if ($timespan <= 7 && $timespan > 1) {
                 $goalVisits += $visits;
                 if ($i % 5 == 0) {
                     $chartData[] = array('timestamp' => $timestamp, 'conversions' => $goalVisits);
                     $goalVisits = 0;
                 }
             } else {
                 $chartData[] = array('timestamp' => $timestamp, 'conversions' => $visits);
             }
             $i += 1;
         }
         // set the data
         $goal->setVisits($totalVisits);
         $goal->setChartData(json_encode($chartData));
         $goals = $overview->getGoals();
         $goals[] = $goal;
         $this->em->persist($goal);
         $this->output->writeln("\t\t" . 'Fetched goal ' . $goal->getPosition() . ': "' . $goal->getName() . '" @ ' . $totalVisits);
     }
 }