Skip to content

Commit

Permalink
Merge pull request #166 from matomo-org/PG-2343-retain-case-option
Browse files Browse the repository at this point in the history
Added an option to retain the case of campaign parameter values via SystemSetting, #PG-2343
  • Loading branch information
AltamashShaikh authored Dec 6, 2024
2 parents 1482511 + a7475fc commit 2adeb49
Show file tree
Hide file tree
Showing 26 changed files with 2,975 additions and 202 deletions.
4 changes: 0 additions & 4 deletions Campaign/CampaignDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace Piwik\Plugins\MarketingCampaignsReporting\Campaign;

use Piwik\Common;
use Piwik\Plugins\MarketingCampaignsReporting\MarketingCampaignsReporting;
use Piwik\Tracker\PageUrl;
use Piwik\Tracker\Request;
Expand Down Expand Up @@ -86,9 +85,6 @@ protected function getValueFromQueryString($param, $queryString)
{
$valueFromRequest = UrlHelper::getParameterFromQueryString($queryString, $param) ?? '';
$valueFromRequest = trim(urldecode($valueFromRequest));
if ($param != "mtm_clid") {
$valueFromRequest = Common::mb_strtolower($valueFromRequest);
}
$valueFromRequest = substr($valueFromRequest, 0, 250);
if (!empty($valueFromRequest)) {
return $valueFromRequest;
Expand Down
7 changes: 7 additions & 0 deletions RecordBuilders/CampaignReporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
use Piwik\ArchiveProcessor\RecordBuilder;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\DataAccess\LogAggregator;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Plugins\MarketingCampaignsReporting\Archiver;
use Piwik\Plugins\MarketingCampaignsReporting\SystemSettings;
use Piwik\Version;

class CampaignReporting extends RecordBuilder
Expand Down Expand Up @@ -132,6 +134,7 @@ protected function aggregateFromLogs(LogAggregator $logAggregator, array $record

protected function getLabelFromRowDimensions(array $dimensionsAsLabel, array $row): string
{
$systemSettings = StaticContainer::get(SystemSettings::class);
$labels = [];
foreach ($dimensionsAsLabel as $dimensionLabelPart) {
if (
Expand All @@ -142,6 +145,10 @@ protected function getLabelFromRowDimensions(array $dimensionsAsLabel, array $ro
}
}
$label = implode(Archiver::SEPARATOR_COMBINED_DIMENSIONS, $labels);
if ($label && !$systemSettings->doNotChangeCaseOfUtmParameters->getValue()) {
$label = Common::mb_strtolower($label);
}

return $label;
}

Expand Down
30 changes: 30 additions & 0 deletions SystemSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\MarketingCampaignsReporting;

use Piwik\Piwik;
use Piwik\Settings\Setting;
use Piwik\Settings\FieldConfig;

class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
{
/** @var Setting */
public $doNotChangeCaseOfUtmParameters;

protected function init()
{
$this->doNotChangeCaseOfUtmParameters = $this->makeSetting('doNotChangeCaseOfUtmParameters', true, FieldConfig::TYPE_BOOL, function (FieldConfig $field) {
$field->title = Piwik::translate('MarketingCampaignsReporting_DoNotChangeCaseOfUtmParametersTitle');
$field->description = Piwik::translate('MarketingCampaignsReporting_DoNotChangeCaseOfUtmParametersDescription');
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
});
}
}
38 changes: 38 additions & 0 deletions Updates/5.1.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\MarketingCampaignsReporting;

use Piwik\Container\StaticContainer;
use Piwik\Updater;
use Piwik\Updates as PiwikUpdates;
use Piwik\Updater\Migration\Factory as MigrationFactory;

class Updates_5_1_0 extends PiwikUpdates
{
/**
* @var MigrationFactory
*/
private $migration;

public function __construct(MigrationFactory $factory)
{
$this->migration = $factory;
}

public function doUpdate(Updater $updater)
{
// set the SystemSetting to false for existing installs as it will be true by default
$systemSettings = StaticContainer::get(SystemSettings::class);
$systemSettings->doNotChangeCaseOfUtmParameters->setIsWritableByCurrentUser(true);
$systemSettings->doNotChangeCaseOfUtmParameters->setValue(false);
$systemSettings->save();
}
}
4 changes: 3 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"Groups": "Campaign Groups",
"Placements": "Campaign Placements",
"CampaignIds": "Campaign Ids",
"CombinedSourcesMediums": "Campaign Source - Medium"
"CombinedSourcesMediums": "Campaign Source - Medium",
"DoNotChangeCaseOfUtmParametersTitle": "Keep campaign parameter capitalisation",
"DoNotChangeCaseOfUtmParametersDescription": "When enabled, Matomo will keep the original capitalisation of campaign parameters to ensure compatibility with third-party services like Google. If disabled, all campaign parameters will be converted to lowercase."
}
}
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "MarketingCampaignsReporting",
"description": "Measure the effectiveness of your marketing campaigns. New reports, segments & track up to five channels: campaign, source, medium, keyword, content.",
"version": "5.0.4",
"version": "5.1.0",
"keywords": ["Campaign", "Marketing", "Channels", "UTM tags"],
"license": "GPL v3+",
"homepage": "https://matomo.org",
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/TrackAdvancedCampaigns.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
namespace Piwik\Plugins\MarketingCampaignsReporting\tests\Fixtures;

use Piwik;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Tests\Framework\Fixture;
use Piwik\Plugins\MarketingCampaignsReporting\SystemSettings;

class TrackAdvancedCampaigns extends Fixture
{
Expand All @@ -39,6 +41,11 @@ public function setUp(): void
{
Piwik\Plugin\Manager::getInstance()->activatePlugin('MarketingCampaignsReporting');

$systemSettings = StaticContainer::get(SystemSettings::class);
$systemSettings->doNotChangeCaseOfUtmParameters->setIsWritableByCurrentUser(true);
$systemSettings->doNotChangeCaseOfUtmParameters->setValue(false);
$systemSettings->save();

$this->orderIndex = 0;

$this->setUpWebsite();
Expand Down
80 changes: 80 additions & 0 deletions tests/Integration/SystemSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\MarketingCampaignsReporting\tests\Integration;

use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Plugins\MarketingCampaignsReporting\SystemSettings;

/**
* @group SystemSettings
* @group Plugins
*/
class SystemSettingsTest extends IntegrationTestCase
{
/**
* @var SystemSettings
*/
private $settings;

public function setUp(): void
{
parent::setUp();

Fixture::loadAllTranslations();

Fixture::createSuperUser();
Fixture::createWebsite('2014-01-01 00:01:02');

$this->settings = new SystemSettings();
}

public function tearDown(): void
{
parent::tearDown();

Fixture::resetTranslations();
}

public function test_doNotChangeCaseOfUtmParameters_default()
{
$this->assertTrue($this->settings->doNotChangeCaseOfUtmParameters->getValue());
}

public function test_doNotChangeCaseOfUtmParameters_shouldThrowException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The value for the "Keep campaign parameter capitalisation" field in the "MarketingCampaignsReporting" plugin is not allowed');
$this->settings->doNotChangeCaseOfUtmParameters->setValue('aa');
}

public function test_doNotChangeCaseOfUtmParameters_updateSuccess()
{
$this->settings->doNotChangeCaseOfUtmParameters->setValue(true);
$this->settings->save();
$this->assertTrue($this->settings->doNotChangeCaseOfUtmParameters->getValue());

$this->settings->doNotChangeCaseOfUtmParameters->setValue(1);
$this->settings->save();
$this->assertTrue($this->settings->doNotChangeCaseOfUtmParameters->getValue());

$this->settings->doNotChangeCaseOfUtmParameters->setValue(0);
$this->settings->save();
$this->assertFalse($this->settings->doNotChangeCaseOfUtmParameters->getValue());

$this->settings->doNotChangeCaseOfUtmParameters->setValue('1');
$this->settings->save();
$this->assertTrue($this->settings->doNotChangeCaseOfUtmParameters->getValue());

$this->settings->doNotChangeCaseOfUtmParameters->setValue('0');
$this->settings->save();
$this->assertFalse($this->settings->doNotChangeCaseOfUtmParameters->getValue());
}
}
10 changes: 7 additions & 3 deletions tests/System/TrackSeveralCampaignsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ public function getApiForTesting()
{
$dateWithPluginEnabled = self::$fixture->dateTimeWithPluginEnabled;
$dateTime = self::$fixture->dateTime;
$phpVersionPrefix = version_compare(PHP_VERSION, 8.3, '<') ? 'min_php_' : '';

$apiToTest[] = [
'API.get',
[
'idSite' => self::$fixture->idSite,
'date' => $dateWithPluginEnabled,
'periods' => ['day'],
'testSuffix' => version_compare(Version::VERSION, '5.2.0-b6', '<') ? 'old' : '',
'testSuffix' => (!empty($phpVersionPrefix) ? $phpVersionPrefix : 'max_php_') . (version_compare(Version::VERSION, '5.2.0-b6', '<') ? 'old' : ''),
]
];

Expand All @@ -95,6 +96,7 @@ public function getApiForTesting()
'date' => $dateWithPluginEnabled,
'periods' => ['day'],
'xmlFieldsToRemove' => $columnsToHide,
'testSuffix' => $phpVersionPrefix,
]
];

Expand Down Expand Up @@ -214,13 +216,15 @@ public function getReferrerApiForTesting()
'Referrers.getCampaigns',
];

$phpVersionPrefix = version_compare(PHP_VERSION, 8.3, '<') ? 'min_php_' : '';

$apiToTest[] = [
$api,
[
'idSite' => self::$fixture->idSite,
'date' => $dateWithPluginEnabled,
'periods' => ['day'],
'testSuffix' => 'expanded',
'testSuffix' => $phpVersionPrefix . 'expanded',
'otherRequestParameters' => ['expanded' => 1],
]
];
Expand All @@ -230,7 +234,7 @@ public function getReferrerApiForTesting()
'idSite' => self::$fixture->idSite,
'date' => $dateWithPluginEnabled,
'periods' => ['day'],
'testSuffix' => 'flat',
'testSuffix' => $phpVersionPrefix . 'flat',
'otherRequestParameters' => ['flat' => 1, 'expanded' => 0],
]
];
Expand Down
4 changes: 2 additions & 2 deletions tests/System/expected/test___API.get_day.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<Referrers_distinctKeywords>0</Referrers_distinctKeywords>
<Referrers_distinctWebsites>0</Referrers_distinctWebsites>
<Referrers_distinctWebsitesUrls>0</Referrers_distinctWebsitesUrls>
<Referrers_distinctCampaigns>17</Referrers_distinctCampaigns>
<Referrers_distinctCampaigns>18</Referrers_distinctCampaigns>
<PagePerformance_network_time>0</PagePerformance_network_time>
<PagePerformance_network_hits>0</PagePerformance_network_hits>
<PagePerformance_servery_time>0</PagePerformance_servery_time>
Expand Down Expand Up @@ -87,4 +87,4 @@
<bounce_rate>100%</bounce_rate>
<nb_actions_per_visit>1</nb_actions_per_visit>
<avg_time_on_site>121</avg_time_on_site>
</result>
</result>
Loading

0 comments on commit 2adeb49

Please sign in to comment.