Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code to make MTM as recommended install by default instead of JS code #22420

Draft
wants to merge 7 commits into
base: 5.x-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions plugins/SitesManager/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugins\SitesManager\SiteContentDetection\Matomo;
use Piwik\Plugins\SitesManager\SiteContentDetection\MatomoTagManager;
use Piwik\Plugins\SitesManager\SiteContentDetection\SiteContentDetectionAbstract;
use Piwik\Plugins\SitesManager\SiteContentDetection\WordPress;
use Piwik\SiteContentDetector;
Expand Down Expand Up @@ -251,6 +252,7 @@ public function getTrackingMethodsForSite()

$recommendedMethod = null;
$matomoIndex = null;
$matomoTagManagerIndex = null;

foreach ($trackingMethods as $index => $tab) {
// Note: We recommend the first method that is recommended (after sorting by priority)
Expand All @@ -263,12 +265,20 @@ public function getTrackingMethodsForSite()
if ($tab['id'] === Matomo::getId()) {
$matomoIndex = $index;
}

if ($tab['id'] === MatomoTagManager::getId()) {
$matomoTagManagerIndex = $index;
}
}

// fall back to javascript code recommendation if nothing was detected
// fall back to TagManager if plugin is active else fallback to javascript code recommendation if nothing was detected
if (null === $recommendedMethod && null !== $matomoIndex) {
$recommendedMethod = $trackingMethods[$matomoIndex];
unset($trackingMethods[$matomoIndex]);
$index = $matomoIndex;
if (null !== $matomoTagManagerIndex && Manager::getInstance()->isPluginActivated('TagManager')) {
$index = $matomoTagManagerIndex;
}
$recommendedMethod = $trackingMethods[$index];
unset($trackingMethods[$index]);
}

Json::sendHeaderJSON();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function isDetected(?string $data = null, ?array $headers = null): bool
return false;
}

public function getRecommendationDetails(SiteContentDetector $detector): array
{
$details = parent::getRecommendationDetails($detector);
if (!$detector->wasDetected(self::getId())) {
$details['text'] = Piwik::translate('SitesManager_SetupMatomoTracker');
}
return $details;
}

public function renderInstructionsTab(SiteContentDetector $detector): string
{
return '<h3>' . Piwik::translate('SitesManager_SiteWithoutDataMatomoTagManager') . '</h3>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/UI/expected-screenshots/EmptySite_trackingCode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/UI/expected-screenshots/EmptySite_trackingCodeJS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions tests/UI/specs/EmptySite_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe("EmptySite", function () {
});

it('should show the tracking code when selected', async function () {
await page.evaluate(() => $('#start-tracking-detection a[href="#matomo"]')[0].click());
await page.evaluate(() => $('#start-tracking-detection a[href="#matomotagmanager"]')[0].click());

// wait till url check field is filled with data, which means loading has finished.
await page.waitForFunction(() => $('#baseUrl').val());
Expand All @@ -61,6 +61,16 @@ describe("EmptySite", function () {
expect(await pageElement.screenshot()).to.matchImage('trackingCode');
});

it('should show the JS tracking code', async function () {
await page.click('#start-tracking-back');
await page.evaluate(() => $('#start-tracking-method-list a[href="#matomo"]')[0].click());

await makeTrackingCodeStatic();

const pageElement = await page.$('.page');
expect(await pageElement.screenshot()).to.matchImage('trackingCodeJS');
});

it('should show the advanced tracking options when clicked', async function () {
AltamashShaikh marked this conversation as resolved.
Show resolved Hide resolved
await page.evaluate(() => $('.advance-option a').click());

Expand Down Expand Up @@ -176,7 +186,7 @@ describe("EmptySite", function () {
await page.goto(urlToTest);
await page.waitForSelector('#start-tracking-method-list'); // wait till list is shown

await page.evaluate(() => $('#start-tracking-detection a[href="#matomo"]')[0].click());
await page.evaluate(() => $('#start-tracking-method-list a[href="#matomo"]')[0].click());

// wait till url check field is filled with data, which means loading has finished.
await page.waitForFunction(() => $('#baseUrl').val());
Expand Down
Loading