How to use key-value and channel to report on Admanager API

40 views
Skip to first unread message

João Victor

unread,
Aug 29, 2024, 10:22:59 PM8/29/24
to Google Ad Manager API Forum
Hello everyone,

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.

The table would be something like this:

Channel | Utm_medium | Revenue

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.


I was only able to generate the utm_medium and the recipe

<?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>';

Ad Manager API Forum Advisor

unread,
Aug 30, 2024, 6:54:42 AM8/30/24
to astudi...@gmail.com, google-doubleclick...@googlegroups.com
Hi,

Thank you for contacting the Ad Manager API support team.

I have reviewed your query and understand that you want to retrieve report with Channel Name, Key-value and the revenue. Please note that not all combinations of columns and dimensions are supported in Ad Manager. I have checked all possible ways to achieve your goal but unfortunately, it is not possible to retrieve data combined in a single report. I recommended generating the different reports and combining them on a common dimension. This can be achieved by doing post processing on your side.Please note key-value and custom dimension are not compatible with each other.
 
This message is in relation to case "ref:!00D1U01174p.!5004Q02vFXiQ:ref" (ADR-00265643)

Thanks,
 
Google Logo Ad Manager API Team


Reply all
Reply to author
Forward
0 new messages