当前位置: 首页 > 知识库问答 >
问题:

使用谷歌广告Api获取广告报告

魏雅惠
2023-03-14

我想得到的活动和广告表现的报告。到目前为止,我已经得到了竞选业绩报告,但我无法得到广告业绩报告。

我在客户端库中看到了谷歌广告api和它们的例子。但我无法理解如何获得广告报道。

我正在制作一个函数,通过谷歌广告api为我获取报告。

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

谷歌广告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个答案

漆雕修能
2023-03-14

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

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
)
 类似资料:
  • 我正在使用api google adwords来生成报告,我可以为活动获得它,但我不能为广告生成报告(我需要获得AdId,AdName,clicks,impressions cost和许多其他数据)。我应该使用哪种类型的报告?我试着用: 但它将错误返回为“未知字段”,或者不返回任何内容。 多谢了。

  • 我合作的公司有许多客户使用谷歌广告为他们的网站做营销。公司希望使用客户的数据进行分析。我得到了一个项目,使用C#集成谷歌广告API,从谷歌广告中获取所有数据,如活动等,并将其移动到我们的系统中为每个客户端。我只给开发人员令牌和客户端的客户ID。例如 客户A的客户ID 客户B的客户ID 当我浏览谷歌广告API文档时,我有点不知所措。在文档中,是OAuth2。0需要创建才能使用客户端库,该库将生成客户

  • 我正在开发一个网络应用程序,代表用户获取谷歌广告活动的信息。我使用的是谷歌提供的python库,但我在获取开始测试API调用的初始凭据时遇到了很多麻烦。 我遵循以下文件:https://github.com/googleads/google-ads-python/wiki/OAuth-Web-Application-Flow 我已经完成了第一步,我有我的客户机密码、客户机ID和重定向uri。 在第

  • sp_getad(); 功能: 根据广告名称获取广告内容 参数: $ad_name:广告名称 返回: 格式 string,广告内容 示例: <?php $ad_name='top_ad'; $ad_content=sp_getad('top_ad'); //获取广告内容 echo $ad_content; //输出广告内容 模板中用法: <div> {:sp_getad('top

  • 在我的android项目中实现谷歌广告后,我面临着应用程序大小的大幅增加,对我来说,这是由com.google.android.gms.internal.ads.中的许多类引起的。有什么方法可以用更低的内存来实现广告吗?

  • 使用Google Ads API“获取帐户层次结构”获取所有帐户。https://developers.google.com/google-ads/api/docs/account-management/get-account-hierarchy?hl=en 发现取消的帐户未被检索。 我确实看到用户界面和Google Data Studio连接器中报告了被取消的账户。我猜两者都使用旧的AdWord