Part 10 - Create Keyword Criteria

We're going to rewrite the logic that creates ad group criterion with the AdWords API so that it uses the Google Ads API.

The _create_keywords method is very similar to the _create_text_ads method in that it builds multiple mutate operations and prints the details of the newly created entities.

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

10.0.0: Updating the Call to _create_keywords (Google Ads API)
_create_keywords(googleads_client, ad_group, KEYWORDS_TO_ADD)
      
10.0.1: Updating the _create_keywords Method Signature (Google Ads API)
def _create_keywords(client, ad_group, keywords_to_add):
      

Part 10.1 - Retrieve the necessary service client

According to the map of AdWords services to Google Ads services, we'll need to use the AdGroupCriterionService in the Google Ads API, thus,

10.1.0: Retrieving the AdGroupCriterionService (AdWords API)
ad_group_criterion_service = client.GetService(
    "AdGroupCriterionService", "v201809"
)
      

should be changed to:

10.1.1: Retrieving the AdGroupCriterionService (Google Ads API)
ad_group_criterion_service = client.get_type("AdGroupCriterionService")
      

Part 10.2 - Create and configure a mutate operation

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

10.2.0: Initializing and Configuring Ad Group Criterion Operations (AdWords API)
operations = []
for keyword in keywords_to_add:
    operation = {
        "xsi_type": "BiddableAdGroupCriterion",
        "adGroupId": ad_group_id,
        "criterion": {
            "xsi_type": "Keyword",
            "text": keyword,
            "matchType": "BROAD",
        },
        "userStatus": "PAUSED",
        "finalUrls": [
            "http://www.example.com/mars/cruise/?kw={}".format(
                urllib.parse.quote(keyword)
            )
        ],
    }
    create_keyword = {"operator": "ADD", "operand": operation}
    operations.append(create_keyword)
      

should be changed to:

10.2.1: Initializing and Configuring AdGroupCriterionOperations (Google Ads API)
operations = []
for keyword in keywords_to_add:
    operation = client.get_type("AdGroupCriterionOperation")
    ad_group_criterion = operation.create
    ad_group_criterion.ad_group = ad_group.resource_name
    ad_group_criterion.status = (
        client.enums.AdGroupCriterionStatusEnum.ENABLED
    )
    ad_group_criterion.keyword.text = keyword
    ad_group_criterion.keyword.match_type = (
        client.enums.KeywordMatchTypeEnum.EXACT
    )
    operations.append(operation)
      

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

10.3.0: Submitting an Ad Group Criterion Mutate Request (AdWords API)
results = ad_group_criterion_service.mutate(operations)
      

should be changed to:

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

response = ad_group_criterion_service.mutate_ad_group_criteria(request=request)
      

Notice that we construct the request object outside of the mutate call. This is necessary in Python 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 AdGroupCriterionService.MutateAdGroupCriteria method documentation.

Part 10.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:

10.4.0: Parsing an Ad Group Criterion Mutate Response (AdWords API)
for result in results["value"]:
    print(
        "Keyword with ad group ID {}, keyword ID {}, text {} and match"
        "type {} was created".format(
            result["adGroupId"],
            result["criterion"]["id"],
            result["criterion"]["text"],
            result["criterion"]["matchType"],
        )
    )
      

should be changed to:

10.4.1: Parsing an Ad Group Criterion Mutate Response (Google Ads API)
for criterion in response.results:
    print(
        f"Keyword with text {criterion.keyword.text}, id="
        f"{criterion.criterion_id} and match type "
        f"{criterion.keyword.match_type} was created"
    )
      

Part 10.5 - Finished _create_keywords method

10.5.0: Complete _create_keywords Method (Google Ads API)
def _create_keywords(client, ad_group, keywords_to_add):
    """Creates a set of new keyword criteria and prints information about them.

    Args:
        client: An instance of the GoogleAdsClient class.
        ad_group: An AdGroup instance.
         Keywords_to_add: a list of keyword strings
    """
    ad_group_criterion_service = client.get_type("AdGroupCriterionService")

    operations = []
    for keyword in keywords_to_add:
        operation = client.get_type("AdGroupCriterionOperation")
        ad_group_criterion_operation = operation.create
        ad_group_criterion_operation.ad_group = ad_group.resource_name
        ad_group_criterion_operation.status = (
            client.enums.AdGroupCriterionStatusEnum.ENABLED
        )
        ad_group_criterion_operation.keyword.text = keyword
        ad_group_criterion_operation.keyword.match_type = (
            client.enums.KeywordMatchTypeEnum.EXACT
        )
        operations.append(operation)

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

    response = ad_group_criterion_service.mutate_ad_group_criteria(request=request)

    for criterion in response.results:
        print(
            f"Keyword with text {criterion.keyword.text}, id="
            f"{criterion.criterion_id} and match type "
            f"{criterion.keyword.match_type} was created"
        )
      

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

python create_complete_campaign_adwords_api_only.py