使用Google Ads Api获取广告报告

时间:2019-04-19 10:47:19

标签: ads adwords-api-v201802 google-ads-api

我正在尝试获取有关广告系列广告效果的报告。 到目前为止,我已经获得了广告系列效果报告,但我无法获得广告效果报告。

我在客户端库中看到了google ads api及其示例。但是我不明白如何获取广告报告。

我正在制作一个可以通过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));
    }

}

我正在尝试从数组中的api获取这种简单的值

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
)

关于如何从api获取广告报告的任何想法,有人做到了吗?我似乎不知道看到文档和客户端库。

2 个答案:

答案 0 :(得分:0)

嗯,我做了一些研究。广告有两种。

1。扩展文字广告

2。仅致电广告

我检查了我的广告的投放类型是“扩展文字广告”。然后从此处的api文档中选择字段 ad_group_ad.ad.expanded_text_ad.headline_part1

https://developers.google.com/google-ads/api/docs/fields/ad_group_ad#ad_group_adadexpanded_text_adheadline_part1

这是完整的功能:

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));
    }
}

我得到了现场结果:

Array
(
   [headline part 1] => Small Business System
)

答案 1 :(得分:0)

别忘了RSA。它们有点棘手,并且资产需要您自己解析才能返回。

相关问题