Share your feedback about the Google Ads (AdWords) API. Take the 2021 AdWords API and Google Ads API Annual Survey.

Part 9 - Create Text Ads

In this part we're going to rewrite the logic that creates ad group ads with the AdWords API so that it uses the Google Ads API.

Again, we'll follow the same set of steps that we did in the last few sections. The main differences here are that the _create_text_ads method sends multiple mutate operations instead of just one, and our methods will not return anything since the entities created aren't used in subsequent operations. This is a good opportunity to demonstrate the use of the response_content_type request header.

First update the method call and signature to accept an ad group instead of just an ad group ID:

9.0.0: Updating the Call to create_text_ads (Google Ads API)
_create_text_ads(googleads_client, ad_group)
      
9.0.1: Updating the _create_text_ads Method Signature (Google Ads API)
def _create_text_ads(client, ad_group):
      

Part 9.1 - Retrieve the necessary service client

According to the map of AdWords services to Google Ads services we will need to use the AdGroupAdService in the Google Ads API. So:

9.1.0: Retrieving the AdGroupAdService (AdWords API)
ad_group_ad_service = client.GetService("AdGroupAdService", "v201809")
      

should be changed to:

9.1.1: Retrieving the AdGroupAdService (Google Ads API)
ad_group_ad_service = client.get_type("AdGroupAdService")
      

Part 9.2 - Create and configure a mutate operation

Here, a loop creates multiple operations. We'll update the entire loop at once:

9.2.0: Initializing and Configuring Ad Group Ad Operations (AdWords API)
operations = []
for i in range(NUMBER_OF_ADS):
    operation = {
        "xsi_type": "AdGroupAd",
        "adGroupId": ad_group_id,
        # Additional properties (non-required).
        "status": "PAUSED",
        "ad": {
            "xsi_type": "ExpandedTextAd",
            "headlinePart1": "Cruise #{} to Mars".format(
                str(uuid.uuid4())[:8]
            ),
            "headlinePart2": "Best Space Cruise Line",
            "headlinePart3": "For Your Loved Ones",
            "description": "Buy your tickets now!",
            "description2": "Discount ends soon",
            "finalUrls": ["http://www.example.com/"],
        },
    }
    adgroup_operations = {"operator": "ADD", "operand": operation}
    operations.append(adgroup_operations)
      

should be changed to:

9.2.1: Initializing and configure Ad Group Ads Operations (Google Ads API)
operations = []
for i in range(0, NUMBER_OF_ADS):
    operation = client.get_type("AdGroupAdOperation")
    ad_group_ad_operation = operation.create
    ad_group_ad_operation.ad_group = ad_group.resource_name
    ad_group_ad_operation.status = client.enums.AdGroupAdStatusEnum.PAUSED
    ad_group_ad_operation.ad.expanded_text_ad.headline_part1 = (
        f"Cruise to Mars #{str(uuid.uuid4())[:4]}"
    )
    ad_group_ad_operation.ad.expanded_text_ad.headline_part2 = (
        "Best Space Cruise Line"
    )
    ad_group_ad_operation.ad.expanded_text_ad.description = (
        "Buy your tickets now!"
    )
    ad_group_ad_operation.ad.final_urls.append("http://www.example.com")
    operations.append(operation)
      

Part 9.3 - Send the operations to the API in a mutate request

9.3.0: Submitting an Ad Group Ad Mutate Request (AdWords API)
results = ad_group_service.mutate(operations)
      

should be changed to the following. Notice that we construct the request object outside of the mutate call. This is necessary in order to set optional header fields, namely the response_content_type header. We determine the name of the request object class by referring to the AdGroupAdService.MutateAdGroupAds method documentation.

9.3.1: Submitting an Ad Group Ad Mutate Request (Google Ads API)
request = client.get_type("MutateAdGroupAdsRequest")
request.customer_id = _CUSTOMER_ID
request.operations = operations
request.response_content_type = (
    client.enums.ResponseContentTypeEnum.MUTABLE_RESOURCE
)

response = ad_group_ad_service.mutate_ad_group_ads(request=request)
      

Part 9.4 - Print information about the new objects

This method only prints details about the newly created entities, so there is no need to return anything:

9.4.0: Parsing an Ad Group Ad Mutate Response (AdWords API)
for result in results["value"]:
    print(
        "Expanded text ad with ID {} and "
        "headline {}-{} {} was created".format(
            result["ad"]["id"],
            result["ad"]["headlinePart1"],
            result["ad"]["headlinePart2"],
            result["ad"]["headlinePart3"],
        )
    )
      

should be changed to:

9.4.1: Parsing an Ad Group Ad Mutate Response (Google Ads API)
for ad_group_ad in response.results:
    print(
        f"Created expanded text ad with ID {ad_group_ad.ad.id}, "
        f"status {ad_group_ad.ad.status} and headline "
        f"{ag_group_ad.ad.expanded_text_ad.headline_part1}."
        f"{ad_group_ad.ad.expanded_text_ad.headline_part2}"
    )
      

Part 9.5 - Finished _create_text_ads Method

9.5.0: Complete _create_text_ads Method (Google Ads API)
def _create_text_ads(client, ad_group):
    """Creates a set of new text ads and prints information about them.

    Args:
        client: An instance of the GoogleAdsClient class.
        ad_group: An AdGroup instance.
    """
    ad_group_ad_service = client.get_type("AdGroupAdService")

    operations = []
    for i in range(0, NUMBER_OF_ADS):
        operation = client.get_type("AdGroupAdOperation")
        ad_group_operation = operation.create
        ad_group_operation.ad_group = ad_group.resource_name
        ad_group_operation.status = client.enums.AdGroupAdStatusEnum.PAUSED
        ad_group_operation.ad.expanded_text_ad.headline_part1 = (
            f"Cruise to Mars #{str(uuid.uuid4())[:4]}"
        )
        ad_group_operation.ad.expanded_text_ad.headline_part2 = (
            "Best Space Cruise Line"
        )
        ad_group_operation.ad.expanded_text_ad.description = (
            "Buy your tickets now!"
        )
        ad_group_operation.ad.final_urls.append("http://www.example.com")
        operations.append(operation)

    request = client.get_type("MutateAdGroupAdsRequest")
    request.customer_id = _CUSTOMER_ID
    request.operations = operations
    request.response_content_type = (
        client.enums.ResponseContentTypeEnum.MUTABLE_RESOURCE
    )

    response = ad_group_ad_service.mutate_ad_group_ads(request=request)

    for ad_group_ad in response.results:
    print(
        f"Created expanded text ad with ID {ad_group_ad.ad.id}, "
        f"status {ad_group_ad.ad.status} and headline "
        f"{ag_group_ad.ad.expanded_text_ad.headline_part1}."
        f"{ad_group_ad.ad.expanded_text_ad.headline_part2}"
    )
      

Run the script to ensure everything is still working as expected:

python create_complete_campaign_adwords_api_only.py