Dúvida - AdWords API - PHP

87 views
Skip to first unread message

Desenvolvimento - Grupo Lead Negócios Inteligentes

unread,
Jan 22, 2015, 7:50:19 PM1/22/15
to adwor...@googlegroups.com
Olá pessoal, é meu primeiro post aqui no fórum, por isso peço perdão por qualquer erro. :D

Galera, é o seguinte, estou começando agora a desvendar a API e estou com uma dúvida: o script que colei abaixo, só faz o download da campanha em questão, eu quero saber se consigo, ao invés de fazer o download, colocar os dados em alguma variável, por exempo, colocar clicks em uma varialvel, CTR em outra e assim por diante, tem como? Se sim, como? kkkk

<?php
error_reporting(-1);
ini_set('display_errors',1);
$path = dirname(__FILE__) . '/../../src';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

require_once '/Google/Api/Ads/AdWords/Lib/AdWordsUser.php';

$user = new AdWordsUser();

$user->SetClientId('8214013316');

// Log SOAP XML request and response.
$user->LogDefaults();

$user->LoadService('ReportDefinitionService', 'v201409');
$LoadService = $user->LoadService('ReportDefinitionService', 'v201409');

// Create selector.
$selector = new Selector();
$selector->fields = array('CampaignId', 'AdGroupId', 'Id', 'Criteria', 'CriteriaType', 'Impressions', 'Clicks', 'Conversions', 'Cost', 'CustomerDescriptiveName', 'Ctr', 'AveragePosition');

// Filter out deleted criteria.
//$selector->predicates[] = new Predicate('Status', 'NOT_IN', array('DELETED'));

// Create report definition.
$reportDefinition = new ReportDefinition();
$reportDefinition->selector = $selector;
$reportDefinition->reportName = 'Criteria performance report #' . uniqid();
$reportDefinition->dateRangeType = 'ALL_TIME';
$reportDefinition->reportType = 'CRITERIA_PERFORMANCE_REPORT';
$reportDefinition->downloadFormat = 'XML'; //CSVFOREXCEL

// Salva o arquivo no caminho indicado abaixo
$filePath = dirname(__FILE__) . '/report.xml';

// Exclude criteria that haven't recieved any impressions over the date range.
$reportDefinition->includeZeroImpressions = FALSE;

// Set additional options.
$options = array('version' => 'v201409', 'returnMoneyInMicros' => TRUE);

// Download report.
ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);


printf("Report with name '%s' was downloaded to '%s'.\n", $reportDefinition->reportName, $filePath);


$doc = new DOMDocument();
$doc->loadXML(file_get_contents($filePath));
$xp = new DOMXPath($doc);
$q = $xp->query("/report/table/row/@cost");
$cost = 0;
foreach($q as $el) {
  $v = $el->textContent;
  $cost += $v / 1000000;
}

echo "Custo: ".$cost;
?>

Desde já, obrigado! 

Anash P. Oommen (AdWords API Team)

unread,
Jan 23, 2015, 7:41:37 AM1/23/15
to adwor...@googlegroups.com
Hi,

If you pass a NULL for the filePath, the DownloadReport will return the report as a String.

Cheers,
Anash P. Oommen,
AdWords API Advisor.

Desenvolvimento - Grupo Lead Negócios Inteligentes

unread,
Jan 23, 2015, 2:27:15 PM1/23/15
to adwor...@googlegroups.com
Obrigado Anash!

Só confirmando, ficará assim? 

// Salva o arquivo no caminho indicado abaixo
$filePath = NULL;

// Download report
ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);

printf("Report with name '%s' was downloaded to '%s'.\n", $reportDefinition->reportName, $filePath);


Obrigado de novo!

Anash P. Oommen (AdWords API Team)

unread,
Jan 27, 2015, 6:10:34 AM1/27/15
to adwor...@googlegroups.com
Hi,

You could either do

$filePath = "myreport.csv";

ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);
printf
("Report with name '%s' was downloaded to '%s'.\n", $reportDefinition->reportName, $filePath);

or

$filePath = NULL;
$report
= ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);
printf
("Report  contents are \n%s", $report);

Cheers,
Anash

Desenvolvimento - Grupo Lead Negócios Inteligentes

unread,
Jan 28, 2015, 12:39:14 PM1/28/15
to adwor...@googlegroups.com
Novamente obrigado Anash!

Última dúvida: Tem como retornar um ARRAY ao invés de STRING?


Obrigado!

Anash P. Oommen (AdWords API Team)

unread,
Jan 29, 2015, 6:44:15 AM1/29/15
to adwor...@googlegroups.com
Hi,

The return value should be mixed. As you can see on https://github.com/googleads/googleads-php-lib/blob/master/src/Google/Api/Ads/AdWords/Util/ReportUtils.php#L166, what the method returns is the response from a curl_exec call.

Cheers,
Anash

Desenvolvimento - Grupo Lead Negócios Inteligentes

unread,
Jan 29, 2015, 12:06:49 PM1/29/15
to adwor...@googlegroups.com
Entendi, muito obrigado Anash!

Reply all
Reply to author
Forward
0 new messages