Skip to content

Commit

Permalink
Merge pull request #56 from matomo-org/matomo4tweaks
Browse files Browse the repository at this point in the history
Some tweaks for Matomo 4
  • Loading branch information
tsteur authored Jul 31, 2020
2 parents f727b34 + fced240 commit 5f08f3d
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 50 deletions.
10 changes: 5 additions & 5 deletions AnonymousPiwikUsageMeasurement.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function registerEvents()
{
return array(
'AssetManager.getJavaScriptFiles' => 'getJsFiles',
'Template.jsGlobalVariables' => 'addPiwikClientTracking',
'Template.jsGlobalVariables' => 'addMatomoClientTracking',
'API.Request.dispatch' => 'logStartTimeOfApiCall',
'API.Request.dispatch.end' => 'trackApiCall',
'Db.getTablesInstalled' => 'getTablesInstalled'
Expand Down Expand Up @@ -101,7 +101,7 @@ public function trackApiCall(&$return, $endHookParams)

public function getJsFiles(&$jsFiles)
{
$jsFiles[] = 'piwik.js';
$jsFiles[] = 'matomo.js';
$jsFiles[] = 'plugins/AnonymousPiwikUsageMeasurement/javascripts/url.js';
$jsFiles[] = 'plugins/AnonymousPiwikUsageMeasurement/javascripts/tracking.js';
$jsFiles[] = 'plugins/AnonymousPiwikUsageMeasurement/angularjs/common/dashboard.directive.js';
Expand All @@ -111,10 +111,10 @@ public function getJsFiles(&$jsFiles)
$jsFiles[] = 'plugins/AnonymousPiwikUsageMeasurement/angularjs/common/multisites.directive.js';
}

public function addPiwikClientTracking(&$out)
public function addMatomoClientTracking(&$out)
{
$settings = StaticContainer::get('Piwik\Plugins\AnonymousPiwikUsageMeasurement\SystemSettings');
$userSettings = StaticContainer::get('Piwik\Plugins\AnonymousPiwikUsageMeasurement\UserSettings');
$settings = StaticContainer::get(SystemSettings::class);
$userSettings = StaticContainer::get(UserSettings::class);

$config = array(
'targets' => array(),
Expand Down
7 changes: 2 additions & 5 deletions SystemSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
/** @var Setting */
public $canUserOptOut;

/** @var Setting */
public $trackToPiwik;

/** @var Setting */
public $ownPiwikSiteId;

Expand Down Expand Up @@ -119,11 +116,11 @@ private function createTrackToCustomSiteUrlSetting()
return '';
}

if (!Common::stringEndsWith($value, '/piwik.php')) {
if (!Common::stringEndsWith($value, '/piwik.php') && !Common::stringEndsWith($value, '/matomo.php')) {
if (!Common::stringEndsWith($value, '/')) {
$value .= '/';
}
$value .= 'piwik.php';
$value .= 'matomo.php';
}

return $value;
Expand Down
2 changes: 1 addition & 1 deletion Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Tracker extends \MatomoTracker

/**
* The method name is not ideal but want to make sure MatomoTracker won't define such method at some point.
* @param string $baseApiUrl eg 'http://demo-anonymous.matomo.org/piwik.php'
* @param string $baseApiUrl eg 'http://demo-anonymous.matomo.org/matomo.php'
*/
public function setBaseApiUrl($baseApiUrl)
{
Expand Down
18 changes: 8 additions & 10 deletions Tracker/CustomVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Piwik\Container\StaticContainer;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Plugins\SegmentEditor\Services\StoredSegmentService;

class CustomVariables
{
Expand Down Expand Up @@ -53,16 +54,13 @@ public function getServerVisitCustomVariables()
),
);

$segmentClass = 'Piwik\Plugins\SegmentEditor\Services\StoredSegmentService';
if (class_exists($segmentClass)) {
$service = StaticContainer::get($segmentClass);
$segments = $service->getAllSegmentsAndIgnoreVisibility();
$customVars[] = array(
'id' => 5,
'name' => 'Num Segments',
'value' => count($segments),
);
}
$service = StaticContainer::get(StoredSegmentService::class);
$segments = $service->getAllSegmentsAndIgnoreVisibility();
$customVars[] = array(
'id' => 5,
'name' => 'Num Segments',
'value' => count($segments),
);

if (isset($mySqlVersion)) {
$customVars[] = array(
Expand Down
3 changes: 1 addition & 2 deletions Tracker/Targets.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Piwik\Common;
use Piwik\Plugins\AnonymousPiwikUsageMeasurement\SystemSettings;
use Piwik\Plugins\AnonymousPiwikUsageMeasurement\Tracker;
use Piwik\SettingsPiwik;
use Piwik\Tests\Framework\Fixture;

Expand Down Expand Up @@ -40,7 +39,7 @@ public function getTargets()
$piwikUrl .= '/';
}
$targets[] = array(
'url' => $piwikUrl . 'piwik.php',
'url' => $piwikUrl . 'matomo.php',
'idSite' => (int) $ownSiteId,
'useAnonymization' => $this->settings->anonymizeSelfPiwik->getValue()
);
Expand Down
21 changes: 0 additions & 21 deletions UserSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,9 @@
*/
class UserSettings extends \Piwik\Settings\Plugin\UserSettings
{
/** @var Setting */
public $canUserOptOut;

/** @var Setting */
public $userTrackingEnabled;

/** @var Setting */
public $trackToPiwik;

/** @var Setting */
public $ownPiwikSiteId;

/** @var Setting */
public $customPiwikSiteId;

/** @var Setting */
public $customPiwikSiteUrl;

/** @var Setting */
public $anonymizeCustomPiwik;

/** @var Setting */
public $anonymizeSelfPiwik;

/**
* @var SystemSettings
*/
Expand Down
7 changes: 5 additions & 2 deletions tests/Integration/SystemSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ public function test_customPiwikSiteUrl_shouldBeAnEmptyStringByDefault()
public function test_customPiwikSiteUrl_shouldAppendPiwikPhpIfNeeded()
{
$this->settings->customPiwikSiteUrl->setValue('http://example.com');
$this->assertSame('http://example.com/piwik.php', $this->settings->customPiwikSiteUrl->getValue());
$this->assertSame('http://example.com/matomo.php', $this->settings->customPiwikSiteUrl->getValue());

$this->settings->customPiwikSiteUrl->setValue('http://example.com/');
$this->assertSame('http://example.com/piwik.php', $this->settings->customPiwikSiteUrl->getValue());
$this->assertSame('http://example.com/matomo.php', $this->settings->customPiwikSiteUrl->getValue());

$this->settings->customPiwikSiteUrl->setValue('http://example.com/piwik.php');
$this->assertSame('http://example.com/piwik.php', $this->settings->customPiwikSiteUrl->getValue());

$this->settings->customPiwikSiteUrl->setValue('http://example.com/matomo.php');
$this->assertSame('http://example.com/matomo.php', $this->settings->customPiwikSiteUrl->getValue());
}

public function test_customPiwikSiteUrl_shouldThrowAnException_WhenNotAValidUrlIsSet()
Expand Down
8 changes: 4 additions & 4 deletions tests/Integration/Tracker/TargetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function test_TrackToCustomPiwikEnabled_ShouldReturnCustomInstanceTarget(
$settings->customPiwikSiteId->setValue(72);
$settings->customPiwikSiteUrl->setValue('http://example.com/piwik');

$customInstance = $this->getCustomPiwikTarget('http://example.com/piwik/piwik.php', 72);
$customInstance = $this->getCustomPiwikTarget('http://example.com/piwik/matomo.php', 72);

$this->assertTargets(array($customInstance), $settings);
}
Expand All @@ -85,12 +85,12 @@ public function test_AllEnabled_shouldReturnAll()
{
$settings = $this->makeSettingsWithNoInstanceEnabled();
$settings->ownPiwikSiteId->setValue($this->idSite);
$settings->customPiwikSiteUrl->setValue('http://example.com/piwik');
$settings->customPiwikSiteUrl->setValue('http://example.com/matomo');
$settings->customPiwikSiteId->setValue(73);

$targets = array(
$this->getOwnPiwikTarget(),
$this->getCustomPiwikTarget('http://example.com/piwik/piwik.php', 73)
$this->getCustomPiwikTarget('http://example.com/matomo/matomo.php', 73)
);

$this->assertTargets($targets, $settings);
Expand All @@ -109,7 +109,7 @@ private function makeSettingsWithNoInstanceEnabled()

private function getOwnPiwikTarget()
{
return $this->getCustomPiwikTarget(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/piwik.php', $this->idSite);
return $this->getCustomPiwikTarget(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/matomo.php', $this->idSite);
}

private function getCustomPiwikTarget($url, $idSite)
Expand Down

0 comments on commit 5f08f3d

Please sign in to comment.