How can I generate a report with Channel Name, Key-value and get the revenue from this report?
I need to generate a table that returns the earnings from the utm_medium key-value and I also need the utm_campaign that is in the Channel.
I tested using a saved query but Google does not allow me to generate this report with key-value and custom dimension.
The idea of this report is to have the ID of the arbitrage campaigns and get the sources from which the traffic came to know how much ROI is returning.
<?php
namespace Google\AdsApi\Examples\AdManager\v202408\ReportService;
require 'vendor/autoload.php';
error_reporting(true);
ini_set("display_errors", true);
use Google\AdsApi\AdManager\AdManagerSession;
use Google\AdsApi\AdManager\AdManagerSessionBuilder;
use Google\AdsApi\AdManager\Util\v202408\ReportDownloader;
use Google\AdsApi\AdManager\v202408\Column;
use Google\AdsApi\AdManager\v202408\DateRangeType;
use Google\AdsApi\AdManager\v202408\Dimension;
use Google\AdsApi\AdManager\v202408\ExportFormat;
use Google\AdsApi\AdManager\v202408\ReportJob;
use Google\AdsApi\AdManager\v202408\ReportQuery;
use Google\AdsApi\AdManager\v202408\ServiceFactory;
use Google\AdsApi\Common\OAuth2TokenBuilder;
// Incluindo o Tailwind CSS via CDN
echo '<!DOCTYPE html>';
echo '<html lang="en">';
echo '<head>';
echo '<meta charset="UTF-8">';
echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
echo '<link href="
https://cdn.jsdelivr.net/npm/tailw...@2.2.19/dist/tailwind.min.css" rel="stylesheet">';
echo '<title>UTM Medium Report</title>';
echo '</head>';
echo '<body>';
class RunReachReport
{
public static function runExample(
ServiceFactory $serviceFactory,
AdManagerSession $session
) {
$reportService = $serviceFactory->createReportService($session);
$reportQuery = new ReportQuery();
$reportQuery->setDimensions([Dimension::CUSTOM_CRITERIA]);
$reportQuery->setColumns([Column::AD_EXCHANGE_LINE_ITEM_LEVEL_REVENUE]);
$reportQuery->setDateRangeType(DateRangeType::REACH_LIFETIME);
$reportJob = new ReportJob();
$reportJob->setReportQuery($reportQuery);
$reportJob = $reportService->runReportJob($reportJob);
$reportDownloader = new ReportDownloader($reportService, $reportJob->getId());
if ($reportDownloader->waitForReportToFinish()) {
$reportData = $reportDownloader->downloadReport(ExportFormat::XML);
$decodedData = gzdecode($reportData);
$xml = simplexml_load_string($decodedData);
$utmMediumResults = self::extractUtmMedium($xml);
self::renderTable($utmMediumResults);
} else {
print "Report failed.\n";
return [];
}
}
private static function extractUtmMedium($xml)
{
$results = [];
foreach ($xml->ReportData->DataSet->Row as $row) {
$utmMedium = null;
$revenue = null;
foreach ($row->Column as $column) {
$name = (string)$column['name'];
$value = (string)$column->Val;
if ($name === 'customCriteriaName' && strpos($value, 'utm_medium=') !== false) {
$utmMedium = $value;
}
if ($name === 'adxReservationPubCostDelivered') {
$revenue = $value;
}
}
if ($utmMedium !== null) {
$results[] = [
'utm_medium' => $utmMedium,
'revenue' => $revenue,
];
}
}
return $results;
}
private static function renderTable($utmMediumResults)
{
echo '<div class="container mx-auto mt-8">';
echo '<table class="min-w-full bg-white border border-gray-200">';
echo '<thead>';
echo '<tr>';
echo '<th class="py-2 px-4 border-b">UTM Medium</th>';
echo '<th class="py-2 px-4 border-b">Revenue</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($utmMediumResults as $result) {
echo '<tr>';
echo '<td class="py-2 px-4 border-b">' . htmlspecialchars($result['utm_medium']) . '</td>';
echo '<td class="py-2 px-4 border-b">' . htmlspecialchars($result['revenue']) . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
}
public static function main()
{
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()
->build();
$session = (new AdManagerSessionBuilder())->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
self::runExample(new ServiceFactory(), $session);
}
}
RunReachReport::main();
// Fechando o HTML
echo '</body>';
echo '</html>';