Esempio n. 1
0
 /**
  * Record page hit.
  *
  * @param           $page
  * @param Request   $request
  * @param string    $code
  * @param Lead|null $lead
  * @param array     $query
  *
  * @return Hit $hit
  *
  * @throws \Exception
  */
 public function hitPage($page, Request $request, $code = '200', Lead $lead = null, $query = [])
 {
     // Don't skew results with user hits
     if (!$this->security->isAnonymous()) {
         return;
     }
     // Process the query
     if (empty($query)) {
         $query = $this->getHitQuery($request, $page);
     }
     $hit = new Hit();
     $hit->setDateHit(new \Datetime());
     // Check for existing IP
     $ipAddress = $this->ipLookupHelper->getIpAddress();
     $hit->setIpAddress($ipAddress);
     // Check for any clickthrough info
     $clickthrough = [];
     if (!empty($query['ct'])) {
         $clickthrough = $query['ct'];
         if (!is_array($clickthrough)) {
             $clickthrough = $this->decodeArrayFromUrl($clickthrough);
         }
         if (!empty($clickthrough['channel'])) {
             if (count($clickthrough['channel']) === 1) {
                 $channelId = reset($clickthrough['channel']);
                 $channel = key($clickthrough['channel']);
             } else {
                 $channel = $clickthrough['channel'][0];
                 $channelId = (int) $clickthrough['channel'][1];
             }
             $hit->setSource($channel);
             $hit->setSourceId($channelId);
         } elseif (!empty($clickthrough['source'])) {
             $hit->setSource($clickthrough['source'][0]);
             $hit->setSourceId($clickthrough['source'][1]);
         }
         if (!empty($clickthrough['email'])) {
             $emailRepo = $this->em->getRepository('MauticEmailBundle:Email');
             if ($emailEntity = $emailRepo->getEntity($clickthrough['email'])) {
                 $hit->setEmail($emailEntity);
             }
         }
     }
     // Get lead if required
     if (null == $lead) {
         $lead = $this->leadModel->getContactFromRequest($query);
     }
     $this->leadModel->saveEntity($lead);
     // Set info from request
     $hit->setQuery($query);
     $hit->setUrl(isset($query['page_url']) ? $query['page_url'] : $request->getRequestUri());
     if (isset($query['page_referrer'])) {
         $hit->setReferer($query['page_referrer']);
     }
     if (isset($query['page_language'])) {
         $hit->setPageLanguage($query['page_language']);
     }
     if (isset($query['page_title'])) {
         $hit->setUrlTitle($query['page_title']);
     }
     // Store tracking ID
     list($trackingId, $trackingNewlyGenerated) = $this->leadModel->getTrackingCookie();
     $hit->setTrackingId($trackingId);
     $hit->setLead($lead);
     $isUnique = $trackingNewlyGenerated;
     if (!$trackingNewlyGenerated) {
         $lastHit = $request->cookies->get('mautic_referer_id');
         if (!empty($lastHit)) {
             //this is not a new session so update the last hit if applicable with the date/time the user left
             $this->getHitRepository()->updateHitDateLeft($lastHit);
         }
         // Check if this is a unique page hit
         $isUnique = $this->getHitRepository()->isUniquePageHit($page, $trackingId);
     }
     if (!empty($page)) {
         if ($page instanceof Page) {
             $hit->setPage($page);
             $hit->setPageLanguage($page->getLanguage());
             $isVariant = $isUnique ? $page->getVariantStartDate() : false;
             try {
                 $this->getRepository()->upHitCount($page->getId(), 1, $isUnique, !empty($isVariant));
             } catch (\Exception $exception) {
                 $this->logger->addError($exception->getMessage(), ['exception' => $exception]);
             }
         } elseif ($page instanceof Redirect) {
             $hit->setRedirect($page);
             try {
                 $this->pageRedirectModel->getRepository()->upHitCount($page->getId(), 1, $isUnique);
                 // If this is a trackable, up the trackable counts as well
                 if (!empty($clickthrough['channel'])) {
                     $channelId = reset($clickthrough['channel']);
                     $channel = key($clickthrough['channel']);
                     $this->pageTrackableModel->getRepository()->upHitCount($page->getId(), $channel, $channelId, 1, $isUnique);
                 }
             } catch (\Exception $exception) {
                 if (MAUTIC_ENV === 'dev') {
                     throw $exception;
                 } else {
                     $this->logger->addError($exception->getMessage(), ['exception' => $exception]);
                 }
             }
         }
     }
     //glean info from the IP address
     if ($details = $ipAddress->getIpDetails()) {
         $hit->setCountry($details['country']);
         $hit->setRegion($details['region']);
         $hit->setCity($details['city']);
         $hit->setIsp($details['isp']);
         $hit->setOrganization($details['organization']);
     }
     $hit->setCode($code);
     if (!$hit->getReferer()) {
         $hit->setReferer($request->server->get('HTTP_REFERER'));
     }
     $hit->setUserAgent($request->server->get('HTTP_USER_AGENT'));
     $hit->setRemoteHost($request->server->get('REMOTE_HOST'));
     if ($isUnique) {
         // Add UTM tags entry if a UTM tag exist
         $queryHasUtmTags = false;
         if (!is_array($query)) {
             parse_str($query, $query);
         }
         foreach ($query as $key => $value) {
             if (strpos($key, 'utm_') !== false) {
                 $queryHasUtmTags = true;
                 break;
             }
         }
         if ($queryHasUtmTags) {
             $utmTags = new UtmTag();
             $utmTags->setDateAdded($hit->getDateHit());
             $utmTags->setUrl($hit->getUrl());
             $utmTags->setReferer($hit->getReferer());
             $utmTags->setQuery($hit->getQuery());
             $utmTags->setUserAgent($hit->getUserAgent());
             $utmTags->setRemoteHost($hit->getRemoteHost());
             $utmTags->setLead($lead);
             if (key_exists('utm_campaign', $query)) {
                 $utmTags->setUtmCampaign($query['utm_campaign']);
             }
             if (key_exists('utm_term', $query)) {
                 $utmTags->setUtmTerm($query['utm_term']);
             }
             if (key_exists('utm_content', $query)) {
                 $utmTags->setUtmConent($query['utm_content']);
             }
             if (key_exists('utm_medium', $query)) {
                 $utmTags->setUtmMedium($query['utm_medium']);
             }
             if (key_exists('utm_source', $query)) {
                 $utmTags->setUtmSource($query['utm_source']);
             }
             $repo = $this->em->getRepository('MauticLeadBundle:UtmTag');
             $repo->saveEntity($utmTags);
             $this->leadModel->setUtmTags($lead, $utmTags);
         }
     }
     //get a list of the languages the user prefers
     $browserLanguages = $request->server->get('HTTP_ACCEPT_LANGUAGE');
     if (!empty($browserLanguages)) {
         $languages = explode(',', $browserLanguages);
         foreach ($languages as $k => $l) {
             if ($pos = strpos(';q=', $l) !== false) {
                 //remove weights
                 $languages[$k] = substr($l, 0, $pos);
             }
         }
         $hit->setBrowserLanguages($languages);
     }
     //device granularity
     $dd = new DeviceDetector($request->server->get('HTTP_USER_AGENT'));
     $dd->parse();
     $deviceRepo = $this->leadModel->getDeviceRepository();
     $device = $deviceRepo->getDevice(null, $lead, $dd->getDeviceName(), $dd->getBrand(), $dd->getModel());
     if (empty($device)) {
         $device = new LeadDevice();
         $device->setClientInfo($dd->getClient());
         $device->setDevice($dd->getDeviceName());
         $device->setDeviceBrand($dd->getBrand());
         $device->setDeviceModel($dd->getModel());
         $device->setDeviceOs($dd->getOs());
         $device->setDateOpen($hit->getDateHit());
         $device->setLead($lead);
         $this->em->persist($device);
     } else {
         $device = $deviceRepo->getEntity($device['id']);
     }
     $hit->setDeviceStat($device);
     // Wrap in a try/catch to prevent deadlock errors on busy servers
     try {
         $this->em->persist($hit);
         $this->em->flush($hit);
     } catch (\Exception $exception) {
         if (MAUTIC_ENV === 'dev') {
             throw $exception;
         } else {
             $this->logger->addError($exception->getMessage(), ['exception' => $exception]);
         }
     }
     if ($this->dispatcher->hasListeners(PageEvents::PAGE_ON_HIT)) {
         $event = new PageHitEvent($hit, $request, $code, $clickthrough, $isUnique);
         $this->dispatcher->dispatch(PageEvents::PAGE_ON_HIT, $event);
     }
     //save hit to the cookie to use to update the exit time
     $this->cookieHelper->setCookie('mautic_referer_id', $hit->getId());
     return $hit;
 }
Esempio n. 2
0
 /**
  * @param $asset
  * @param null   $request
  * @param string $code
  * @param array  $systemEntry
  *
  * @throws \Doctrine\ORM\ORMException
  * @throws \Exception
  */
 public function trackDownload($asset, $request = null, $code = '200', $systemEntry = [])
 {
     // Don't skew results with in-house downloads
     if (empty($systemEntry) && !$this->security->isAnonymous()) {
         return;
     }
     if ($request == null) {
         $request = $this->request;
     }
     $download = new Download();
     $download->setDateDownload(new \Datetime());
     // Download triggered by lead
     if (empty($systemEntry)) {
         //check for any clickthrough info
         $clickthrough = $request->get('ct', false);
         if (!empty($clickthrough)) {
             $clickthrough = $this->decodeArrayFromUrl($clickthrough);
             if (!empty($clickthrough['lead'])) {
                 $lead = $this->leadModel->getEntity($clickthrough['lead']);
                 if ($lead !== null) {
                     $this->leadModel->setLeadCookie($clickthrough['lead']);
                     list($trackingId, $trackingNewlyGenerated) = $this->leadModel->getTrackingCookie();
                     $leadClickthrough = true;
                     $this->leadModel->setCurrentLead($lead);
                 }
             }
             if (!empty($clickthrough['channel'])) {
                 if (count($clickthrough['channel']) === 1) {
                     $channelId = reset($clickthrough['channel']);
                     $channel = key($clickthrough['channel']);
                 } else {
                     $channel = $clickthrough['channel'][0];
                     $channelId = (int) $clickthrough['channel'][1];
                 }
                 $download->setSource($channel);
                 $download->setSourceId($channelId);
             } elseif (!empty($clickthrough['source'])) {
                 $download->setSource($clickthrough['source'][0]);
                 $download->setSourceId($clickthrough['source'][1]);
             }
             if (!empty($clickthrough['email'])) {
                 $emailRepo = $this->em->getRepository('MauticEmailBundle:Email');
                 if ($emailEntity = $emailRepo->getEntity($clickthrough['email'])) {
                     $download->setEmail($emailEntity);
                 }
             }
         }
         if (empty($leadClickthrough)) {
             list($lead, $trackingId, $trackingNewlyGenerated) = $this->leadModel->getCurrentLead(true);
         }
         $download->setLead($lead);
     } else {
         $trackingId = '';
         if (isset($systemEntry['lead'])) {
             $lead = $systemEntry['lead'];
             if (!$lead instanceof Lead) {
                 $leadId = is_array($lead) ? $lead['id'] : $lead;
                 $lead = $this->em->getReference('MauticLeadBundle:Lead', $leadId);
             }
             $download->setLead($lead);
         }
         if (!empty($systemEntry['source'])) {
             $download->setSource($systemEntry['source'][0]);
             $download->setSourceId($systemEntry['source'][1]);
         }
         if (isset($systemEntry['email'])) {
             $email = $systemEntry['email'];
             if (!$email instanceof Email) {
                 $emailId = is_array($email) ? $email['id'] : $email;
                 $email = $this->em->getReference('MauticEmailBundle:Email', $emailId);
             }
             $download->setEmail($email);
         }
         if (isset($systemEntry['tracking_id'])) {
             $trackingId = $systemEntry['tracking_id'];
             $trackingNewlyGenerated = false;
         } elseif ($this->security->isAnonymous() && !defined('IN_MAUTIC_CONSOLE')) {
             // If the session is anonymous and not triggered via CLI, assume the lead did something to trigger the
             // system forced download such as an email
             list($trackingId, $trackingNewlyGenerated) = $this->leadModel->getTrackingCookie();
         }
     }
     $isUnique = true;
     if (!empty($trackingNewlyGenerated)) {
         // Cookie was just generated so this is definitely a unique download
         $isUnique = $trackingNewlyGenerated;
     } elseif (!empty($trackingId)) {
         // Determine if this is a unique download
         $isUnique = $this->getDownloadRepository()->isUniqueDownload($asset->getId(), $trackingId);
     }
     $download->setTrackingId($trackingId);
     if (!empty($asset) && empty($systemEntry)) {
         $download->setAsset($asset);
         $this->getRepository()->upDownloadCount($asset->getId(), 1, $isUnique);
     }
     //check for existing IP
     $ipAddress = $this->ipLookupHelper->getIpAddress();
     $download->setCode($code);
     $download->setIpAddress($ipAddress);
     if ($request !== null) {
         $download->setReferer($request->server->get('HTTP_REFERER'));
     }
     // Dispatch event
     if ($this->dispatcher->hasListeners(AssetEvents::ASSET_ON_LOAD)) {
         $event = new AssetLoadEvent($download, $isUnique);
         $this->dispatcher->dispatch(AssetEvents::ASSET_ON_LOAD, $event);
     }
     // Wrap in a try/catch to prevent deadlock errors on busy servers
     try {
         $this->em->persist($download);
         $this->em->flush($download);
     } catch (\Exception $e) {
         if (MAUTIC_ENV === 'dev') {
             throw $e;
         } else {
             error_log($e);
         }
     }
     $this->em->detach($download);
 }