首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

AdWords -使用Python下载应用程序参与活动报告

要使用 Python 下载 Google AdWords(现称为 Google Ads)应用程序参与活动报告,你需要使用 Google Ads API。以下是一个详细的步骤指南,展示如何使用 Python 和 Google Ads API 下载应用程序参与活动报告。

前提条件

  1. Google Ads 账户:你需要一个 Google Ads 账户。
  2. Google Cloud 项目:你需要在 Google Cloud Console 中创建一个项目,并启用 Google Ads API。
  3. OAuth2 凭证:你需要设置 OAuth2 凭证,以便你的应用程序可以访问 Google Ads API。
  4. Google Ads API 客户端库:你需要安装 Google Ads API 的 Python 客户端库。

步骤

1. 安装 Google Ads API 客户端库

首先,安装 Google Ads API 的 Python 客户端库:

代码语言:javascript
复制
pip install google-ads

2. 设置 OAuth2 凭证

在 Google Cloud Console 中创建 OAuth2 凭证,并下载 credentials.json 文件。将该文件放在你的项目目录中。

3. 配置 Google Ads API 客户端

创建一个 google-ads.yaml 配置文件,内容如下:

代码语言:javascript
复制
developer_token: 'INSERT_YOUR_DEVELOPER_TOKEN_HERE'
client_id: 'INSERT_YOUR_OAUTH2_CLIENT_ID_HERE'
client_secret: 'INSERT_YOUR_OAUTH2_CLIENT_SECRET_HERE'
refresh_token: 'INSERT_YOUR_OAUTH2_REFRESH_TOKEN_HERE'
login_customer_id: 'INSERT_YOUR_LOGIN_CUSTOMER_ID_HERE'  # 可选

4. 下载应用程序参与活动报告

以下是一个示例代码,展示如何使用 Google Ads API 下载应用程序参与活动报告:

代码语言:javascript
复制
import sys
from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException

def main(client, customer_id):
    ga_service = client.get_service("GoogleAdsService", version="v11")

    query = """
        SELECT
            campaign.id,
            campaign.name,
            ad_group.id,
            ad_group.name,
            metrics.impressions,
            metrics.clicks,
            metrics.conversions,
            metrics.cost_micros
        FROM
            ad_group
        WHERE
            segments.date DURING LAST_30_DAYS
        """

    try:
        response = ga_service.search(customer_id=customer_id, query=query)

        for row in response:
            print(f"Campaign ID: {row.campaign.id}")
            print(f"Campaign Name: {row.campaign.name}")
            print(f"Ad Group ID: {row.ad_group.id}")
            print(f"Ad Group Name: {row.ad_group.name}")
            print(f"Impressions: {row.metrics.impressions}")
            print(f"Clicks: {row.metrics.clicks}")
            print(f"Conversions: {row.metrics.conversions}")
            print(f"Cost (micros): {row.metrics.cost_micros}")
            print("\n")

    except GoogleAdsException as ex:
        print(f"Request with ID '{ex.request_id}' failed with status '{ex.error.code().name}' and includes the following errors:")
        for error in ex.failure.errors:
            print(f"\tError with message '{error.message}'.")
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print(f"\t\tOn field: {field_path_element.field_name}")
        sys.exit(1)

if __name__ == "__main__":
    # 使用 google-ads.yaml 配置文件初始化 Google Ads 客户端
    google_ads_client = GoogleAdsClient.load_from_storage("google-ads.yaml")

    # 替换为你的客户 ID
    customer_id = "INSERT_YOUR_CUSTOMER_ID_HERE"
    main(google_ads_client, customer_id)

解释

  1. 安装客户端库:使用 pip install google-ads 安装 Google Ads API 的 Python 客户端库。
  2. 设置 OAuth2 凭证:在 Google Cloud Console 中创建 OAuth2 凭证,并下载 credentials.json 文件。
  3. 配置客户端:创建 google-ads.yaml 配置文件,包含你的开发者令牌、OAuth2 客户端 ID、客户端密钥和刷新令牌。
  4. 下载报告
    • 使用 GoogleAdsClient 初始化 Google Ads 客户端。
    • 使用 GoogleAdsService 运行查询,获取应用程序参与活动报告。
    • 处理并打印查询结果。

通过这种方式,你可以使用 Python 和 Google Ads API 下载应用程序参与活动报告。根据你的具体需求,你可以调整查询和处理逻辑。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 领券