/** * Get a count of unique hits for the current tracking ID * * @param Page|Redirect $page * @param string $trackingId * * @return int * @throws \Doctrine\ORM\NoResultException * @throws \Doctrine\ORM\NonUniqueResultException */ public function getHitCountForTrackingId($page, $trackingId) { $q = $this->createQueryBuilder('h')->select('count(h.id) as num'); if ($page instanceof Page) { $q->where('IDENTITY(h.page) = ' . $page->getId()); } elseif ($page instanceof Redirect) { $q->where('IDENTITY(h.redirect) = ' . $page->getId()); } $q->andWhere('h.trackingId = :id')->setParameter('id', $trackingId); $count = $q->getQuery()->getSingleResult(); return (int) $count['num']; }
/** * Determine if the page hit is a unique * * @param Page|Redirect $page * @param string $trackingId * * @return bool */ public function isUniquePageHit($page, $trackingId) { $q = $this->getEntityManager()->getConnection()->createQueryBuilder(); $q2 = $this->getEntityManager()->getConnection()->createQueryBuilder(); $q2->select('null')->from(MAUTIC_TABLE_PREFIX . 'page_hits', 'h'); $expr = $q2->expr()->andX($q2->expr()->eq('h.tracking_id', ':id')); if ($page instanceof Page) { $expr->add($q2->expr()->eq('h.page_id', $page->getId())); } elseif ($page instanceof Redirect) { $expr->add($q2->expr()->eq('h.redirect_id', $page->getId())); } $q2->where($expr); $q->select('u.is_unique')->from(sprintf('(SELECT (NOT EXISTS (%s)) is_unique)', $q2->getSQL()), 'u')->setParameter('id', $trackingId); return (bool) $q->execute()->fetchColumn(); }
/** * Determines the winner of A/B test based on dwell time rates * * @param MauticFactory $factory * @param Page $parent * @param $children * * @return array */ public static function determineDwellTimeTestWinner($factory, $parent, $children) { //find the hits that did not go any further $repo = $factory->getEntityManager()->getRepository('MauticPageBundle:Hit'); $pageIds = array($parent->getId()); foreach ($children as $c) { $pageIds[] = $c->getId(); } $startDate = $parent->getVariantStartDate(); if ($startDate != null && !empty($pageIds)) { //get their bounce rates $counts = $repo->getDwellTimes(array('pageIds' => $pageIds, 'startDate' => $startDate)); $translator = $factory->getTranslator(); $support = array(); if ($counts) { //in order to get a fair grade, we have to compare the averages here since a page that is only shown //25% of the time will have a significantly lower sum than a page shown 75% of the time $avgs = array(); $support['data'] = array(); $support['labels'] = array(); foreach ($counts as $pid => $stats) { $avgs[$pid] = $stats['average']; $support['data'][$translator->trans('mautic.page.abtest.label.dewlltime.average')][] = $stats['average']; $support['labels'][] = $pid . ':' . $stats['title']; } //set max for scales $max = max($avgs); $support['step_width'] = ceil($max / 10) * 10; //get the page ids with the greatest average dwell time $winners = $max > 0 ? array_keys($avgs, $max) : array(); return array('winners' => $winners, 'support' => $support, 'basedOn' => 'page.dwelltime', 'supportTemplate' => 'MauticPageBundle:SubscribedEvents\\AbTest:bargraph.html.php'); } } return array('winners' => array(), 'support' => array(), 'basedOn' => 'page.dwelltime'); }
/** * @param string $alias * @param Page $entity * * @return mixed */ public function checkUniqueAlias($alias, $entity = null) { $q = $this->createQueryBuilder('e')->select('count(e.id) as alias_count')->where('e.alias = :alias'); $q->setParameter('alias', $alias); if (!empty($entity)) { $parent = $entity->getTranslationParent(); $children = $entity->getTranslationChildren(); if ($parent || count($children)) { //allow same alias among language group $ids = array(); if (!empty($parent)) { $children = $parent->getTranslationChildren(); $ids[] = $parent->getId(); } foreach ($children as $child) { if ($child->getId() != $entity->getId()) { $ids[] = $child->getId(); } } $q->andWhere($q->expr()->notIn('e.id', $ids)); } $parent = $entity->getVariantParent(); $children = $entity->getVariantChildren(); if ($parent || count($children)) { //allow same alias among language group $ids = array(); if (!empty($parent)) { $children = $parent->getVariantChildren(); $ids[] = $parent->getId(); } foreach ($children as $child) { if ($child->getId() != $entity->getId()) { $ids[] = $child->getId(); } } $q->andWhere($q->expr()->notIn('e.id', $ids)); } if ($entity->getId()) { $q->andWhere('e.id != :id'); $q->setParameter('id', $entity->getId()); } } $results = $q->getQuery()->getSingleResult(); return $results['alias_count']; }
/** * Determines the winner of A/B test based on bounce rates * * @param $factory * @param Page $parent * @param Page[] $children * * @return array */ public static function determineBounceTestWinner($factory, $parent, $children) { //find the hits that did not go any further $repo = $factory->getEntityManager()->getRepository('MauticPageBundle:Hit'); $pageIds = $parent->getRelatedEntityIds(); $startDate = $parent->getVariantStartDate(); if ($startDate != null && !empty($pageIds)) { //get their bounce rates $counts = $repo->getBounces($pageIds, $startDate, true); if ($counts) { // Group by translation $combined = [$parent->getId() => $counts[$parent->getId()]]; if ($parent->hasTranslations()) { $translations = $parent->getTranslationChildren()->getKeys(); foreach ($translations as $translation) { $combined[$parent->getId()]['bounces'] += $counts[$translation]['bounces']; $combined[$parent->getId()]['totalHits'] += $counts[$translation]['totalHits']; $combined[$parent->getId()]['rate'] = $counts[$parent->getId()]['totalHits'] ? round($counts[$parent->getId()]['bounces'] / $counts[$parent->getId()]['totalHits'] * 100, 2) : 0; } } foreach ($children as $child) { if ($child->hasTranslations()) { $combined[$child->getId()] = $counts[$child->getId()]; $translations = $child->getTranslationChildren()->getKeys(); foreach ($translations as $translation) { $combined[$child->getId()]['bounces'] += $counts[$translation]['bounces']; $combined[$child->getId()]['totalHits'] += $counts[$translation]['totalHits']; $combined[$child->getId()]['rate'] = $counts[$child->getId()]['totalHits'] ? round($counts[$child->getId()]['bounces'] / $counts[$child->getId()]['totalHits'] * 100, 2) : 0; } } } unset($counts); //let's arrange by rate $rates = []; $support['data'] = []; $support['labels'] = []; $bounceLabel = $factory->getTranslator()->trans('mautic.page.abtest.label.bounces'); foreach ($combined as $pid => $stats) { $rates[$pid] = $stats['rate']; $support['data'][$bounceLabel][] = $rates[$pid]; $support['labels'][] = $pid . ':' . $stats['title']; } $max = max($rates); $support['step_width'] = ceil($max / 10) * 10; //get the page ids with the greatest average dwell time $winners = $max > 0 ? array_keys($rates, $max) : []; return ['winners' => $winners, 'support' => $support, 'basedOn' => 'page.bouncerate', 'supportTemplate' => 'MauticPageBundle:SubscribedEvents\\AbTest:bargraph.html.php']; } } return ['winners' => [], 'support' => [], 'basedOn' => 'page.bouncerate']; }
/** * Get number of page bounces * * @param Page $page * * @return int */ public function getDwellTimeStats(Page $page) { return $this->getHitRepository()->getDwellTimes(array('pageIds' => $page->getId())); }
/** * Determines the winner of A/B test based on number of asset downloads * * @param MauticFactory $factory * @param Page $parent * @param $children * * @return array */ public static function determineDownloadWinner($factory, $parent, $children) { $repo = $factory->getEntityManager()->getRepository('MauticAssetBundle:Download'); //if this is an email A/B test, then link email to page to form submission //if it is a page A/B test, then link form submission to page $type = $parent instanceof Email ? 'email' : 'page'; $ids = array($parent->getId()); foreach ($children as $c) { if ($c->isPublished()) { $id = $c->getId(); $ids[] = $id; } } $startDate = $parent->getVariantStartDate(); if ($startDate != null && !empty($ids)) { $counts = $type == 'page' ? $repo->getDownloadCountsByPage($ids, $startDate) : $repo->getDownloadCountsByEmail($ids, $startDate); $translator = $factory->getTranslator(); if ($counts) { $downloads = $support = $data = array(); $hasResults = array(); $downloadsLabel = $translator->trans('mautic.asset.abtest.label.downloads'); $hitsLabel = $type == 'page' ? $translator->trans('mautic.asset.abtest.label.hits') : $translator->trans('mautic.asset.abtest.label.sentemils'); foreach ($counts as $stats) { $rate = $stats['total'] ? round($stats['count'] / $stats['total'] * 100, 2) : 0; $downloads[$stats['id']] = $rate; $data[$downloadsLabel][] = $stats['count']; $data[$hitsLabel][] = $stats['total']; $support['labels'][] = $stats['id'] . ':' . $stats['name'] . ' (' . $rate . '%)'; $hasResults[] = $stats['id']; } //make sure that parent and published children are included if (!in_array($parent->getId(), $hasResults)) { $data[$downloadsLabel][] = 0; $data[$hitsLabel][] = 0; $support['labels'][] = $parent->getId() . ':' . ($type == 'page' ? $parent->getTitle() : $parent->getName()) . ' (0%)'; } foreach ($children as $c) { if ($c->isPublished()) { if (!in_array($c->getId(), $hasResults)) { $data[$downloadsLabel][] = 0; $data[$hitsLabel][] = 0; $support['labels'][] = $c->getId() . ':' . ($type == 'page' ? $c->getTitle() : $c->getName()) . ' (0%)'; } } } $support['data'] = $data; //set max for scales $maxes = array(); foreach ($support['data'] as $label => $data) { $maxes[] = max($data); } $top = max($maxes); $support['step_width'] = ceil($top / 10) * 10; //put in order from least to greatest just because asort($downloads); //who's the winner? $max = max($downloads); //get the page ids with the most number of downloads $winners = $max > 0 ? array_keys($downloads, $max) : array(); return array('winners' => $winners, 'support' => $support, 'basedOn' => 'asset.downloads', 'supportTemplate' => 'MauticPageBundle:SubscribedEvents\\AbTest:bargraph.html.php'); } } return array('winners' => array(), 'support' => array(), 'basedOn' => 'asset.downloads'); }
/** * Get number of page bounces. * * @param Page $page * @param \DateTime $fromDate * * @return int */ public function getBounces(Page $page, \DateTime $fromDate = null) { return $this->getHitRepository()->getBounces($page->getId(), $fromDate); }
/** * {@inheritDoc} */ public function getId() { if ($this->__isInitialized__ === false) { return (int) parent::getId(); } $this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', array()); return parent::getId(); }