Part 8 - Create an Ad Group

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

Once again we'll follow the same set of steps that we did in the last two sections. First update the calls to _create_ad_group and its method signature.

First, update the method signature to accept the Google Ads client and a campaign (not just a campaign ID), and to return an ad group:

8.0.0: Updating the Call to _create_ad_group (Google Ads API)
ad_group = _create_ad_group(googleads_client, campaign)
      

Update the method signature so it accepts a campaign:

8.0.1: Updating the _create_ad_group Method Signature (Google Ads API)
def _create_ad_group(client, campaign):
      

Lastly update the calls to _create_text_ads and _create_keywords to pass the ad group's ID as the second parameter:

8.0.2: Updating the Calls to _create_text_ads and _create_keywords (AdWords API)
_create_text_ads(adwords_client, ad_group.id)
_create_keywords(adwords_client, ad_group.id, KEYWORDS_TO_ADD)
      

Part 8.1 - Retrieve the necessary service client

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

8.1.0: Retrieving the AdGroupService (AdWords API)
ad_group_service = client.GetService("AdGroupService", "v201809")
      

should be changed to:

8.1.1: Retrieving the AdGroupService (Google Ads API)
ad_group_service = client.get_type("AdGroupService")
      

Part 8.2 - Create and configure a mutate operation

8.2.0: Initializing and Configuring an Ad Group Operation (AdWords API)
ad_group = {
    "name": "Earth to Mars Cruise #{}".format(uuid.uuid4()),
    "campaignId": campaign_id,
    "status": "ENABLED",
    "biddingStrategyConfiguration": {
        "bids": [
            {
                # The 'xsi_type' field enables you to specify the xsi:type of the
                # object being created. It's only necessary when you must provide
                # an explicit type that the client library can't infer.
                "xsi_type": "CpcBid",
                "bid": {"microAmount": 10000000},
            }
        ]
    },
    "adGroupAdRotationMode": "OPTIMIZE",
}

adgroup_operations = [{"operator": "ADD", "operand": ad_group}]
      

should be changed to:

8.2.1: Initializing and Configuring an Ad Group Operation (Google Ads API)
operation = client.get_type("AdGroupOperation")
ad_group = operation.create
ad_group.name = f"Earth to Mars Cruises #{uuid.uuid4()}"
ad_group.campaign = campaign.resource_name
ad_group.status = client.enums.AdGroupStatusEnum.ENABLED
ad_group.type = client.enums.AdGroupTypeEnum.SEARCH_STANDARD
ad_group.cpc_bid_micros = 10000000
      

Part 8.3 - Send the operation to the API in a mutate request

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

should be changed to:

8.3.1: Submitting an Ad Group Mutate Request (Google Ads API)
response = adgroup_service.mutate_ad_groups(
    customer_id=_CUSTOMER_ID, operations=[operation]
)
      

Part 8.4 - Get information about the new object

8.4.0: Parsing an Ad Group Mutate Response (AdWords API)
created_ad_group = results["value"][0]
      

should be changed to:

8.4.1: Parsing an Ad Group Mutate Response (Google Ads API)
ad_group_resource_name = response.results[0].resource_name
      

Then use the _search_google_ads method with the below query to retrieve and return the ad group:

8.4.2: Using Google Ads API Reporting to Retrieve the Full Ad Group Object
query = f"""
    SELECT
      ad_group.id,
      ad_group.name,
      ad_group.resource_name
    FROM ad_group
    WHERE ad_group.resource_name = '{resource_name}'"""

googleads_row = _search_google_ads(client, query)

return googleads_row.ad_group
      

Part 8.5 - Finished _create_ad_group method

8.5.0: Complete _create_ad_group Method (Google Ads API)
def _create_ad_group(client, campaign):
    """Creates a new ad group and returns a subset of its fields.

    Only the fields selected in the below GAQL query will be present on
    the returned campaign.

    Args:
        client: An instance of the GoogleAdsClient class.
        campaign: A Campaign instance.

    Returns:
        (AdGroup) The newly created ad group with a subset of its fields.
    """
    ad_group_service = client.get_type("AdGroupService")

    operation = client.get_type("AdGroupOperation")
    ad_group = operation.create
    ad_group.name = f"Earth to Mars Cruises #{uuid.uuid4()}"
    ad_group.campaign = campaign.resource_name
    ad_group.status = client.enums.AdGroupStatusEnum.ENABLED
    ad_group.type = client.enums.AdGroupTypeEnum.SEARCH_STANDARD
    ad_group.cpc_bid_micros = 10000000

    response = adgroup_service.mutate_ad_groups(
        customer_id=_CUSTOMER_ID, operations=[operation]
    )

    ad_group_resource_name = response.results[0].resource_name

    query = f"""
    SELECT
      ad_group.id,
      ad_group.name,
      ad_group.resource_name
    FROM ad_group
    WHERE ad_group.resource_name = '{resource_name}'"""

    googleads_row = _search_google_ads(client, query)

    return googleads_row.ad_group
      

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

python create_complete_campaign_adwords_api_only.py