I'm having a problem pulling reports from the API.
In the Google Ad Manager dashboard, when generating a report, the information appears correctly, click impressions...
When I pull it through the API, I don't get any information, just the column headings.
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
$session = (new AdManagerSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build();
$adManagerServices = new AdManagerServices();
// Criação da consulta de relatório.
$reportQuery = new ReportQuery();
$reportQuery->setDimensions([Dimension::DATE]); // Dimensão Data
$reportQuery->setColumns([
Column::AD_SERVER_IMPRESSIONS,
Column::AD_SERVER_CLICKS,
Column::AD_SERVER_CTR,
Column::AD_SERVER_CPM_AND_CPC_REVENUE
]); // Adicione as métricas que você deseja incluir
// Configuração de data personalizada para os últimos 7 dias.
$today = new DateTime('now', new DateTimeZone('America/Sao_Paulo'));
$sevenDaysAgo = new DateTime('-7 days', new DateTimeZone('America/Sao_Paulo'));
$reportQuery->setDateRangeType(DateRangeType::CUSTOM_DATE);
$reportQuery->setStartDate(AdManagerDateTimes::fromDateTime($sevenDaysAgo)->getDate());
$reportQuery->setEndDate(AdManagerDateTimes::fromDateTime($today)->getDate());
// Criação e execução do relatório.
$reportJob = new ReportJob();
$reportJob->setReportQuery($reportQuery);
$reportService = $adManagerServices->get($session, ReportService::class);
$reportJob = $reportService->runReportJob($reportJob);
// Monitoramento do status do relatório.
$reportDownloader = new ReportDownloader($reportService, $reportJob->getId());
// Espera até que o relatório esteja pronto.
while (true) {
$status = $reportService->getReportJobStatus($reportJob->getId());
if ($status === ReportJobStatus::COMPLETED) {
break;
} elseif ($status === ReportJobStatus::FAILED) {
throw new \RuntimeException('O relatório falhou ao ser gerado.');
}
sleep(10); // Espera 10 segundos antes de verificar o status novamente.
}
// Download do relatório.
$filePath = tempnam(sys_get_temp_dir(), 'report-') . '.csv';
$reportDownloader->downloadReport(ExportFormat::CSV_DUMP, $filePath);
// Leitura do relatório.
$reportContent = file_get_contents($filePath);
unlink($filePath); // Limpeza do arquivo temporário.
return new Response($reportContent, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="report.csv"',
]);