Esempio n. 1
0
 /**
  * Get bar chart data of hits
  *
  * @param char     $unit   {@link php.net/manual/en/function.date.php#refsect1-function.date-parameters}
  * @param DateTime $dateFrom
  * @param DateTime $dateTo
  * @param string   $dateFormat
  * @param array    $filter
  *
  * @return array
  */
 public function getDeviceGranularityData($unit, \DateTime $dateFrom, \DateTime $dateTo, $dateFormat = null, $filter = array())
 {
     $data['values'] = array();
     $data['labels'] = array();
     $q = $this->em->getConnection()->createQueryBuilder();
     $q->select('count(l.id) as leads, ds.device')->from(MAUTIC_TABLE_PREFIX . 'lead_lists_leads', 't')->join('t', MAUTIC_TABLE_PREFIX . 'leads', 'l', 'l.id = t.lead_id')->join('t', MAUTIC_TABLE_PREFIX . 'page_hits', 'h', 'h.lead_id=l.id')->join('h', MAUTIC_TABLE_PREFIX . 'lead_devices', 'ds', 'ds.id = h.device_id')->orderBy('ds.device', 'DESC')->andWhere($q->expr()->gte('t.date_added', ':date_from'))->setParameter('date_from', $dateFrom->format('Y-m-d'))->andWhere($q->expr()->lte('t.date_added', ':date_to'))->setParameter('date_to', $dateTo->format('Y-m-d' . " 23:59:59"));
     if (isset($filter['leadlist_id']['value'])) {
         $q->andWhere($q->expr()->eq('t.leadlist_id', ':leadlistid'))->setParameter('leadlistid', $filter['leadlist_id']['value']);
     }
     $q->groupBy('ds.device');
     if (!empty($options['canViewOthers'])) {
         $q->andWhere('l.created_by = :userId')->setParameter('userId', $this->user->getId());
     }
     $results = $q->execute()->fetchAll();
     foreach ($results as $result) {
         $data['labels'][] = substr(empty($result['device']) ? $this->translator->trans('mautic.core.no.info') : $result['device'], 0, 12);
         $data['values'][] = $result['leads'];
     }
     $data['xAxes'][] = array('display' => true);
     $data['yAxes'][] = array('display' => true);
     $baseData = array('label' => $this->translator->trans('mautic.core.device'), 'data' => $data['values']);
     $chart = new BarChart($data['labels']);
     $datasets[] = array_merge($baseData, $chart->generateColors(2));
     $chartData = array('labels' => $data['labels'], 'datasets' => $datasets, 'options' => array('xAxes' => $data['xAxes'], 'yAxes' => $data['yAxes']));
     return $chartData;
 }
Esempio n. 2
0
 /**
  * Get a stats for email by list
  *
  * @param Email|int $email
  * @param bool      $includeVariants
  *
  * @return array
  */
 public function getEmailDeviceStats($email, $includeVariants = false, $dateFrom = null, $dateTo = null)
 {
     if (!$email instanceof Email) {
         $email = $this->getEntity($email);
     }
     $emailIds = $includeVariants ? $email->getRelatedEntityIds() : [$email->getId()];
     $templateEmail = 'template' === $email->getEmailType();
     $results = $this->getStatDeviceRepository()->getDeviceStats($emailIds, $dateFrom, $dateTo);
     // Organize by list_id (if a segment email) and/or device
     $stats = [];
     $devices = [];
     foreach ($results as $result) {
         if (empty($result['device'])) {
             $result['device'] = $this->translator->trans('mautic.core.unknown');
         } else {
             $result['device'] = mb_substr($result['device'], 0, 12);
         }
         $devices[$result['device']] = $result['device'];
         if ($templateEmail) {
             // List doesn't matter
             $stats[$result['device']] = $result['count'];
         } elseif (null !== $result['list_id']) {
             if (!isset($stats[$result['list_id']])) {
                 $stats[$result['list_id']] = [];
             }
             if (!isset($stats[$result['list_id']][$result['device']])) {
                 $stats[$result['list_id']][$result['device']] = (int) $result['count'];
             } else {
                 $stats[$result['list_id']][$result['device']] += (int) $result['count'];
             }
         }
     }
     $listCount = 0;
     if (!$templateEmail) {
         $lists = $email->getLists();
         $listNames = [];
         foreach ($lists as $l) {
             $listNames[$l->getId()] = $l->getName();
         }
         $listCount = count($listNames);
     }
     natcasesort($devices);
     $chart = new BarChart(array_values($devices));
     if ($templateEmail) {
         // Populate the data
         $chart->setDataset(null, array_values($stats), 0);
     } else {
         $combined = [];
         $key = $listCount > 1 ? 1 : 0;
         foreach ($listNames as $id => $name) {
             // Fill in missing devices
             $listStats = [];
             foreach ($devices as $device) {
                 $listStat = !isset($stats[$id][$device]) ? 0 : $stats[$id][$device];
                 $listStats[] = $listStat;
                 if (!isset($combined[$device])) {
                     $combined[$device] = 0;
                 }
                 $combined[$device] += $listStat;
             }
             // Populate the data
             $chart->setDataset($name, $listStats, $key);
             $key++;
         }
         if ($listCount > 1) {
             $chart->setDataset($this->translator->trans('mautic.email.lists.combined'), array_values($combined), 0);
         }
     }
     return $chart->render();
 }
Esempio n. 3
0
 /**
  * Get bar chart data of hits
  *
  * @param char     $unit   {@link php.net/manual/en/function.date.php#refsect1-function.date-parameters}
  * @param DateTime $dateFrom
  * @param DateTime $dateTo
  * @param string   $dateFormat
  * @param array    $filter
  *
  * @return array
  */
 public function getHitsBarChartData($unit, \DateTime $dateFrom, \DateTime $dateTo, $dateFormat = null, $filter = array())
 {
     $chart = new BarChart($unit, $dateFrom, $dateTo, $dateFormat);
     $query = $chart->getChartQuery($this->factory->getEntityManager()->getConnection());
     $chartData = $query->fetchTimeData('page_hits', 'date_hit', $filter);
     $chart->setDataset($this->factory->getTranslator()->trans('mautic.page.field.hits'), $chartData);
     return $chart->render();
 }