/** * Returns an array containing the following information: * - referer_type * - direct -- absence of referrer URL OR referrer URL has the same host * - site -- based on the referrer URL * - search_engine -- based on the referrer URL * - campaign -- based on campaign URL parameter * * - referer_name * - () * - piwik.net -- site host name * - google.fr -- search engine host name * - adwords-search -- campaign name * * - referer_keyword * - () * - () * - my keyword * - my paid keyword * - () * - () * * - referer_url : the same for all the referrer types * * @param string $referrerUrl must be URL Encoded * @param string $currentUrl * @param int $idSite * @return array */ protected function getReferrerInformation($referrerUrl, $currentUrl, $idSite, Request $request, Visitor $visitor) { $this->idsite = $idSite; // default values for the referer_* fields $referrerUrl = Common::unsanitizeInputValue($referrerUrl); if (!empty($referrerUrl) && !UrlHelper::isLookLikeUrl($referrerUrl)) { $referrerUrl = ''; } $currentUrl = PageUrl::cleanupUrl($currentUrl); $this->referrerUrl = $referrerUrl; $this->referrerUrlParse = @parse_url($this->referrerUrl); $this->currentUrlParse = @parse_url($currentUrl); $this->typeReferrerAnalyzed = Common::REFERRER_TYPE_DIRECT_ENTRY; $this->nameReferrerAnalyzed = ''; $this->keywordReferrerAnalyzed = ''; $this->referrerHost = ''; if (isset($this->referrerUrlParse['host'])) { $this->referrerHost = $this->referrerUrlParse['host']; } $referrerDetected = $this->detectReferrerCampaign($request, $visitor); if (!$referrerDetected) { if ($this->detectReferrerDirectEntry() || $this->detectReferrerSearchEngine()) { $referrerDetected = true; } } if (!$referrerDetected && !empty($this->referrerHost)) { $this->typeReferrerAnalyzed = Common::REFERRER_TYPE_WEBSITE; $this->nameReferrerAnalyzed = Common::mb_strtolower($this->referrerHost); $urlsByHost = $this->getCachedUrlsByHostAndIdSite(); $directEntry = new SiteUrls(); $path = $directEntry->getPathMatchingUrl($this->referrerUrlParse, $urlsByHost); if (!empty($path) && $path !== '/') { $this->nameReferrerAnalyzed .= rtrim($path, '/'); } } $referrerInformation = array('referer_type' => $this->typeReferrerAnalyzed, 'referer_name' => $this->nameReferrerAnalyzed, 'referer_keyword' => $this->keywordReferrerAnalyzed, 'referer_url' => $this->referrerUrl); return $referrerInformation; }
/** * @dataProvider getTestPathMatchingUrl */ public function test_getPathMatchingUrl($expectedMatchSites, $parsedUrl) { $urlsGroupedByHost = array('apache.piwik' => array('/foo/second/' => array(2), '/foo/sec/' => array(4), '/foo/bar/' => array(1), '/third/' => array(3), '/test/' => array(1, 2), '/' => array(1, 3)), 'example.org' => array('/foo/test/two/' => array(3), '/foo/second/' => array(6), '/' => array(2, 5))); $matchedSites = $this->siteUrls->getPathMatchingUrl($parsedUrl, $urlsGroupedByHost); $this->assertSame($expectedMatchSites, $matchedSites); }