Reputation: 3993
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.
Upvotes: 0
Views: 1093
Reputation: 7256
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:
$dir = '/adsapi_php.ini' // the path of your .ini file;
require '/vendor/autoload.php' // load the sdk;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
... // 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 Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201802\cm\AdGroupService;
use Google\AdsApi\AdWords\v201802\cm\AdGroupAdService;
use Google\AdsApi\AdWords\v201802\cm\OrderBy;
use Google\AdsApi\AdWords\v201802\cm\PolicyApprovalStatus;
use Google\AdsApi\AdWords\v201802\cm\SortOrder;
use Google\AdsApi\AdWords\v201802\cm\Paging;
use Google\AdsApi\AdWords\v201802\cm\ExpandedTextAd;
use Google\AdsApi\AdWords\v201802\cm\AdGroupAdOperation;
use Google\AdsApi\AdWords\v201802\cm\AdType;
use Google\AdsApi\AdWords\v201802\cm\AdGroupAdStatus;
use Google\AdsApi\AdWords\Reporting\v201802\DownloadFormat;
use Google\AdsApi\AdWords\Reporting\v201802\ReportDefinition;
use Google\AdsApi\AdWords\Reporting\v201802\ReportDefinitionDateRangeType;
use Google\AdsApi\AdWords\Reporting\v201802\ReportDownloader;
use Google\AdsApi\AdWords\ReportSettingsBuilder;
use Google\AdsApi\AdWords\v201802\cm\Predicate;
use Google\AdsApi\AdWords\v201802\cm\PredicateOperator;
use Google\AdsApi\AdWords\v201802\cm\ReportDefinitionReportType;
use Google\AdsApi\AdWords\v201802\cm\Selector;
use Google\AdsApi\AdWords\v201802\cm\DateRange;
use Google\AdsApi\Common\OAuth2TokenBuilder;
$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
}
Upvotes: 1