Skip to content

Commit

Permalink
Finished out the other List Commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
generalredneck committed Dec 28, 2015
1 parent 0715c59 commit 384012b
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 6 deletions.
6 changes: 5 additions & 1 deletion application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
19 changes: 15 additions & 4 deletions src/Command/ListAccountsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<error>A service-email must be configured.</error>');
return 1;
}
if (empty($key_location)) {
$output->writeln('<error>A key-location must be configured.</error>');
return 2;
}
if (!file_exists($key_location)) {
$output->writeln('<error>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();
Expand Down
75 changes: 75 additions & 0 deletions src/Command/ListPropertiesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace GeneralRedneck\GaReferrerSpamFilters\Command;

use GeneralRedneck\GaReferrerSpamFilters\Service;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ListPropertiesCommand extends Command
{
protected function configure()
{
$this->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('<error>A service-email must be configured.</error>');
return 1;
}
if (empty($key_location)) {
$output->writeln('<error>A key-location must be configured.</error>');
return 2;
}
if (!file_exists($key_location)) {
$output->writeln('<error>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("<comment>No Account configured, but there is only one account available. Using " . $accounts[0]->getId() . ":" . $accounts[0]->name . " </comment>");
$ga_account_id = $accounts[0]->getId();
}
else {
$output->writeln("<error>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.</error>");
}
}
}
$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("<error>No web property ids found for this user.</error>");
return 1;
}
}
}
97 changes: 97 additions & 0 deletions src/Command/ListViewsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
namespace GeneralRedneck\GaReferrerSpamFilters\Command;

use GeneralRedneck\GaReferrerSpamFilters\Service;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ListViewsCommand extends Command
{
protected function configure()
{
$this->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('<error>A service-email must be configured.</error>');
return 1;
}
if (empty($key_location)) {
$output->writeln('<error>A key-location must be configured.</error>');
return 2;
}
if (!file_exists($key_location)) {
$output->writeln('<error>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("<comment>No Account configured, but there is only one account available. Using " . $accounts[0]->getId() . ":" . $accounts[0]->name . " </comment>");
$ga_account_id = $accounts[0]->getId();
}
else {
$output->writeln("<error>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.</error>");
}
}
}
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("<comment>No property id configured, but there is only one available. Using " . $properties[0]->getId() . ":" . $properties[0]->name . " </comment>");
$ga_property_id = $properties[0]->getId();
}
else {
$output->writeln("<error>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.</error>");
}
}
}
$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("<error>No views found for the specified account or web property id.</error>");
return 1;
}
}
}
9 changes: 8 additions & 1 deletion src/Command/UpdateGaFiltersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 384012b

Please sign in to comment.