From baf3010cc0b186b56d5d32483aa184eb53ae3e14 Mon Sep 17 00:00:00 2001 From: Altamash Shaikh Date: Fri, 19 Jul 2024 10:29:16 +0530 Subject: [PATCH 1/6] Add code to make MTM as recommended install by default instead of JS code, #PG-3586 --- plugins/SitesManager/Controller.php | 17 +++++++++++++---- .../SiteContentDetection/MatomoTagManager.php | 9 +++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/plugins/SitesManager/Controller.php b/plugins/SitesManager/Controller.php index 077b8669d53..84a96725e14 100644 --- a/plugins/SitesManager/Controller.php +++ b/plugins/SitesManager/Controller.php @@ -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; @@ -250,7 +251,7 @@ public function getTrackingMethodsForSite() ]; $recommendedMethod = null; - $matomoIndex = null; + $matomoIndex = $matomoTagManagerIndex = null; foreach ($trackingMethods as $index => $tab) { // Note: We recommend the first method that is recommended (after sorting by priority) @@ -263,12 +264,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(); diff --git a/plugins/SitesManager/SiteContentDetection/MatomoTagManager.php b/plugins/SitesManager/SiteContentDetection/MatomoTagManager.php index a240bf2bda5..6fdc9dbcec4 100644 --- a/plugins/SitesManager/SiteContentDetection/MatomoTagManager.php +++ b/plugins/SitesManager/SiteContentDetection/MatomoTagManager.php @@ -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 '

' . Piwik::translate('SitesManager_SiteWithoutDataMatomoTagManager') . '

From 02572c68392eb5ef6a5989a3527a7436acc85551 Mon Sep 17 00:00:00 2001 From: Altamash Shaikh Date: Fri, 19 Jul 2024 11:02:15 +0530 Subject: [PATCH 2/6] Fixes for UI tests --- .../UI/expected-screenshots/EmptySite_emptySiteDashboard.png | 4 ++-- tests/UI/specs/EmptySite_spec.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/UI/expected-screenshots/EmptySite_emptySiteDashboard.png b/tests/UI/expected-screenshots/EmptySite_emptySiteDashboard.png index 8c2cc090322..e47bb8bae05 100644 --- a/tests/UI/expected-screenshots/EmptySite_emptySiteDashboard.png +++ b/tests/UI/expected-screenshots/EmptySite_emptySiteDashboard.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e028f33300163b0bafb8db4f2f8189356e6d6e3d4d6730b653f4fab60f845431 -size 100042 +oid sha256:c61b8d454f1686e55acd009fae65440ae9feb87fe6344034f089573160c2525e +size 101010 diff --git a/tests/UI/specs/EmptySite_spec.js b/tests/UI/specs/EmptySite_spec.js index 6344ad5d10a..fe33e10f2bc 100644 --- a/tests/UI/specs/EmptySite_spec.js +++ b/tests/UI/specs/EmptySite_spec.js @@ -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()); From 030c759b6ea148ab3393d082e477c121a69fde1e Mon Sep 17 00:00:00 2001 From: Altamash Shaikh Date: Fri, 19 Jul 2024 11:40:32 +0530 Subject: [PATCH 3/6] fixes for UI tests --- .../EmptySite_trackingCode.png | 4 ++-- tests/UI/specs/EmptySite_spec.js | 16 ---------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/tests/UI/expected-screenshots/EmptySite_trackingCode.png b/tests/UI/expected-screenshots/EmptySite_trackingCode.png index 243532ea708..625bda64317 100644 --- a/tests/UI/expected-screenshots/EmptySite_trackingCode.png +++ b/tests/UI/expected-screenshots/EmptySite_trackingCode.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:af0eb2652c8198650fcb15eccfe7f2226245e8e78a40ec7adfe9445acb1f4966 -size 112665 +oid sha256:ca6c9bcf9dd26e735d11ad27dd412fbf67f5645b0d35f6b5ace55bac50b133db +size 105195 diff --git a/tests/UI/specs/EmptySite_spec.js b/tests/UI/specs/EmptySite_spec.js index fe33e10f2bc..f1171a42ecb 100644 --- a/tests/UI/specs/EmptySite_spec.js +++ b/tests/UI/specs/EmptySite_spec.js @@ -61,22 +61,6 @@ describe("EmptySite", function () { expect(await pageElement.screenshot()).to.matchImage('trackingCode'); }); - it('should show the advanced tracking options when clicked', async function () { - await page.evaluate(() => $('.advance-option a').click()); - - // wait till checkbox isn't disabled anymore, which means loading has finished. - await page.waitForFunction(() => !$('#javascript-tracking-all-subdomains').is(':disabled')); - - const pageElement = await page.$('.page'); - expect(await pageElement.screenshot()).to.matchImage('showAdvancedTrackingOptions'); - }); - - it('should hide the advanced tracking options when clicked', async function () { - await page.evaluate(() => $('.advance-option a').click()); - - await page.waitForSelector('#javascript-advanced-options', {hidden: true, timeout: 1000}); - }); - it('should show SPA/PWA details when clicked', async function () { await page.click('#start-tracking-back'); await page.evaluate(() => $('#start-tracking-method-list a[href="#spapwa"]')[0].click()); From 2e1b2ad624a6380cf205f7dfd7ceb1b1ee4a27bd Mon Sep 17 00:00:00 2001 From: Altamash Shaikh Date: Fri, 19 Jul 2024 15:18:46 +0530 Subject: [PATCH 4/6] Apply PR feedback and add UI tests --- plugins/SitesManager/Controller.php | 3 ++- tests/UI/specs/EmptySite_spec.js | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/plugins/SitesManager/Controller.php b/plugins/SitesManager/Controller.php index 84a96725e14..73bedbcbc58 100644 --- a/plugins/SitesManager/Controller.php +++ b/plugins/SitesManager/Controller.php @@ -251,7 +251,8 @@ public function getTrackingMethodsForSite() ]; $recommendedMethod = null; - $matomoIndex = $matomoTagManagerIndex = null; + $matomoIndex = null; + $matomoTagManagerIndex = null; foreach ($trackingMethods as $index => $tab) { // Note: We recommend the first method that is recommended (after sorting by priority) diff --git a/tests/UI/specs/EmptySite_spec.js b/tests/UI/specs/EmptySite_spec.js index f1171a42ecb..4b293eea3c4 100644 --- a/tests/UI/specs/EmptySite_spec.js +++ b/tests/UI/specs/EmptySite_spec.js @@ -61,6 +61,32 @@ 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 () { + await page.evaluate(() => $('.advance-option a').click()); + + // wait till checkbox isn't disabled anymore, which means loading has finished. + await page.waitForFunction(() => !$('#javascript-tracking-all-subdomains').is(':disabled')); + + const pageElement = await page.$('.page'); + expect(await pageElement.screenshot()).to.matchImage('showAdvancedTrackingOptions'); + }); + + it('should hide the advanced tracking options when clicked', async function () { + await page.evaluate(() => $('.advance-option a').click()); + + await page.waitForSelector('#javascript-advanced-options', {hidden: true, timeout: 1000}); + }); + it('should show SPA/PWA details when clicked', async function () { await page.click('#start-tracking-back'); await page.evaluate(() => $('#start-tracking-method-list a[href="#spapwa"]')[0].click()); From 215b5a0bc2735ddd2608fed2f3cabf03bc48d58a Mon Sep 17 00:00:00 2001 From: Altamash Shaikh Date: Fri, 19 Jul 2024 15:52:19 +0530 Subject: [PATCH 5/6] Add new Ui screenshot --- tests/UI/expected-screenshots/EmptySite_trackingCodeJS.png | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tests/UI/expected-screenshots/EmptySite_trackingCodeJS.png diff --git a/tests/UI/expected-screenshots/EmptySite_trackingCodeJS.png b/tests/UI/expected-screenshots/EmptySite_trackingCodeJS.png new file mode 100644 index 00000000000..243532ea708 --- /dev/null +++ b/tests/UI/expected-screenshots/EmptySite_trackingCodeJS.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af0eb2652c8198650fcb15eccfe7f2226245e8e78a40ec7adfe9445acb1f4966 +size 112665 From 9d84955d25bedf11b423d0667ec32c36077ca4b4 Mon Sep 17 00:00:00 2001 From: Altamash Shaikh Date: Fri, 19 Jul 2024 18:30:22 +0530 Subject: [PATCH 6/6] Fixes UI test --- tests/UI/specs/EmptySite_spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/UI/specs/EmptySite_spec.js b/tests/UI/specs/EmptySite_spec.js index 4b293eea3c4..b00c0019732 100644 --- a/tests/UI/specs/EmptySite_spec.js +++ b/tests/UI/specs/EmptySite_spec.js @@ -186,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());