diff --git a/application.php b/application.php index de0ed64..dbe3f51 100644 --- a/application.php +++ b/application.php @@ -3,8 +3,10 @@ require __DIR__.'/vendor/autoload.php'; use GeneralRedneck\GaReferrerSpamFilters\Command\ListAccountsCommand; -use GeneralRedneck\GaReferrerSpamFilters\Command\UpdateSpamListCommand; +use GeneralRedneck\GaReferrerSpamFilters\Command\ListPropertiesCommand; +use GeneralRedneck\GaReferrerSpamFilters\Command\ListViewsCommand; use GeneralRedneck\GaReferrerSpamFilters\Command\UpdateGaFiltersCommand; +use GeneralRedneck\GaReferrerSpamFilters\Command\UpdateSpamListCommand; use Symfony\Component\Config\FileLocator; use Symfony\Component\Console\Application; use Symfony\Component\Console\Input\InputOption; @@ -51,4 +53,6 @@ $application->add(new ListAccountsCommand()); $application->add(new UpdateSpamListCommand()); $application->add(new UpdateGaFiltersCommand()); +$application->add(new ListPropertiesCommand()); +$application->add(new ListViewsCommand()); $application->run(); diff --git a/src/Command/ListAccountsCommand.php b/src/Command/ListAccountsCommand.php index 3e8d38a..a71fd63 100644 --- a/src/Command/ListAccountsCommand.php +++ b/src/Command/ListAccountsCommand.php @@ -19,10 +19,21 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { - $service = new Service( - $input->getOption('service-email'), - $input->getOption('key-location') - ); + $service_email = $input->getOption('service-email'); + $key_location = $input->getOption('key-location'); + if (empty($service_email)) { + $output->writeln('A service-email must be configured.'); + return 1; + } + if (empty($key_location)) { + $output->writeln('A key-location must be configured.'); + return 2; + } + if (!file_exists($key_location)) { + $output->writeln('The file ' . $key_location . ' does not exist.'); + return 3; + } + $service = new Service($service_email, $key_location); $accounts = $service->getGaAccounts(); if (count($accounts->getItems()) > 0) { $table_data = array(); diff --git a/src/Command/ListPropertiesCommand.php b/src/Command/ListPropertiesCommand.php new file mode 100644 index 0000000..7bb51bf --- /dev/null +++ b/src/Command/ListPropertiesCommand.php @@ -0,0 +1,75 @@ +setName('listproperties') + ->setDescription('List GA Web Property Ids (UA-xxxxxxx-yy) associated with the configured GA account') + ->addOption( + 'ga-account-id', + 'a', + InputOption::VALUE_OPTIONAL, + 'Set the id of the account you wish to connect to via the service email. See listaccounts.' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $ga_account_id = $input->getOption('ga-account-id'); + $service_email = $input->getOption('service-email'); + $key_location = $input->getOption('key-location'); + if (empty($service_email)) { + $output->writeln('A service-email must be configured.'); + return 1; + } + if (empty($key_location)) { + $output->writeln('A key-location must be configured.'); + return 2; + } + if (!file_exists($key_location)) { + $output->writeln('The file ' . $key_location . ' does not exist.'); + return 3; + } + $service = new Service($service_email, $key_location); + if (empty($ga_account_id)) { + if (!empty($this->getApplication()->config['ga-account-id'])) { + $ga_account_id = $this->getApplication()->config['ga-account-id']; + } + else { + $accounts = $service->getGaAccounts()->getItems(); + if (count($accounts) == 1) { + $output->writeln("No Account configured, but there is only one account available. Using " . $accounts[0]->getId() . ":" . $accounts[0]->name . " "); + $ga_account_id = $accounts[0]->getId(); + } + else { + $output->writeln("No Account configured and more than one account is associated with this service email. Configure an account id by passing --ga-account-id or modifying config.yml. See listaccounts for a full list of accounts."); + } + } + } + $properties = $service-> getGaProperties($ga_account_id); + if (count($properties->getItems()) > 0) { + $table_data = array(); + foreach ($properties as $property) { + $table_data[] = array($property->id, $property->name); + } + $table = new Table($output); + $table->setHeaders(array('ID', 'Name')) + ->setRows($table_data); + $table->render(); + } + else { + $output->writeln("No web property ids found for this user."); + return 1; + } + } +} diff --git a/src/Command/ListViewsCommand.php b/src/Command/ListViewsCommand.php new file mode 100644 index 0000000..691d7cc --- /dev/null +++ b/src/Command/ListViewsCommand.php @@ -0,0 +1,97 @@ +setName('listviews') + ->setDescription('List GA views associated with the configured GA account and Web Property Id') + ->addOption( + 'ga-account-id', + 'a', + InputOption::VALUE_OPTIONAL, + 'Set the id of the account you wish to connect to via the service email. See listaccounts.' + ) + ->addOption( + 'ga-property-id', + 'p', + InputOption::VALUE_OPTIONAL, + 'Set the id of the web property you wish to connect to via the account id. See listproperties.' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $ga_account_id = $input->getOption('ga-account-id'); + $ga_property_id = $input->getOption('ga-property-id'); + $service_email = $input->getOption('service-email'); + $key_location = $input->getOption('key-location'); + if (empty($service_email)) { + $output->writeln('A service-email must be configured.'); + return 1; + } + if (empty($key_location)) { + $output->writeln('A key-location must be configured.'); + return 2; + } + if (!file_exists($key_location)) { + $output->writeln('The file ' . $key_location . ' does not exist.'); + return 3; + } + $service = new Service($service_email, $key_location); + if (empty($ga_account_id)) { + if (!empty($this->getApplication()->config['ga-account-id'])) { + $ga_account_id = $this->getApplication()->config['ga-account-id']; + } + else { + $accounts = $service->getGaAccounts()->getItems(); + if (count($accounts) == 1) { + $output->writeln("No Account configured, but there is only one account available. Using " . $accounts[0]->getId() . ":" . $accounts[0]->name . " "); + $ga_account_id = $accounts[0]->getId(); + } + else { + $output->writeln("No Account configured and more than one account is associated with this service email. Configure an account id by passing --ga-account-id or modifying config.yml. See listaccounts for a full list of accounts."); + } + } + } + if (empty($ga_property_id)) { + if (!empty($this->getApplication()->config['ga-property-id'])) { + $ga_property_id = $this->getApplication()->config['ga-property-id']; + } + else { + $properties = $service->getGaProperties($ga_account_id)->getItems(); + if (count($properties) == 1) { + $output->writeln("No property id configured, but there is only one available. Using " . $properties[0]->getId() . ":" . $properties[0]->name . " "); + $ga_property_id = $properties[0]->getId(); + } + else { + $output->writeln("No property id configured and more than one web property is associated with this service email and account id. Configure a web property id by passing --ga-property-id or modifying config.yml. See listproperties for a full list of web properties."); + } + } + } + $views = $service->getGaViews($ga_account_id, $ga_property_id); + if (count($views->getItems()) > 0) { + $table_data = array(); + foreach ($views as $view) { + $table_data[] = array($view->id, $view->name); + } + $table = new Table($output); + $table->setHeaders(array('ID', 'Name')) + ->setRows($table_data); + $table->render(); + } + else { + $output->writeln("No views found for the specified account or web property id."); + return 1; + } + } +} diff --git a/src/Command/UpdateGaFiltersCommand.php b/src/Command/UpdateGaFiltersCommand.php index cec4564..b6ea9b5 100644 --- a/src/Command/UpdateGaFiltersCommand.php +++ b/src/Command/UpdateGaFiltersCommand.php @@ -44,10 +44,10 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $service_email = $input->getOption('service-email'); $key_location = $input->getOption('key-location'); - $domain_list_location = $input->getOption('domain-list-location'); $ga_account_id = $input->getOption('ga-account-id'); $ga_property_id = $input->getOption('ga-property-id'); $ga_view_id = $input->getOption('ga-view-id'); + $domain_list_location = $input->getOption('domain-list-location'); $domain_list_location = empty($domain_list_location) ? $this->getApplication()->config['domain-list-location'] : $domain_list_location; if (empty($service_email)) { @@ -149,6 +149,11 @@ protected function execute(InputInterface $input, OutputInterface $output) { $filter->setExcludeDetails($details); $filterResult = $service->getGaService()->management_filters->insert($ga_account_id, $filter); sleep(1); + + // TODO: we need to check to see if the filters are linked with the + // specified view cause we may already have the filters but they just + // not linked up. + // Construct the filter reference. $filterRef = new \Google_Service_Analytics_FilterRef(); $filterRef->setAccountId($ga_account_id); @@ -162,6 +167,8 @@ protected function execute(InputInterface $input, OutputInterface $output) { } else { $filter = $filters[$name]; + // TODO: we need to check to see what other views we are updateing by + // updating this filter. if ($filter->getExcludeDetails()->getExpressionValue() != $details->getExpressionValue()) { $filter->setType("EXCLUDE"); $filter->setExcludeDetails($details);