Skip to content

Commit

Permalink
Merge pull request #538 from matomo-org/PG-3619-remove-ga3-import
Browse files Browse the repository at this point in the history
Removed option to schedule a GA3 import, #PG-3619
  • Loading branch information
AltamashShaikh authored Jul 22, 2024
2 parents 25a08da + a6246cd commit ccd42ee
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 1,279 deletions.
2 changes: 2 additions & 0 deletions Commands/ImportReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ protected function configure()
*/
protected function doExecute() : int
{
$this->getOutput()->writeln("GA3 Import disabled");
return self::FAILURE;
try {
return $this->executeImpl();
} catch (ImportWasCancelledException $ex) {
Expand Down
69 changes: 2 additions & 67 deletions Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,68 +247,7 @@ public function changeImportEndDate()
Notification\Manager::notify('GoogleAnalyticsImporter_changeImportEndDate_failure', $notification);
}
}
public function startImport()
{
Piwik::checkUserHasSuperUserAccess();
$this->checkTokenInUrl();
Json::sendHeaderJSON();
try {
Nonce::checkNonce('GoogleAnalyticsImporter.startImportNonce', Common::getRequestVar('nonce'));
$startDate = trim(Common::getRequestVar('startDate', ''));
if (!empty($startDate)) {
$startDate = Date::factory($startDate . ' 00:00:00');
}
$endDate = trim(Common::getRequestVar('endDate', ''));
$inputEndDate = StaticContainer::get(EndDate::class);
$endDate = $inputEndDate->limitMaxEndDateIfNeeded($endDate);
if (!empty($endDate)) {
$endDate = Date::factory($endDate)->getStartOfDay();
}
// set credentials in google client
$googleAuth = StaticContainer::get(Authorization::class);
$googleAuth->getConfiguredClient();
/** @var Importer $importer */
$importer = StaticContainer::get(\Piwik\Plugins\GoogleAnalyticsImporter\Importer::class);
$propertyId = trim(Common::getRequestVar('propertyId'));
$viewId = trim(Common::getRequestVar('viewId'));
$accountId = trim(Common::getRequestVar('accountId', \false));
$account = $accountId ?: ImportReports::guessAccountFromProperty($propertyId);
$isMobileApp = Common::getRequestVar('isMobileApp', 0, 'int') == 1;
$timezone = trim(Common::getRequestVar('timezone', '', 'string'));
$extraCustomDimensions = Common::getRequestVar('extraCustomDimensions', [], $type = 'array');
$isVerboseLoggingEnabled = Common::getRequestVar('isVerboseLoggingEnabled', 0, $type = 'int') == 1;
$forceCustomDimensionSlotCheck = Common::getRequestVar('forceCustomDimensionSlotCheck', 1, $type = 'int') == 1;
$idSite = $importer->makeSite($account, $propertyId, $viewId, $timezone, $isMobileApp ? Type::ID : \Piwik\Plugins\WebsiteMeasurable\Type::ID, $extraCustomDimensions, $forceCustomDimensionSlotCheck);
try {
if (empty($idSite)) {
throw new \Exception("Unable to import site entity.");
// sanity check
}
/** @var ImportStatus $importStatus */
$importStatus = StaticContainer::get(\Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::class);
if (!empty($startDate) || !empty($endDate)) {
// we set the last imported date to one day before the start date
$importStatus->setImportDateRange($idSite, $startDate ?: null, $endDate ?: null);
}
if ($isVerboseLoggingEnabled) {
$importStatus->setIsVerboseLoggingEnabled($idSite, $isVerboseLoggingEnabled);
}
// start import now since the scheduled task may not run until tomorrow
\Piwik\Plugins\GoogleAnalyticsImporter\Tasks::startImport($importStatus->getImportStatus($idSite));
} catch (\Exception $ex) {
$importStatus->erroredImport($idSite, $ex->getMessage());
throw $ex;
}
echo json_encode(['result' => 'ok']);
} catch (\Exception $ex) {
$this->logException($ex, __FUNCTION__);
$notification = new Notification($this->getNotificationExceptionText($ex));
$notification->type = Notification::TYPE_TRANSIENT;
$notification->context = Notification::CONTEXT_ERROR;
$notification->title = Piwik::translate('General_Error');
Notification\Manager::notify('GoogleAnalyticsImporter_startImport_failure', $notification);
}
}

public function startImportGA4()
{
Piwik::checkUserHasSuperUserAccess();
Expand Down Expand Up @@ -393,8 +332,6 @@ public function resumeImport()
}
if ($isGA4) {
\Piwik\Plugins\GoogleAnalyticsImporter\Tasks::startImportGA4($status);
} else {
\Piwik\Plugins\GoogleAnalyticsImporter\Tasks::startImport($status);
}
echo json_encode(['result' => 'ok']);
} catch (\Exception $ex) {
Expand Down Expand Up @@ -437,8 +374,6 @@ public function scheduleReImport()
// start import now since the scheduled task may not run until tomorrow
if ($isGA4) {
\Piwik\Plugins\GoogleAnalyticsImporter\Tasks::startImportGA4($importStatus->getImportStatus($idSite));
} else {
\Piwik\Plugins\GoogleAnalyticsImporter\Tasks::startImport($importStatus->getImportStatus($idSite));
}
echo json_encode(['result' => 'ok']);
} catch (\Exception $ex) {
Expand Down Expand Up @@ -492,7 +427,7 @@ public function displayConfigureImportNotification()
}
if (empty($status)) {
$siteContentDetector = new \Piwik\SiteContentDetector();
$siteContentDetector->detectContent([GoogleAnalytics3::getId(), GoogleAnalytics4::getId()], $currentIdSite);
$siteContentDetector->detectContent([GoogleAnalytics4::getId()], $currentIdSite);
if ($siteContentDetector->wasDetected(GoogleAnalytics3::getId()) || $siteContentDetector->wasDetected(GoogleAnalytics4::getId())) {
$showNotification = \true;
$settingsUrl = SettingsPiwik::getPiwikUrl() . 'index.php?' . Url::getQueryStringFromParameters(['idSite' => $currentIdSite, 'module' => 'GoogleAnalyticsImporter', 'action' => 'index']);
Expand Down
2 changes: 1 addition & 1 deletion GoogleAnalyticsImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function getClientSideTranslationKeys(&$translationKeys)
$translationKeys[] = 'GoogleAnalyticsImporter_GAImportNoDataScreenStep03';
$translationKeys[] = 'GoogleAnalyticsImporter_GAImportNoDataScreenStep04';
$translationKeys[] = 'GoogleAnalyticsImporter_GAImportNoDataScreenStep05';
$translationKeys[] = 'GoogleAnalyticsImporter_GAImportNoDataScreenStep06';
$translationKeys[] = 'GoogleAnalyticsImporter_GAImportNoDataScreenStep06GA4';
$translationKeys[] = 'GoogleAnalyticsImporter_GAImportNoDataScreenStep07';
$translationKeys[] = 'GoogleAnalyticsImporter_GAImportNoDataScreenStep07Note';
$translationKeys[] = 'GoogleAnalyticsImporter_GAImportNoDataScreenStep08';
Expand Down
2 changes: 1 addition & 1 deletion SiteContentDetection/GoogleAnalyticsImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function renderInstructionsTab(SiteContentDetector $detector) : string
$view->auth_nonce = Nonce::getNonce('gaimport.auth', 1200);
$view->isConnectAccountsActivated = $isConnectAccountsPluginActivated;
$view->strategy = $isConnectAccountsPluginActivated && GoogleConnect::isStrategyActive() ? GoogleConnect::getStrategyName() : 'CUSTOM';
$view->isGA3 = $detector->wasDetected(GoogleAnalytics3::getId());
$view->isGA3 = false;
$view->configureConnectionProps = \Piwik\Plugins\GoogleAnalyticsImporter\GoogleAnalyticsImporter::getConfigureConnectProps($nonce);
$view->extensions = Controller::getComponentExtensions(\true);
$view->hasClientConfiguration = $authorization->hasClientConfiguration();
Expand Down
2 changes: 1 addition & 1 deletion TESTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Testing
## Testing (Deprecated)

This document outlines how to run the automated tests for the Google Analytics Importer plugin - GA3(Universal Analytics).

Expand Down
40 changes: 0 additions & 40 deletions Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public function resumeScheduledImports()
}
if (!empty($status['isGA4'])) {
self::startImportGA4($status);
} else {
self::startImport($status);
}
}
$logger->info('Done scheduling imports.');
Expand All @@ -74,44 +72,6 @@ public function archiveImportedReports()
}
$logger->info('Done running archive commands.');
}
public static function startImport($status)
{
if (\Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::isImportRunning($status)) {
return;
}
$logToSingleFile = StaticContainer::get('GoogleAnalyticsImporter.logToSingleFile');
$idSite = $status['idSite'];
$isVerboseLoggingEnabled = !empty($status['is_verbose_logging_enabled']);
$hostname = SettingsPiwik::getPiwikInstanceId();
$importLogFile = self::getImportLogFile($idSite, $hostname, $logToSingleFile);
if (!is_writable($importLogFile) && !is_writable(dirname($importLogFile))) {
$importLogFile = '/dev/null';
}
$cliPhp = new CliPhp();
$phpBinary = $cliPhp->findPhpBinary() ?: 'php';
$pathToConsole = '/console';
if (defined('PIWIK_TEST_MODE')) {
$pathToConsole = '/tests/PHPUnit/proxy/console';
}
$nohup = self::getNohupCommandIfPresent();
$command = "{$nohup} {$phpBinary} " . PIWIK_INCLUDE_PATH . $pathToConsole . ' ';
if (!empty($hostname)) {
$command .= '--matomo-domain=' . escapeshellarg($hostname) . ' ';
}
$command .= 'googleanalyticsimporter:import-reports --idsite=' . (int) $idSite;
if ($isVerboseLoggingEnabled) {
$command .= ' -vvv';
}
if ($logToSingleFile || !$isVerboseLoggingEnabled) {
$command .= ' >> ';
} else {
$command .= ' > ';
}
$command .= $importLogFile . ' 2>&1 &';
$logger = StaticContainer::get(LoggerInterface::class);
$logger->debug("Import command: {command}", ['command' => $command]);
static::exec($shouldUsePassthru = \false, $command);
}
public static function startImportGA4($status)
{
if (\Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::isImportRunning($status)) {
Expand Down
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"GAImportNoDataScreenStep04": "Navigate to the %1$sGoogle Analytics Import admin page%2$s. %3$sLearn more%4$s.",
"GAImportNoDataScreenStep05": "Select the Import type.",
"GAImportNoDataScreenStep06": "Fill out the Schedule an Import form.",
"GAImportNoDataScreenStep06GA4": "Fill out the Schedule an Import for Google Analytics 4 (GA4) form.",
"GAImportNoDataScreenStep07": "Click \"%1$s\".",
"GAImportNoDataScreenStep07Note": "%1$sNote:%2$s Once you click \"%3$s\", Matomo will automatically create a new Matomo website for each selected property.",
"GAImportNoDataScreenStep08": "Importing data takes time, %1$sclick here to monitor the status of your data import%2$s.",
Expand Down
42 changes: 1 addition & 41 deletions tests/Integration/TasksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,47 +70,7 @@ public function test_resumeScheduledImports_runANormalStatusCommandCorrectly()
Option::set(ImportStatus::OPTION_NAME_PREFIX . 1, json_encode(['idSite' => 1, 'status' => ImportStatus::STATUS_STARTED]));
$tasks = new \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\TasksWithMockExec();
$tasks->resumeScheduledImports();
$this->assertEquals([
// nohup /home/travis/.phpenv/versions/7.2.27/bin/php -q /home/travis/build/matomo-org/plugin-GoogleAnalyticsImporter/matomo/tests/PHPUnit/proxy/console --matomo-domain='localhost' googleanalyticsimporter:import-reports --idsite=1 >> /home/travis/build/matomo-org/plugin-GoogleAnalyticsImporter/matomo/tmp/logs/gaimportlog.1.localhost.log 2>&1 &
[\false, 'nohup ' . $this->getPhpBinary() . ' ' . PIWIK_INCLUDE_PATH . "/tests/PHPUnit/proxy/console{$this->getCommandHostOption()} googleanalyticsimporter:import-reports --idsite=1 >> " . $this->tmpPath . '/logs/gaimportlog.1.' . SettingsPiwik::getPiwikInstanceId() . '.log 2>&1 &'],
], \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\TasksWithMockExec::$commandsRun);
}
public function test_resumeScheduledImports_runAStatusWithVerboseLoggingCorrectly()
{
Option::set(ImportStatus::OPTION_NAME_PREFIX . 1, json_encode(['idSite' => 1, 'is_verbose_logging_enabled' => 1, 'status' => ImportStatus::STATUS_STARTED]));
$tasks = new \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\TasksWithMockExec();
$tasks->resumeScheduledImports();
$this->assertEquals([[\false, 'nohup ' . $this->getPhpBinary() . ' ' . PIWIK_INCLUDE_PATH . "/tests/PHPUnit/proxy/console{$this->getCommandHostOption()} googleanalyticsimporter:import-reports --idsite=1 -vvv > " . $this->tmpPath . '/logs/gaimportlog.1.' . SettingsPiwik::getPiwikInstanceId() . '.log 2>&1 &']], \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\TasksWithMockExec::$commandsRun);
}
public function test_resumeScheduledImports_runAStatusWithVerboseLoggingCorrectlyWithInstanceIdUpdated()
{
$oldValue = GeneralConfig::getConfigValue('instance_id');
GeneralConfig::setConfigValue('instance_id', 'touch /tmp/success');
Option::set(ImportStatus::OPTION_NAME_PREFIX . 1, json_encode(['idSite' => 1, 'is_verbose_logging_enabled' => 1, 'status' => ImportStatus::STATUS_STARTED]));
$tasks = new \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\TasksWithMockExec();
$tasks->resumeScheduledImports();
$expected = 'nohup ' . $this->getPhpBinary() . ' ' . PIWIK_INCLUDE_PATH . "/tests/PHPUnit/proxy/console{$this->getCommandHostOption()} googleanalyticsimporter:import-reports --idsite=1 -vvv > /dev/null 2>&1 &";
//since instance id is now sanitized in Matomo 5.x
if (version_compare(Version::VERSION, '5.0.0-b1', '>=')) {
$expected = 'nohup ' . $this->getPhpBinary() . ' ' . PIWIK_INCLUDE_PATH . "/tests/PHPUnit/proxy/console{$this->getCommandHostOption()} googleanalyticsimporter:import-reports --idsite=1 -vvv > " . PIWIK_INCLUDE_PATH . '/tmp/logs/gaimportlog.1.touchtmpsuccess.log' . " 2>&1 &";
}
$this->assertEquals([[\false, $expected]], \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\TasksWithMockExec::$commandsRun);
GeneralConfig::setConfigValue('instance_id', $oldValue);
}
public function test_resumeScheduledImports_runAStatusWithVerboseLoggingCorrectlyWithInstanceIdUpdated_1()
{
$oldValue = GeneralConfig::getConfigValue('instance_id');
GeneralConfig::setConfigValue('instance_id', 'test; rm -rf .');
Option::set(ImportStatus::OPTION_NAME_PREFIX . 1, json_encode(['idSite' => 1, 'is_verbose_logging_enabled' => 1, 'status' => ImportStatus::STATUS_STARTED]));
$tasks = new \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\TasksWithMockExec();
$tasks->resumeScheduledImports();
$expected = 'nohup ' . $this->getPhpBinary() . ' ' . PIWIK_INCLUDE_PATH . "/tests/PHPUnit/proxy/console{$this->getCommandHostOption()} googleanalyticsimporter:import-reports --idsite=1 -vvv > " . $this->tmpPath . '/logs/gaimportlog.1.test\\; rm -rf ..log 2>&1 &';
//since instance id is now sanitized in Matomo 5.x
if (version_compare(Version::VERSION, '5.0.0-b1', '>=')) {
$expected = 'nohup ' . $this->getPhpBinary() . ' ' . PIWIK_INCLUDE_PATH . "/tests/PHPUnit/proxy/console{$this->getCommandHostOption()} googleanalyticsimporter:import-reports --idsite=1 -vvv > " . $this->tmpPath . '/logs/gaimportlog.1.testrm-rf..log 2>&1 &';
}
$this->assertEquals([[\false, $expected]], \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\TasksWithMockExec::$commandsRun);
GeneralConfig::setConfigValue('instance_id', $oldValue);
$this->assertEquals([], \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\TasksWithMockExec::$commandsRun);
}
public function test_archiveImportedReports_shouldSkipBrokenStatusEntries_ImportsThatHaveNotImportedAnything_OrLastArchivedDateIsEqualOrGreaterToLastImportedDate()
{
Expand Down
Loading

0 comments on commit ccd42ee

Please sign in to comment.