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

Get Reports On Ads Using Google Ads Api

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

我在客户端库中看到了 google 广告 api 及其示例。但是我无法理解如何获取广告报告。

我制作了一个功能,可以通过 google 广告 api 为我获取报告。

Google 广告 Api:https://developers.google.com/google-ads/api/docs/fields/ad_group_ad#ad_group_adadexpanded_text_addescription2

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

好吧,我做了一些研究。有两种类型的广告。

1.加大型文字广告

2。仅调用广告

我检查了我的广告类型 运行,结果是 'Expanded Text Ads'。然后从此处的 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
)