from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
import os
def main():
# Get the path to the google-ads.yaml file
script_dir = os.path.dirname(os.path.abspath(__file__))
yaml_path = os.path.join(script_dir, "google-ads.yaml")
# Load Google Ads client from the specified configuration file
client = GoogleAdsClient.load_from_storage(yaml_path)
# Retrieve the customer_id from the YAML configuration
customer_id = client.login_customer_id
# Get the service for querying campaigns
google_ads_service = client.get_service("GoogleAdsService")
# Campaign ID to search for
campaign_id = 21694145989
# Create a query to get the campaign details including specific status fields
query = f"""
SELECT
campaign.status,
campaign.primary_status,
campaign.primary_status_reasons,
campaign.serving_status
FROM
campaign
WHERE
"""
try:
# Execute the query and retrieve the results
response = google_ads_service.search(customer_id=customer_id, query=query)
# Initialize a flag to check if any results were found
results_found = False
# Output campaign details
print(f"Campaign Status Details for ID {campaign_id}:")
for row in response:
results_found = True
campaign = row.campaign
print(f"Campaign Name: {campaign.name}")
print(f"Campaign ID: {campaign.id}")
print(f"Campaign Primary Status: {campaign.primary_status}")
if campaign.primary_status_reasons:
print("Campaign Primary Status Reasons:")
for reason in campaign.primary_status_reasons:
print(f" - {reason}")
else:
print("Campaign Primary Status Reasons: None")
if not results_found:
print(f"No campaign found with ID {campaign_id}")
except GoogleAdsException as ex:
print(f"Google Ads API Error: {ex}")
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}")
if __name__ == "__main__":
main()