Skip to content

Instantly share code, notes, and snippets.

@Rauf-Kurbanov
Created November 21, 2023 08:40
Show Gist options
  • Save Rauf-Kurbanov/cd45db578370cd98e5f49bd895733686 to your computer and use it in GitHub Desktop.
Save Rauf-Kurbanov/cd45db578370cd98e5f49bd895733686 to your computer and use it in GitHub Desktop.
google quickstart
import google
from google.ads.googleads.client import GoogleAdsClient
# Load client configuration from `google-ads.yaml` file.
client = GoogleAdsClient.load_from_storage("google-ads.yaml")
# Initialize the Google Ads API service client.
google_ads_service = client.get_service("GoogleAdsService")
# Your Google Ads customer ID.
# customer_id = '8385313792' # test account
customer_id = '4043721935' # test manager account
# Create a query to select all campaign names.
query = """
SELECT
campaign.id,
campaign.name
FROM
campaign
ORDER BY
campaign.id
"""
# Issue a search request using streaming.
response = google_ads_service.search_stream(customer_id=customer_id, query=query)
try:
for batch in response:
for row in batch.results:
print(f"Found campaign with ID {row.campaign.id} and name '{row.campaign.name.value}'.")
except google.ads.google_ads.errors.GoogleAdsException as ex:
print(f"Request with ID '{ex.request_id}' failed with status "
f"'{ex.error.code().name}' and includes {len(ex.failure.errors)} 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}")
except Exception as ex:
print(f"Request failed with unknown error: {ex}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment