使用Google Ads Api获取有关广告的报告

问题描述 投票:0回答:1

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

我在客户端库中看到了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获取广告报告的任何想法,有人做过吗?我似乎无法弄清楚看到文档和客户端库。

ads adwords-api-v201802 google-ads-api
1个回答
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
)
© www.soinside.com 2019 - 2024. All rights reserved.