diff options
Diffstat (limited to 'plugins/Referrers/Columns/Base.php')
-rw-r--r-- | plugins/Referrers/Columns/Base.php | 100 |
1 files changed, 75 insertions, 25 deletions
diff --git a/plugins/Referrers/Columns/Base.php b/plugins/Referrers/Columns/Base.php index a27aa925e2..0d9a658f79 100644 --- a/plugins/Referrers/Columns/Base.php +++ b/plugins/Referrers/Columns/Base.php @@ -1,4 +1,5 @@ <?php + /** * Matomo - free/libre analytics platform * @@ -6,9 +7,11 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later * */ + namespace Piwik\Plugins\Referrers\Columns; use Piwik\Common; +use Piwik\Exception\UnexpectedWebsiteFoundException; use Piwik\Piwik; use Piwik\Plugin\Dimension\VisitDimension; use Piwik\Plugins\Referrers\SearchEngine as SearchEngineDetection; @@ -18,12 +21,10 @@ use Piwik\Tracker\Cache; use Piwik\Tracker\PageUrl; use Piwik\Tracker\Request; use Piwik\Tracker\Visitor; -use Piwik\Tracker\Action; use Piwik\UrlHelper; abstract class Base extends VisitDimension { - // @see detect*() referrer methods protected $typeReferrerAnalyzed; protected $nameReferrerAnalyzed; @@ -71,16 +72,22 @@ abstract class Base extends VisitDimension { $this->idsite = $idSite; - // default values for the referer_* fields $referrerUrl = Common::unsanitizeInputValue($referrerUrl); - if (!empty($referrerUrl) - && !UrlHelper::isLookLikeUrl($referrerUrl) + + // Ignore referrer url if it doesn't look like a URL or is excluded in settings + if ( + !empty($referrerUrl) + && ( + !UrlHelper::isLookLikeUrl($referrerUrl) + || $this->isReferrerExcluded($referrerUrl) + ) ) { $referrerUrl = ''; } $currentUrl = PageUrl::cleanupUrl($currentUrl); + // default values for the referer_* fields $this->referrerUrl = $referrerUrl; $this->referrerUrlParse = @parse_url($this->referrerUrl); $this->currentUrlParse = @parse_url($currentUrl); @@ -96,7 +103,8 @@ abstract class Base extends VisitDimension $referrerDetected = $this->detectReferrerCampaign($request, $visitor); if (!$referrerDetected) { - if ($this->detectReferrerDirectEntry($request) + if ( + $this->detectReferrerDirectEntry($request) || $this->detectReferrerSearchEngine() || $this->detectReferrerSocialNetwork() ) { @@ -121,12 +129,12 @@ abstract class Base extends VisitDimension $this->excludeQueryParamsFromReferrerUrl(); - $referrerInformation = array( + $referrerInformation = [ 'referer_type' => $this->typeReferrerAnalyzed, 'referer_name' => $this->nameReferrerAnalyzed, 'referer_keyword' => $this->keywordReferrerAnalyzed, 'referer_url' => $this->referrerUrl, - ); + ]; if (!empty($referrerInformation['referer_name'])) { $referrerInformation['referer_name'] = $this->truncateReferrerName($referrerInformation['referer_name']); @@ -139,6 +147,46 @@ abstract class Base extends VisitDimension return $referrerInformation; } + protected function getExcludedReferrers($idSite): array + { + try { + $attributes = Cache::getCacheWebsiteAttributes($idSite); + + if (isset($attributes['excluded_referrers'])) { + return $attributes['excluded_referrers']; + } + } catch (UnexpectedWebsiteFoundException $e) { + $cached = Cache::getCacheGeneral(); + if (isset($cached['global_excluded_referrers'])) { + return $cached['global_excluded_referrers']; + } + } + + return []; + } + + protected function isReferrerExcluded(string $referrerUrl): bool + { + $urlsByHost = []; + $parsedReferrer = @parse_url($referrerUrl); + + $excludedUrls = new SiteUrls(); + $excludedReferrers = $this->getExcludedReferrers($this->idsite); + foreach ($excludedReferrers as $excludedReferrer) { + // ensure referrer is prefixed with protocol, so a parse_url works + $excludedReferrer = 'https://' . preg_replace('/^https?:\/\//', '', $excludedReferrer); + $excludedUrls->addUrlByHost($urlsByHost, $this->idsite, $excludedReferrer); + } + + $matchingSites = $excludedUrls->getIdSitesMatchingUrl($parsedReferrer, $urlsByHost); + + if (is_array($matchingSites) && in_array($this->idsite, $matchingSites)) { + return true; + } + + return false; + } + protected function excludeQueryParamsFromReferrerUrl() { $parametersToExclude = []; @@ -236,7 +284,7 @@ abstract class Base extends VisitDimension * logic. * @param string referrerUrl The referrer URL from the tracking request. */ - Piwik::postEvent('Tracker.detectReferrerSearchEngine', array(&$searchEngineInformation, $this->referrerUrl)); + Piwik::postEvent('Tracker.detectReferrerSearchEngine', [&$searchEngineInformation, $this->referrerUrl]); $cachedReferrerSearchEngine[$this->referrerUrl] = $searchEngineInformation; $cache->save($cacheKey, $cachedReferrerSearchEngine); @@ -287,7 +335,7 @@ abstract class Base extends VisitDimension * logic. * @param string referrerUrl The referrer URL from the tracking request. */ - Piwik::postEvent('Tracker.detectReferrerSocialNetwork', array(&$socialNetworkName, $this->referrerUrl)); + Piwik::postEvent('Tracker.detectReferrerSocialNetwork', [&$socialNetworkName, $this->referrerUrl]); $cachedReferrerSocialNetworks[$this->referrerUrl] = $socialNetworkName; $cache->save($cacheKey, $cachedReferrerSocialNetworks); @@ -334,7 +382,8 @@ abstract class Base extends VisitDimension protected function detectReferrerCampaignFromLandingUrl() { - if (!isset($this->currentUrlParse['query']) + if ( + !isset($this->currentUrlParse['query']) && !isset($this->currentUrlParse['fragment']) ) { return false; @@ -351,7 +400,8 @@ abstract class Base extends VisitDimension } // 2) Detect from fragment #hash - if (!$found + if ( + !$found && isset($this->currentUrlParse['fragment']) ) { $this->detectCampaignFromString($this->currentUrlParse['fragment']); @@ -385,7 +435,7 @@ abstract class Base extends VisitDimension return $cache['allUrlsByHostAndIdSite']; } - return array(); + return []; } /** @@ -433,8 +483,10 @@ abstract class Base extends VisitDimension protected function detectCampaignKeywordFromReferrerUrl() { - if (!empty($this->nameReferrerAnalyzed) - && !empty($this->keywordReferrerAnalyzed)) { + if ( + !empty($this->nameReferrerAnalyzed) + && !empty($this->keywordReferrerAnalyzed) + ) { // keyword is already set, we skip return true; } @@ -448,7 +500,8 @@ abstract class Base extends VisitDimension } // Set the keyword, to the hostname found, in a Adsense Referrer URL '&url=' parameter - if (empty($this->keywordReferrerAnalyzed) + if ( + empty($this->keywordReferrerAnalyzed) && !empty($this->referrerUrlParse['query']) && !empty($this->referrerHost) && (strpos($this->referrerHost, 'googleads') !== false || strpos($this->referrerHost, 'doubleclick') !== false) @@ -458,7 +511,6 @@ abstract class Base extends VisitDimension if (!empty($value)) { $parsedAdsenseReferrerUrl = parse_url($value); if (!empty($parsedAdsenseReferrerUrl['host'])) { - if (empty($this->nameReferrerAnalyzed)) { $type = $this->getParameterValueFromReferrerUrl('ad_type'); $type = $type ? " ($type)" : ''; @@ -469,7 +521,6 @@ abstract class Base extends VisitDimension } } } - } /** @@ -530,7 +581,6 @@ abstract class Base extends VisitDimension /** * @param Request $request * @param Visitor $visitor - * @param Action|null $action * @return mixed */ public function getValueForRecordGoal(Request $request, Visitor $visitor) @@ -556,7 +606,8 @@ abstract class Base extends VisitDimension // 0) In some (unknown!?) cases the campaign is not found in the attribution cookie, but the URL ref was found. // In this case we look up if the current visit is credited to a campaign and will credit this campaign rather than the URL ref (since campaigns have higher priority) - if (empty($referrerCampaignName) + if ( + empty($referrerCampaignName) && $type == Common::REFERRER_TYPE_CAMPAIGN && !empty($name) ) { @@ -570,12 +621,11 @@ abstract class Base extends VisitDimension Common::printDebug("Campaign information from 1st party cookie is used."); } // 2) Referrer URL parsing elseif (!empty($referrerUrl)) { - $idSite = $request->getIdSite(); $referrer = $this->getReferrerInformation($referrerUrl, $currentUrl = '', $idSite, $request, $visitor); // if the parsed referrer is interesting enough, ie. website, social network or search engine - if (in_array($referrer['referer_type'], array(Common::REFERRER_TYPE_SEARCH_ENGINE, Common::REFERRER_TYPE_WEBSITE, Common::REFERRER_TYPE_SOCIAL_NETWORK))) { + if (in_array($referrer['referer_type'], [Common::REFERRER_TYPE_SEARCH_ENGINE, Common::REFERRER_TYPE_WEBSITE, Common::REFERRER_TYPE_SOCIAL_NETWORK])) { $type = $referrer['referer_type']; $name = $referrer['referer_name']; $keyword = $referrer['referer_keyword']; @@ -590,11 +640,11 @@ abstract class Base extends VisitDimension $this->setCampaignValuesToLowercase($type, $name, $keyword); - $fields = array( + $fields = [ 'referer_type' => $type, 'referer_name' => $name, 'referer_keyword' => $keyword, - ); + ]; if (array_key_exists($this->columnName, $fields)) { return $fields[$this->columnName]; @@ -622,7 +672,7 @@ abstract class Base extends VisitDimension protected function isReferrerInformationNew(Visitor $visitor, $information) { - foreach (array('referer_keyword', 'referer_name', 'referer_type') as $infoName) { + foreach (['referer_keyword', 'referer_name', 'referer_type'] as $infoName) { if ($this->hasReferrerColumnChanged($visitor, $information, $infoName)) { return true; } |