Skip to content

Commit

Permalink
Full refactor of site content detection and no data page logic (#21029)
Browse files Browse the repository at this point in the history
* refactor site content detection and no data page

* tagmanager submodule

* start to refactor consent manager handling

* fix method name

* small code refactoring

* handle active tab through classes

* improve code and fix some tests

* update fixtures

* migrate and add new tests for SiteContentDetection classes

* remove state from detection classes again

* fix some stuff

* pass detector to render method

* remove not needed constant

* fix caching

* code cleanup

* hide listing on others tab if corresponding tab is displayed

* hide spa/pwa on others pages

* remove incorrectly copied code

* Move detectConsentManager from Tour to SitesManager API

* fix new checks for GA

* updates expected screenshot

* improve detectConsentManager api method

* use correct template var

* removes no longer used translation key

* Moved SPA/PWA class to TagManager

* updates submodule

* fix typo

* fix translation string

* cs

* Move tracking code tab to new Matomo detection class and remove duplicate code for email generation

* apply some review feedback

* submodule update

* clean up installation controller

* update version to 5.0.0-rc3

* submodule update
  • Loading branch information
sgiehl authored Sep 15, 2023
1 parent b17a741 commit 77b4d09
Show file tree
Hide file tree
Showing 93 changed files with 3,468 additions and 1,776 deletions.
423 changes: 186 additions & 237 deletions core/SiteContentDetector.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plugins/CoreAdminHome/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function checkSiteHasTrackedVisits($idSite)
{
$this->rememberTrackingCodeReminderRan($idSite);

if (!SitesManager::shouldPerormEmptySiteCheck($idSite)) {
if (!SitesManager::shouldPerformEmptySiteCheck($idSite)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
}

#tracking-code li .trackingCodeAdvancedOptions .advance-option {
.site-without-data #matomo li .trackingCodeAdvancedOptions .advance-option {
margin-top: 1rem;
margin-left: 1.25rem
}
4 changes: 2 additions & 2 deletions plugins/CoreAdminHome/templates/trackingCodeGenerator.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<a href="#/trackingApi">{{ 'CoreAdminHome_HttpTrackingApi'|translate }}</a>
<a href="#/singlePageApplication">{{ 'SitesManager_SiteWithoutDataSinglePageApplication'|translate }}</a>
<a href="#/google-tag-manager">{{ 'SitesManager_SiteWithoutDataGoogleTagManager'|translate }}</a>
<a href="#/wordpress">Wordpress</a>
<a href="#/wordpress">WordPress</a>
<a href="#/cloudflare">Cloudflare</a>
<a href="#/vue">Vue.js</a>
<a href="#/react">React.js</a>
Expand Down Expand Up @@ -71,7 +71,7 @@
<p>{{ 'CoreAdminHome_GoogleTagManagerDescription'|translate('<a href="https://matomo.org/faq/new-to-piwik/how-do-i-use-matomo-analytics-within-gtm-google-tag-manager/" rel="noreferrer noopener" target="_blank">','</a>')|raw }}</p>
</div>

<div vue-entry="CoreHome.ContentBlock" content-title="Wordpress" anchor="wordpress">
<div vue-entry="CoreHome.ContentBlock" content-title="WordPress" anchor="wordpress">
<p>{{ 'CoreAdminHome_WordpressDescription'|translate('<a href="https://matomo.org/faq/new-to-piwik/how-do-i-install-the-matomo-tracking-code-on-wordpress/" target="_blank">','</a>')|raw }}</p>
</div>

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 29 additions & 29 deletions plugins/CoreAdminHome/vue/dist/CoreAdminHome.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plugins/CoreAdminHome/vue/dist/CoreAdminHome.umd.min.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default defineComponent({
{
module: 'API',
format: 'json',
method: 'Tour.detectConsentManager',
method: 'SitesManager.detectConsentManager',
idSite,
filter_limit: '-1',
},
Expand Down
47 changes: 9 additions & 38 deletions plugins/Installation/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
use Piwik\Plugins\Diagnostics\DiagnosticService;
use Piwik\Plugins\LanguagesManager\LanguagesManager;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Plugins\SitesManager\SitesManager;
use Piwik\Plugins\UsersManager\API as APIUsersManager;
use Piwik\Plugins\UsersManager\NewsletterSignup;
use Piwik\Plugins\UsersManager\UserUpdater;
use Piwik\ProxyHeaders;
use Piwik\SettingsPiwik;
use Piwik\SiteContentDetector;
use Piwik\Tracker\TrackerCodeGenerator;
use Piwik\Translation\Translator;
use Piwik\Updater;
Expand All @@ -46,15 +46,11 @@
class Controller extends \Piwik\Plugin\ControllerAdmin
{

public function __construct(SiteContentDetector $siteContentDetector)
public function __construct()
{
$this->siteContentDetector = $siteContentDetector;
parent::__construct();
}

/** @var SiteContentDetector */
private $siteContentDetector;

public $steps = array(
'welcome' => 'Installation_Welcome',
'systemCheck' => 'Installation_SystemCheck',
Expand Down Expand Up @@ -393,29 +389,13 @@ public function trackingCode()

$javascriptGenerator = new TrackerCodeGenerator();
$jsTag = $javascriptGenerator->generate($idSite, Url::getCurrentUrlWithoutFileName());
$rawJsTag = TrackerCodeGenerator::stripTags($jsTag);

$showMatomoLinks = true;
Piwik::postEvent('SitesManager.showMatomoLinksInTrackingCodeEmail', array(&$showMatomoLinks));

$trackingUrl = trim(SettingsPiwik::getPiwikUrl(), '/') . '/' . $javascriptGenerator->getPhpTrackerEndpoint();

$this->siteContentDetector->detectContent([SiteContentDetector::ALL_CONTENT]);

$emailBody = $this->renderTemplateAs('@SitesManager/_trackingCodeEmail', array(
'jsTag' => $rawJsTag,
'showMatomoLinks' => $showMatomoLinks,
'trackingUrl' => $trackingUrl,
'idSite' => $idSite,
'gtmUsed' => $this->siteContentDetector->gtm,
'ga3Used' => $this->siteContentDetector->ga3,
'ga4Used' => $this->siteContentDetector->ga4,
'cloudflare' => $this->siteContentDetector->cloudflare,
'jsFramework' => $this->siteContentDetector->jsFramework,
'consentManagerName' => $this->siteContentDetector->consentManagerName,
'consentManagerUrl' => $this->siteContentDetector->consentManagerUrl,
'consentManagerIsConnected' => $this->siteContentDetector->isConnected
), $viewType = 'basic');

// Needs to be generated as super user, as API requests would otherwise fail
$emailBody = Access::doAsSuperUser(
function () use ($idSite) {
return SitesManager::renderTrackingCodeEmail($idSite);
}
);

// Load the Tracking code and help text from the SitesManager
$viewTrackingHelp = new \Piwik\View('@SitesManager/_displayJavascriptCode');
Expand All @@ -426,15 +406,6 @@ public function trackingCode()
$viewTrackingHelp->piwikUrl = Url::getCurrentUrlWithoutFileName();
$viewTrackingHelp->isInstall = true;

$viewTrackingHelp->gtmUsed = $this->siteContentDetector->gtm;
$viewTrackingHelp->ga3Used = $this->siteContentDetector->ga3;
$viewTrackingHelp->ga4Used = $this->siteContentDetector->ga4;
$viewTrackingHelp->cloudflare = $this->siteContentDetector->cloudflare;
$viewTrackingHelp->jsFramework = $this->siteContentDetector->jsFramework;
$viewTrackingHelp->consentManagerName = $this->siteContentDetector->consentManagerName;
$viewTrackingHelp->consentManagerUrl = $this->siteContentDetector->consentManagerUrl;
$viewTrackingHelp->consentManagerIsConnected = $this->siteContentDetector->isConnected;

$view->trackingHelp = $viewTrackingHelp->render();
$view->displaySiteName = $siteName;

Expand Down
3 changes: 1 addition & 2 deletions plugins/Installation/Installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Piwik\Piwik;
use Piwik\Plugins\Installation\Exception\DatabaseConnectionFailedException;
use Piwik\SettingsPiwik;
use Piwik\SiteContentDetector;
use Piwik\View as PiwikView;

/**
Expand Down Expand Up @@ -114,7 +113,7 @@ public function setControllerToLoad($newControllerName)

protected function getInstallationController()
{
return new $this->installationControllerName(new SiteContentDetector());
return new $this->installationControllerName();
}

/**
Expand Down
23 changes: 16 additions & 7 deletions plugins/PrivacyManager/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Piwik\Plugins\CustomJsTracker\File;
use Piwik\Plugins\LanguagesManager\LanguagesManager;
use Piwik\Plugins\LanguagesManager\API as APILanguagesManager;
use Piwik\Plugins\SitesManager\SiteContentDetection\ConsentManagerDetectionAbstract;
use Piwik\Plugins\SitesManager\SiteContentDetection\SiteContentDetectionAbstract;
use Piwik\SiteContentDetector;
use Piwik\Scheduler\Scheduler;
use Piwik\Tracker\TrackerCodeGenerator;
Expand Down Expand Up @@ -175,16 +177,23 @@ public function consent()

$view = new View('@PrivacyManager/askingForConsent');

$this->siteContentDetector->detectContent([SiteContentDetector::CONSENT_MANAGER]);
$this->siteContentDetector->detectContent([SiteContentDetectionAbstract::TYPE_CONSENT_MANAGER]);
$consentManager = $this->siteContentDetector->getDetectsByType(SiteContentDetectionAbstract::TYPE_CONSENT_MANAGER);
$view->consentManagerName = null;
if ($this->siteContentDetector->consentManagerId) {
$view->consentManagerName = $this->siteContentDetector->consentManagerName;
$view->consentManagerUrl = $this->siteContentDetector->consentManagerUrl;
$view->consentManagerIsConnected = $this->siteContentDetector->isConnected;
if (!empty($consentManager)) {
$consentManager = $this->siteContentDetector->getSiteContentDetectionById(reset($consentManager));
if ($consentManager instanceof ConsentManagerDetectionAbstract) {
$view->consentManagerName = $consentManager::getName();
$view->consentManagerUrl = $consentManager::getInstructionUrl();
$view->consentManagerIsConnected = in_array(
$consentManager::getId(),
$this->siteContentDetector->connectedConsentManagers
);
}
}

$consentManagers = SiteContentDetector::getConsentManagerDefinitions();
$knownConsentManagers = array_combine(array_column($consentManagers, 'name'), array_column($consentManagers, 'url'));
$consentManagers = SiteContentDetector::getKnownConsentManagers();
$knownConsentManagers = array_combine(array_column($consentManagers, 'name'), array_column($consentManagers, 'instructionUrl'));

$view->knownConsentManagers = $knownConsentManagers;
$this->setBasicVariablesView($view);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 33 additions & 1 deletion plugins/SitesManager/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Piwik\Piwik;
use Piwik\Plugin\SettingsProvider;
use Piwik\Plugins\CorePluginsAdmin\SettingsMetadata;
use Piwik\Plugins\SitesManager\SiteContentDetection\ConsentManagerDetectionAbstract;
use Piwik\Plugins\SitesManager\SiteContentDetection\SiteContentDetectionAbstract;
use Piwik\Plugins\WebsiteMeasurable\Settings\Urls;
use Piwik\ProxyHttp;
use Piwik\Scheduler\Scheduler;
Expand All @@ -32,6 +34,7 @@
use Piwik\SettingsPiwik;
use Piwik\SettingsServer;
use Piwik\Site;
use Piwik\SiteContentDetector;
use Piwik\Tracker\Cache;
use Piwik\Tracker\TrackerCodeGenerator;
use Piwik\Translation\Translator;
Expand Down Expand Up @@ -84,11 +87,15 @@ class API extends \Piwik\Plugin\API

private $timezoneNameCache = [];

public function __construct(SettingsProvider $provider, SettingsMetadata $settingsMetadata, Translator $translator)
/** @var SiteContentDetector */
private $siteContentDetector;

public function __construct(SettingsProvider $provider, SettingsMetadata $settingsMetadata, Translator $translator, SiteContentDetector $siteContentDetector)
{
$this->settingsProvider = $provider;
$this->settingsMetadata = $settingsMetadata;
$this->translator = $translator;
$this->siteContentDetector = $siteContentDetector;
}

/**
Expand Down Expand Up @@ -1764,4 +1771,29 @@ public function getNumWebsitesToDisplayPerPage()

return SettingsPiwik::getWebsitesCountToDisplay();
}


/**
* Detect consent manager details for a site
*
* @internal
* @unsanitized
*/
public function detectConsentManager(int $idSite, int $timeOut = 60): ?array
{
Piwik::checkUserHasViewAccess($idSite);

$this->siteContentDetector->detectContent([SiteContentDetectionAbstract::TYPE_CONSENT_MANAGER], $idSite, null, $timeOut);
$consentManagers = $this->siteContentDetector->getDetectsByType(SiteContentDetectionAbstract::TYPE_CONSENT_MANAGER);
if (!empty($consentManagers)) {
/** @var ConsentManagerDetectionAbstract $consentManager */
$consentManager = $this->siteContentDetector->getSiteContentDetectionById(reset($consentManagers));
return ['name' => $consentManager::getName(),
'url' => $consentManager::getInstructionUrl(),
'isConnected' => in_array($consentManager::getId(), $this->siteContentDetector->connectedConsentManagers)
];
}

return null;
}
}
Loading

0 comments on commit 77b4d09

Please sign in to comment.