Reputation: 203
I am trying to get the reports for campaigns and ads performance. So far i have gotten the campaign performance reports, but i am unable to to get ads performance reports.
I have seen the google ads api and and their examples in client library. But i am unable to understand how to get ad reports.
I making a function that will fetch the reports for me via google ads api.
Google Ads Api: https://developers.google.com/google-ads/api/docs/fields/ad_group_ad#ad_group_adadexpanded_text_addescription2
Google Ads Api Github: https://github.com/googleads/google-ads-php/
public function getAdsPerformance($customerId)
{
// Customer ID which i am using ---> 2942416690
try {
// Creates a query that retrieves all campaigns.
$query = 'SELECT ad_group_ad.ad.expanded_text_ad.description2 FROM ad_group_ad';
// Issues a search request by specifying page size.
$response = $this->googleAdsServiceClient->search($customerId, $query, ['pageSize' => $this->page_size]);
// Iterates over all rows in all pages and prints the requested field values for
// the campaign in each row.
foreach ($response->iterateAllElements() as $googleAdsRow) {
$adGroup = $googleAdsRow->getAdGroupAd();
// $customer = $googleAdsRow->getCustomer();
// $metrics = $googleAdsRow->getMetrics();
/** @var GoogleAdsRow $googleAdsRow */
$result = [
'ad' => $adGroup->getResourceName()->getValue(),
];
print "<pre>";
print_r($result);
print "</pre>";
}
} catch (GoogleAdsException $googleAdsException) {
printf(
"Request with ID '%s' has failed.%sGoogle Ads failure details:%s",
$googleAdsException->getRequestId(),
PHP_EOL,
PHP_EOL
);
foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
$error = [
'code' => $error->getErrorCode()->getErrorCode(),
'status' => $error->getStatus(),
'message' => $error->getMessage()
];
printf(json_encode($error));
}
} catch (ApiException $apiException) {
$error = [
'code' => $apiException->getCode(),
'status' => $apiException->getStatus(),
'message' => $apiException->getBasicMessage()
];
printf(json_encode($error));
}
}
I am trying to get this type of simple values from api in array
Array
(
[campaign] => some test campaign
[currency] => PLN
[clicks] => 100
[impressions] => 300
[cost] => 250.08
[avg_position] => 1.07
[avg_cpc] => 0.8
[conversions] => 0
[cost/conv] => 0
[conv_rate] => 0
[ctr] => 0.9
[avg_cpm] => 2.5
[interaction_rate] => 0.1
[interactions] => 52
)
Any idea about how can i get the ad reports from the api, Anybody done it? I can't seem to figure out seeing documentation and the client library.
Upvotes: 1
Views: 2313
Reputation: 203
Well, i did it with some research. There are two types of ads.
1. Expanded Text Ads
2. Call only Ads
I checked what was the type of my ad running, and it was 'Expanded Text Ads'. And then picked the field ad_group_ad.ad.expanded_text_ad.headline_part1 from the api documentation from here:
this is complete function:
public function getAdsPerformance($customerId)
{
try {
$query =
'SELECT ad_group_ad.ad.expanded_text_ad.headline_part1 '
. 'FROM ad_group_ad '
. 'WHERE ad_group_ad.ad.type = EXPANDED_TEXT_AD';
$response = $this->googleAdsServiceClient->search($customerId, $query, ['pageSize' => $this->page_size]);
foreach ($response->iterateAllElements() as $googleAdsRow) {
$ad = $googleAdsRow->getAdGroupAd()->getAd();
$result = [
'headline part 1' => $ad->getExpandedTextAd()->getHeadlinePart1()->getValue(),
];
print "<pre>";
print_r($result);
print "</pre>";
}
} catch (GoogleAdsException $googleAdsException) {
printf(
"Request with ID '%s' has failed.%sGoogle Ads failure details:%s",
$googleAdsException->getRequestId(),
PHP_EOL,
PHP_EOL
);
foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
$error = [
'code' => $error->getErrorCode()->getErrorCode(),
'status' => $error->getStatus(),
'message' => $error->getMessage()
];
// return $error;
printf(json_encode($error));
}
} catch (ApiException $apiException) {
$error = [
'code' => $apiException->getCode(),
'status' => $apiException->getStatus(),
'message' => $apiException->getBasicMessage()
];
printf(json_encode($error));
}
}
And i got the field result:
Array
(
[headline part 1] => Small Business System
)
Upvotes: 2