Skip to content

Commit

Permalink
Merge pull request #544 from matomo-org/PG-3661-add-gaimporter-activi…
Browse files Browse the repository at this point in the history
…ties

Adding new events to be tracked by ActivityLog
  • Loading branch information
snake14 authored Aug 8, 2024
2 parents ef2e6d4 + feabaa7 commit 478519f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Commands/ImportGA4Reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugin\ConsoleCommand;
use Piwik\Plugin\Manager;
use Piwik\Plugins\GoogleAnalyticsImporter\CannotProcessImportException;
Expand Down Expand Up @@ -172,6 +173,15 @@ protected function executeImpl() : int
$output->writeln(LogToSingleFileProcessor::$cliOutputPrefix . "Failed to import property entities, aborting.");
return self::FAILURE;
}

// Record the correct activity for the command. It's either a new import or a resumed import
$status = $importStatus->getImportStatus($idSite);
if ($createdSiteInCommand) {
Piwik::postEvent('GoogleAnalyticsImporter.startImportGA4.end', [$status]);
} else {
Piwik::postEvent('GoogleAnalyticsImporter.resumeImport.end', [$status]);
}

$dateRangesToReImport = empty($status['reimport_ranges']) ? [] : $status['reimport_ranges'];
$dateRangesToReImport = array_map(function ($d) {
return [Date::factory($d[0]), Date::factory($d[1])];
Expand Down
14 changes: 13 additions & 1 deletion Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ public function deleteClientCredentials()
Nonce::checkNonce('GoogleAnalyticsImporter.deleteGoogleClientConfig', Common::getRequestVar('config_nonce'));
/** @var Authorization $authorization */
$authorization = StaticContainer::get(Authorization::class);
$config = $authorization->getClientConfiguration();
$authorization->deleteClientConfiguration();
Piwik::postEvent('GoogleAnalyticsImporter.removeClientConfiguration.end', [$config]);
// Redirect to index so that will be the URL and not the delete URL
Url::redirectToUrl(Url::getCurrentUrlWithoutQueryString() . Url::getCurrentQueryStringWithParametersModified(['action' => 'index', 'code' => null, 'scope' => null, 'state' => null]));
}
Expand Down Expand Up @@ -155,6 +157,8 @@ public function processAuthCode()
$authorization = StaticContainer::get(Authorization::class);
$client = $authorization->getConfiguredClient();
$authorization->saveAccessToken($oauthCode, $client);
$config = $authorization->getClientConfiguration();
Piwik::postEvent('GoogleAnalyticsImporter.authorizedClientConfiguration.end', [$config]);
} catch (\Exception $e) {
return $this->index($this->getNotificationExceptionText($e));
}
Expand Down Expand Up @@ -188,6 +192,7 @@ public function configureClient()
}
$authorization->validateConfig($config);
$authorization->saveConfig($config);
Piwik::postEvent('GoogleAnalyticsImporter.addClientConfiguration.end', [$config]);
} catch (\Exception $ex) {
$errorMessage = $this->getNotificationExceptionText($ex);
$errorMessage = substr($errorMessage, 0, 1024);
Expand All @@ -211,8 +216,10 @@ public function deleteImportStatus()
$idSite = Common::getRequestVar('idSite', null, 'int');
/** @var ImportStatus $importStatus */
$importStatus = StaticContainer::get(\Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::class);
$status = $importStatus->getImportStatus($idSite);
$importStatus->deleteStatus($idSite);
echo json_encode(['result' => 'ok']);
Piwik::postEvent('GoogleAnalyticsImporter.deleteImportStatus.end', [$status]);
} catch (\Exception $ex) {
$this->logException($ex, __FUNCTION__);
$notification = new Notification($this->getNotificationExceptionText($ex));
Expand Down Expand Up @@ -295,7 +302,9 @@ public function startImportGA4()
$importStatus->setIsVerboseLoggingEnabled($idSite, $isVerboseLoggingEnabled);
}
// start import now since the scheduled task may not run until tomorrow
\Piwik\Plugins\GoogleAnalyticsImporter\Tasks::startImportGA4($importStatus->getImportStatus($idSite));
$status = $importStatus->getImportStatus($idSite);
\Piwik\Plugins\GoogleAnalyticsImporter\Tasks::startImportGA4($status);
Piwik::postEvent('GoogleAnalyticsImporter.startImportGA4.end', [$status]);
} catch (\Exception $ex) {
$importStatus->erroredImport($idSite, $ex->getMessage());
throw $ex;
Expand Down Expand Up @@ -334,6 +343,7 @@ public function resumeImport()
\Piwik\Plugins\GoogleAnalyticsImporter\Tasks::startImportGA4($status);
}
echo json_encode(['result' => 'ok']);
Piwik::postEvent('GoogleAnalyticsImporter.resumeImport.end', [$status]);
} catch (\Exception $ex) {
$this->logException($ex, __FUNCTION__);
$notification = new Notification($this->getNotificationExceptionText($ex));
Expand Down Expand Up @@ -376,6 +386,8 @@ public function scheduleReImport()
\Piwik\Plugins\GoogleAnalyticsImporter\Tasks::startImportGA4($importStatus->getImportStatus($idSite));
}
echo json_encode(['result' => 'ok']);
$status = $importStatus->getImportStatus($idSite);
Piwik::postEvent('GoogleAnalyticsImporter.scheduleReImport.end', [$status]);
} catch (\Exception $ex) {
$this->logException($ex, __FUNCTION__);
$notification = new Notification($this->getNotificationExceptionText($ex));
Expand Down

0 comments on commit 478519f

Please sign in to comment.