diff options
author | Stefan Giehl <stefan@matomo.org> | 2020-05-07 23:27:07 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-07 23:27:07 +0300 |
commit | bd9aa9db1a95e6f1ef74b45e3723849fab2f292c (patch) | |
tree | cee7d409856646bde0419ea82c173f20c2d5026a /plugins | |
parent | e85965095dc4d75eece83b43fc164324452c5e75 (diff) |
Exclude some url parameters from referrer urls (#15905)
Diffstat (limited to 'plugins')
3 files changed, 163 insertions, 1 deletions
diff --git a/plugins/CustomVariables/tests/System/expected/test_CustomVariablesSystemTest__Live.getLastVisitsDetails_day.xml b/plugins/CustomVariables/tests/System/expected/test_CustomVariablesSystemTest__Live.getLastVisitsDetails_day.xml index 6a723dbbbe..c7d0d56976 100644 --- a/plugins/CustomVariables/tests/System/expected/test_CustomVariablesSystemTest__Live.getLastVisitsDetails_day.xml +++ b/plugins/CustomVariables/tests/System/expected/test_CustomVariablesSystemTest__Live.getLastVisitsDetails_day.xml @@ -108,7 +108,7 @@ <referrerName>Google</referrerName> <referrerKeyword>this keyword should be ranked</referrerKeyword> <referrerKeywordPosition>1</referrerKeywordPosition> - <referrerUrl>http://www.google.com/url?sa=t&source=web&cd=1&ved=0CB4QFjAA&url=http://piwik.org/&rct=j&q=this keyword should be ranked&ei=V8WfTePkKKLfiALrpZWGAw&usg=AFQjCNF_MGJRqKPvaKuUokHtZ3VvNG9ALw&sig2=BvKAdCtNixsmfNWXjsNyMw</referrerUrl> + <referrerUrl>http://www.google.com/url?source=web&cd=1&url=http://piwik.org/&rct=j&q=this keyword should be ranked&sig2=BvKAdCtNixsmfNWXjsNyMw</referrerUrl> <referrerSearchEngineUrl>http://google.com</referrerSearchEngineUrl> <referrerSearchEngineIcon>plugins/Morpheus/icons/dist/searchEngines/google.com.png</referrerSearchEngineIcon> <referrerSocialNetworkUrl /> diff --git a/plugins/Referrers/Columns/Base.php b/plugins/Referrers/Columns/Base.php index 41e846b028..3d6d27b158 100644 --- a/plugins/Referrers/Columns/Base.php +++ b/plugins/Referrers/Columns/Base.php @@ -119,6 +119,8 @@ abstract class Base extends VisitDimension } } + $this->excludeQueryParamsFromReferrerUrl(); + $referrerInformation = array( 'referer_type' => $this->typeReferrerAnalyzed, 'referer_name' => $this->nameReferrerAnalyzed, @@ -137,6 +139,60 @@ abstract class Base extends VisitDimension return $referrerInformation; } + protected function excludeQueryParamsFromReferrerUrl() + { + $parametersToExclude = []; + + if (!empty($this->referrerHost) && strpos($this->referrerHost, 'instagram.com') !== false) { + $parametersToExclude[] = 'e'; + $parametersToExclude[] = 's'; + } + if (!empty($this->referrerHost) && strpos($this->referrerHost, 'facebook.com') !== false) { + $parametersToExclude[] = 'h'; + $parametersToExclude[] = 'p'; + } + if (!empty($this->referrerHost) && (strpos($this->referrerHost, 'google.') !== false || strpos($this->referrerHost, 'googleusercontent.') !== false)) { + $parametersToExclude[] = 'ust'; + $parametersToExclude[] = 'usg'; + $parametersToExclude[] = 'usd'; + $parametersToExclude[] = 'sa'; + $parametersToExclude[] = 'sntz'; + $parametersToExclude[] = 'ei'; + $parametersToExclude[] = 'sa'; + $parametersToExclude[] = 'bvm'; + $parametersToExclude[] = 'usg'; + $parametersToExclude[] = 'ved'; + $parametersToExclude[] = 'client'; + $parametersToExclude[] = 'channel'; + } + + if (!empty($this->referrerHost) && strpos($this->referrerHost, 'main.exoclick.com') !== false) { + $parametersToExclude[] = 'data'; + $parametersToExclude[] = 'wpn'; + } + if (!empty($this->referrerHost) && strpos($this->referrerHost, 'youtube.com') !== false) { + $parametersToExclude[] = 'redir_token'; + $parametersToExclude[] = 'html_redirect'; + $parametersToExclude[] = 'continuation'; + $parametersToExclude[] = 'feature'; + } + if (!empty($this->referrerHost) && strpos($this->referrerHost, 'bing.com') !== false) { + $parametersToExclude[] = 'cvid'; + $parametersToExclude[] = 'refig'; + $parametersToExclude[] = 'elv'; + $parametersToExclude[] = 'plvar'; + $parametersToExclude[] = 'setlang'; + $parametersToExclude[] = 'qs'; + $parametersToExclude[] = 'cc'; + $parametersToExclude[] = 'mkt'; + $parametersToExclude[] = 'PC'; + $parametersToExclude[] = 'form'; + $parametersToExclude[] = 'src'; + } + + $this->referrerUrl = PageUrl::excludeQueryParametersFromUrl($this->referrerUrl, $this->idsite, $parametersToExclude); + } + protected function getReferrerInformationFromRequest(Request $request, Visitor $visitor) { $referrerUrl = $request->getParam('urlref'); diff --git a/plugins/Referrers/tests/Integration/Columns/ReferrerUrlTest.php b/plugins/Referrers/tests/Integration/Columns/ReferrerUrlTest.php new file mode 100644 index 0000000000..0c96f8451e --- /dev/null +++ b/plugins/Referrers/tests/Integration/Columns/ReferrerUrlTest.php @@ -0,0 +1,106 @@ +<?php +/** + * Matomo - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\Referrers\tests\Integration\Columns; + +use Piwik\Plugins\Referrers\Columns\ReferrerUrl; +use Piwik\Tests\Framework\Fixture; +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; +use Piwik\Tracker\Cache; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visit\VisitProperties; +use Piwik\Tracker\Visitor; + +/** + * @group Referrers + * @group ReferrerUrlTest + * @group ReferrerUrl + * @group Plugins + */ +class ReferrerUrlTest extends IntegrationTestCase +{ + /** + * @var ReferrerUrl + */ + private $referrerUrl; + private $idSite1 = 1; + + public function setUp() + { + parent::setUp(); + + Cache::clearCacheGeneral(); + + $date = '2012-01-01 00:00:00'; + $ecommerce = false; + + Fixture::createWebsite($date, $ecommerce, $name = 'test1', $url = 'http://piwik.org/foo/bar'); + + $this->referrerUrl = new ReferrerUrl(); + } + + public function tearDown() + { + // clean up your test here if needed + Cache::clearCacheGeneral(); + + parent::tearDown(); + } + + /** + * @dataProvider getReferrerUrls + */ + public function test_onNewVisit_shouldDetectCorrectReferrerUrl($referrerUrl, $expectedUrl) + { + $request = $this->getRequest(['idsite' => $this->idSite1, 'url' => 'http://piwik.org/foo/bar', 'urlref' => $referrerUrl]); + $detectedUrl = $this->referrerUrl->onNewVisit($request, $this->getNewVisitor(), $action = null); + + $this->assertSame($expectedUrl, $detectedUrl); + } + + public function getReferrerUrls() + { + // $referrerUrl, $expectedUrl + return [ + // instagram referrer urls + ['https://l.instagram.com/?u=https%3A%2F%2Fexample.com%2Fexample.com&e=BTPcuqWixl6Mf5hgYPp6wXIlstuaEdJssdYEvT9s8-6yme_lb275lY2Bwc-YvE-fZNtSKux4QB-v8xNk&s=1', + 'https://l.instagram.com/?u=https%3A%2F%2Fexample.com%2Fexample.com'], + ['https://m.instagram.com/?u=https%3A%2F%2Fexample.com%2Fexample.com&e=BTPcuqWixl6Mf5hgYPp6wXIlstuaEdJssdYEvT9s8-6yme_lb275lY2Bwc-YvE-fZNtSKux4QB-v8xNk', + 'https://m.instagram.com/?u=https%3A%2F%2Fexample.com%2Fexample.com'], + + // facebook referrer urls + ['http://l.facebook.com/l.php?u=http://www.example.com.com/&h=BL0RXrrUUyk_ZbqijDe_mVGBi3ZsyVxJEvOfIhjlUEiRy4zkKwYMDUWbuoICNzhC6pKm6zbGCPAJQP4s8e2psymaokRV3dhp7FPx4Zk6B4x0fBbYTi54xynmBsoBRFB7f5t', + 'http://l.facebook.com/l.php?u=http://www.example.com.com/'], + ['http://lm.facebook.com/l.php?u=http://example.com/foobar&h=BT2Dh3r3VDLoabL3Rb1lpmN-_s0lFtReSGzBED3kfUGnaO5fPF-x8LspJAfJN9kkee5ptpybYgyIx68yzgo9kPAN6snSZL_eNcmgu5xhuUcLXJukNKvi0XMOY78Ca9NKexnpJKxKUDeVApPcfB', + 'http://lm.facebook.com/l.php?u=http://example.com/foobar'], + + // google referrer urls + ['https://www.google.com/url?q=https://example.com/foo&sa=D&ust=1689581471834000&usg=BCQjCNFw5f1S7rLgPNephpTW_4-i2KnAGA', + 'https://www.google.com/url?q=https://example.com/foo'], + + // bing referrer urls + ['https://www.bing.com/search?q=foo+bar&form=EDGTCK&qs=AB&cvid=ff8399e313a74fb592b0ca1d91c42224&refig=4540178a841b46ce8de1664920449112&cc=BE&setlang=4k-NL&elv=AXXfrEiqqD9r3GuelwApuloWthKnH5oOVtTkjmeLPBeagbGxe4rwyaaV!5HJFcbCTxaO4q5w7QqvI8XbCTXyJKn1N4PzqCvVFSdBSr*sdwlB&plvar=0', + 'https://www.bing.com/search?q=foo+bar'], + + // ensure custom url still keep those parameters + ['http://www.example.com/index.php?s=test&e=val&h=param&cvid=custom', + 'http://www.example.com/index.php?s=test&e=val&h=param&cvid=custom'] + ]; + } + + private function getRequest($params) + { + return new Request($params); + } + + private function getNewVisitor() + { + return new Visitor(new VisitProperties()); + } + +} |