Get account values

47 views
Skip to first unread message

Hugo Senna

unread,
Mar 16, 2021, 3:48:34 PM3/16/21
to AdWords API and Google Ads API Forum

Hello, I am developing a panel to simplify the visualization. I need to recover the following data:

Applied value;
amount spent or amount remaining

I am using the following code:

<?php
 
require __DIR__ . '/googleapi/autoload.php';
 
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\ReportSettingsBuilder;
use Google\AdsApi\AdWords\v201809\cm\CampaignService;
use Google\AdsApi\AdWords\v201702\cm\ReportDefinitionReportType;
use Google\AdsApi\AdWords\v201702\cm\ReportDefinitionService;
use Google\AdsApi\AdWords\Reporting\v201809\ReportDownloader;
use Google\AdsApi\AdWords\v201809\cm\OrderBy;
use Google\AdsApi\AdWords\v201809\cm\Paging;
use Google\AdsApi\AdWords\v201809\cm\Selector;
use Google\AdsApi\Common\OAuth2TokenBuilder;
 
class Adwords
{    
 
    public static function getData(){
        $oAuth2Credential = (new OAuth2TokenBuilder())
            ->fromFile()
            ->build();            
 
        $session = (new AdWordsSessionBuilder())
            ->fromFile()
            ->withOAuth2Credential($oAuth2Credential)  
            ->withClientCustomerId('495-142-3572')                      
            ->build();   //708-050-6564
         
       
        $reportQuery = 'SELECT AbsoluteTopImpressionPercentage,
ActiveViewCpm,
ActiveViewCtr,
ActiveViewImpressions,
ActiveViewMeasurability,
ActiveViewMeasurableCost,
ActiveViewMeasurableImpressions,
ActiveViewViewability,
AllConversionRate,
AllConversions,
AllConversionValue,
AverageCost,
AverageCpc,
AverageCpe,
AverageCpm,
AverageCpv,
AveragePosition,
Clicks,
ContentBudgetLostImpressionShare,
ContentImpressionShare,
ContentRankLostImpressionShare,
ConversionRate,
Conversions,
ConversionValue,
Cost,
CostPerAllConversion,
CostPerConversion,
CrossDeviceConversions,
Ctr,
EngagementRate,
Engagements,
Impressions,
InteractionRate,
Interactions,
InteractionTypes,
InvalidClickRate,
InvalidClicks,
SearchBudgetLostImpressionShare,
SearchExactMatchImpressionShare,
SearchImpressionShare,
SearchRankLostImpressionShare,
TopImpressionPercentage,
ValuePerAllConversion,
ValuePerConversion,
VideoViewRate,
VideoViews,
ViewThroughConversions FROM ACCOUNT_PERFORMANCE_REPORT DURING 20210301,20210331';
         
        $reportFormat = 'XML';
        $reportDownloader = new ReportDownloader($session);
        $reportDownloadResult = $reportDownloader->downloadReportWithAwql(
        $reportQuery, $reportFormat);
 
        $datatest =  $reportDownloadResult->getAsString();
 
        $xml = simplexml_load_string($datatest);
        $json = json_encode($xml);     
       
        $json = json_decode($json, true);
       
        echo "<pre>";
        print_r($json);
        echo "</pre>";
       
       
        $custo = $json['table']['row']['@attributes']['cost'];
        $cpc = $json['table']['row']['@attributes']['avgCPC'];
        $leadmes = $json['table']['row']['@attributes']['allConv'];
       
        $costAllConv = $json['table']['row']['@attributes']['costAllConv'];
        $costConv = $json['table']['row']['@attributes']['costConv'];
       
       
        echo "Impressões: " .$json['table']['row']['@attributes']['impressions']."<br>";
        echo "Clicks: " .$json['table']['row']['@attributes']['clicks']."<br>";
        echo "Custo: ". number_format($custo / 1000000, 2, ',', '.')."<br>";
        echo "CPC Médio: ". number_format($cpc / 1000000, 2, ',', '.')."<br>";
        echo "Lead do mês: ". number_format($leadmes, 2, ',', '.')."<br>";
        echo "<hr>";
        echo "costAllConv: ". number_format($costAllConv / 1000000, 2, ',', '.')."<br>";
        echo "costConv: ". number_format($costConv / 1000000, 2, ',', '.')."<br>";
       
    }
}
 
AdWords::getData();

google_api.jpg

Google Ads API Forum Advisor Prod

unread,
Mar 17, 2021, 6:19:35 AM3/17/21
to hugo...@gmail.com, adwor...@googlegroups.com
Hi Hugo,

Thank you for posting your concern.

Could you confirm if my understanding is correct that you want to get the amount spent and amount remaining of your Google Ads account? If yes, then you can use the Cost field, which I can see that you are already using, to get the spend of your account accumulated for specific date.

As for the amount remaining, I am afraid that there is currently no field that could return this value. Please follow our blog as we will post there any updates about the fields that could return this data.

Otherwise, please provide more details to your concern so that I can check if it is possible in AdWords API.

Regards,
Google Logo
Ernie John Blanca Tacata
Google Ads API Team
 


ref:_00D1U1174p._5004Q2Dw9Nf:ref
Reply all
Reply to author
Forward
0 new messages