googleads/google-ads-python

get_ad_groups.py

shivam-2002 opened this issue · 2 comments

There isn't any file get_ad_groups.py. but It is mentioned "https://github.com/googleads/google-ads-python/blob/main/examples/basic_operations/add_ad_groups.py#L17". Without having file how can we execute it.

Hi @shivam-2002 thanks for pointing this out. This looks like a typo, there is indeed no example called get_ad_groups. I'll remove that line, meanwhile if you want to retrieve ad groups see the ad_group resource.

To be more specific, if you want to get all the ad groups for an account, you can follow the get_campaigns example, but use the below query instead:

SELECT
  ad_group.id,
  ad_group.name
FROM ad_group

Than You Ben for your kind attention, I already have written the code for above. If you want you can attach a new file get_ad_groups.py.

import argparse
import sys

from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException


def main(client, customer_id):
    # Get the GoogleAdsService client.
    googleads_service = client.get_service("GoogleAdsService")
    query = f"""
        SELECT
          ad_group.id,
          ad_group.name
        FROM ad_group
        """

    search_response = googleads_service.search(
        customer_id=customer_id, query=query
    )

    for res in search_response:
        print(res)


if __name__ == "__main__":
    # GoogleAdsClient will read the google-ads.yaml configuration file in the
    # home directory if none is specified.
    googleads_client = GoogleAdsClient.load_from_storage(version="v15")

    parser = argparse.ArgumentParser(
        description="Updates the audience target restriction of a given ad "
        "group to bid only."
    )
    # The following argument(s) should be provided to run the example.
    parser.add_argument(
        "-c",
        "--customer_id",
        type=str,
        required=True,
        help="The Google Ads customer ID.",
    )

    args = parser.parse_args()

    try:
        main(googleads_client, args.customer_id)
    except GoogleAdsException as ex:
        print(
            f'Request with ID "{ex.request_id}" failed with status '
            f'"{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)
        
        
        

Hi @shivam-2002 thanks for pointing this out. This looks like a typo, there is indeed no example called get_ad_groups. I'll remove that line, meanwhile if you want to retrieve ad groups see the ad_group resource.

To be more specific, if you want to get all the ad groups for an account, you can follow the get_campaigns example, but use the below query instead:

SELECT
  ad_group.id,
  ad_group.name
FROM ad_group