Googleads PHP API how to setup access?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Googleads PHP API how to setup access?



I'm feeling totally and utterly lost in a circle of documentation links without anything given me the answers I need.



What I want to achieve is simple, hit an API endpoint and get key data back for a campaign (cpc, clicks, impressions, costs etc). This will probably be set up on a cron to run every week.



Looking at the Github page here https://github.com/googleads/googleads-php-lib it sends you to this link for setting up API access on behalf of your clients (web flow) https://github.com/googleads/googleads-php-lib/wiki/API-access-on-behalf-of-your-clients-(web-flow)



I have created a adwords clientID and client secret as per the instructions above but then the next steps are asking to redirect the users to a login page to confirm access?



Is there anyway around this as it's simply not going to work on a cron to pull in some data once a week, the user will have to manually do it.




1 Answer
1



You have to to setup OAuth2 for api access, you should follow these instructions .

So now you have your adsapi_php.ini configured with your credentials, and it allows you to query the api.

Then you can use the php sdk to perform queries:


adsapi_php.ini


php sdk


$dir = '/adsapi_php.ini' // the path of your .ini file;
require '/vendor/autoload.php' // load the sdk;
use GoogleAdsApiAdWordsAdWordsServices;
use GoogleAdsApiAdWordsAdWordsSession;
use GoogleAdsApiAdWordsAdWordsSessionBuilder;
... // load what you need

$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile($dir)
->build();

$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();

$adWordsServices = new AdWordsServices();



This above is just an example, i suggest to look on github form more detailed and working examples.



Edit

For have statistics about one campaign, you have to do a Criteria performance report.

This is an example of code that usual i use: (note that is a part of a big application probably not working straight out the box). it generate the last week report for a campaign.


$dir = 'path/to/adsapi_php.ini';
require 'path/to/vendor/autoload.php';

use GoogleAdsApiAdWordsAdWordsServices;
use GoogleAdsApiAdWordsAdWordsSession;
use GoogleAdsApiAdWordsAdWordsSessionBuilder;
use GoogleAdsApiAdWordsv201802cmAdGroupService;
use GoogleAdsApiAdWordsv201802cmAdGroupAdService;
use GoogleAdsApiAdWordsv201802cmOrderBy;
use GoogleAdsApiAdWordsv201802cmPolicyApprovalStatus;
use GoogleAdsApiAdWordsv201802cmSortOrder;
use GoogleAdsApiAdWordsv201802cmPaging;
use GoogleAdsApiAdWordsv201802cmExpandedTextAd;
use GoogleAdsApiAdWordsv201802cmAdGroupAdOperation;
use GoogleAdsApiAdWordsv201802cmAdType;
use GoogleAdsApiAdWordsv201802cmAdGroupAdStatus;
use GoogleAdsApiAdWordsReportingv201802DownloadFormat;
use GoogleAdsApiAdWordsReportingv201802ReportDefinition;
use GoogleAdsApiAdWordsReportingv201802ReportDefinitionDateRangeType;
use GoogleAdsApiAdWordsReportingv201802ReportDownloader;
use GoogleAdsApiAdWordsReportSettingsBuilder;
use GoogleAdsApiAdWordsv201802cmPredicate;
use GoogleAdsApiAdWordsv201802cmPredicateOperator;
use GoogleAdsApiAdWordsv201802cmReportDefinitionReportType;
use GoogleAdsApiAdWordsv201802cmSelector;
use GoogleAdsApiAdWordsv201802cmDateRange;
use GoogleAdsApiCommonOAuth2TokenBuilder;

$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile($dir)
->build();

$adWordsServices = new AdWordsServices();

$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->withClientCustomerId($accountId)
->build();

getaveragestats($oAuth2Credential, $session,$accountId,$campaignid,$campaingname);
function getaveragestats($oAuth2Credential, $session,$accountId,$campaignid,$campaingname)

$oneweekago = strtotime("-1 week");
$oneweekAgo = date("Ymd",$oneweekago);
$datefrom = $oneweekAgo;
$dateto = date("Ymd");
$selector = new Selector();
$selector->setDateRange(new DateRange($datefrom, $dateto));
$selector->setFields(['Date','Impressions','AveragePosition','AverageCpc']);
$selector->setPredicates([
new Predicate('CampaignId', PredicateOperator::IN, [$campaignid])]);
$reportDefinition = new ReportDefinition();
$reportDefinition->setSelector($selector);
$reportDefinition->setReportName('Report per la campagna #' . $campaignid);
$reportDefinition->setDateRangeType(ReportDefinitionDateRangeType::CUSTOM_DATE);
$reportDefinition->setReportType(ReportDefinitionReportType::CAMPAIGN_PERFORMANCE_REPORT);
$reportDefinition->setDownloadFormat(DownloadFormat::CSV);

// Download report.
$reportDownloader = new ReportDownloader($session);
$reportSettingsOverride = (new ReportSettingsBuilder())
->includeZeroImpressions(false)
->build();
$reportDownloadResult = $reportDownloader->downloadReport($reportDefinition, $reportSettingsOverride);

$reportAsString = $reportDownloadResult->getAsString();
$your_array = array_filter(explode("n",$reportAsString));
$titolo = $your_array[0];
$rowNames = $your_array[1];
$rowNames = explode(",", $rowNames);
$total = end($your_array);
$tot = explode(",", $total);
$result = ;
for ($x = 2; $x < (count($your_array)-1); $x++)
$row = explode(",", $your_array[$x]);
$tmp = array(
'data'=>$row[0],
'impressions'=>$row[1],
'position'=>$row[2],
'cpc'=> intval($row[3])/100000
);
$result=$tmp;

function sortFunction( $a, $b )
return strtotime($a["data"]) - strtotime($b["data"]);

$avrg = getAverage($result);
return $avrg






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard