def add_detailed_demographic_targeting(client, customer_id, ad_group_id, detailed_demo_name):
"""
Adds a Detailed Demographic audience segment to the specified Search campaign ad group.
Args:
client: An initialized GoogleAdsClient for v18.
customer_id: The customer ID (str) of the Google Ads account.
ad_group_id: The ID (str or int) of the ad group to target.
detailed_demo_name: The name of the detailed demographic segment (e.g. "Parents of Preschoolers (4-5 years)").
"""
logger.info(f"Starting add_detailed_demographic_targeting with customer_id: {customer_id}, ad_group_id: {ad_group_id}, detailed_demo_name: {detailed_demo_name}")
# Obtain the GoogleAdsService to perform GAQL queries.
googleads_service = client.get_service("GoogleAdsService")
# GAQL query to find the detailed demographic segment ID by name.
f"FROM detailed_demographic " \
logger.debug(f"Executing GAQL query: {query}")
try:
search_response = googleads_service.search(customer_id=customer_id, query=query)
logger.debug("GAQL query executed successfully.")
except GoogleAdsException as ex:
error_details = handle_googleads_exception(ex, "search for detailed demographic")
logger.error(f"GAQL query failed: {error_details}")
raise Exception(f"GAQL query failed: {error_details}") # Handle API errors
detailed_demo_id = None
for row in search_response:
break
if detailed_demo_id is None:
logger.warning(f"Detailed demographic segment '{detailed_demo_name}' not found. Ensure the name is correct.")
return False
# Create an AdGroupCriterion operation to add the audience segment.
adgroup_criterion_service = client.get_service("AdGroupCriterionService")
criterion_operation = client.get_type("AdGroupCriterionOperation")
adgroup_criterion = criterion_operation.create
# Initialize the AdGroupCriterion
adgroup_criterion.ad_group = client.get_service("GoogleAdsService").ad_group_path(customer_id, ad_group_id)
adgroup_criterion.audience = client.get_type("AudienceInfo")
# Set the Audience resource name directly
adgroup_criterion.audience.audience = f"customers/{customer_id}/audiences/{detailed_demo_id}"
adgroup_criterion.status = client.enums.AdGroupCriterionStatusEnum.ENABLED
# Set the create operation
criterion_operation.create = adgroup_criterion
logger.info("AdGroupCriterion operation created.")
# Add the criterion to the ad group.
try:
response = adgroup_criterion_service.mutate_ad_group_criteria(customer_id=customer_id, operations=[criterion_operation])
new_criterion_resource_name = response.results[0].resource_name
logger.info(f"Successfully added detailed demographic targeting segment: '{detailed_demo_name}' "
f"to Ad Group ID {ad_group_id}. New criterion resource: {new_criterion_resource_name}")
return True
except GoogleAdsException as ex:
error_details = handle_googleads_exception(ex, "add detailed demographic targeting")
logger.error(f"Failed to add detailed demographic targeting: {error_details}")
return False
I am referring to detailed demographics like this:
Detailed Demographic Segments (Hierarchically Organized):
----------------------------------------------------------------------------------------------------------
| ID | Name | Parent |
|--------|---------------------------------------------|------------------------------------------------|
| 30038 | Education | (Root category) |
| 30032 | Current College Students | Education (30038) |
| 30001 | Highest Level of Educational Attainment | Education (30038) |
| 30005 | Advanced Degree | Highest Level of Educational Attainment (30001) |
| 30004 | Bachelor's Degree | Highest Level of Educational Attainment (30001) |
| 30002 | High School Graduate | Highest Level of Educational Attainment (30001) |
| 30018 | Employment | (Root category) |
| 30021 | Company Size | Employment (30018) |
| 30030 | Large Employer (250-10k Employees) | Company Size (30021) |
| 30028 | Small Employer (1-249 Employees) | Company Size (30021) |
| 30031 | Very Large Employer (10k+ Employees) | Company Size (30021) |
| 30020 | Industry | Employment (30018) |
| 30027 | Construction Industry | Industry (30020) |
| 30025 | Education Sector | Industry (30020) |
| 30041 | Financial Industry | Industry (30020) |
| 30022 | Healthcare Industry | Industry (30020) |
| 30024 | Hospitality Industry | Industry (30020) |
| 30026 | Manufacturing Industry | Industry (30020) |
| 30042 | Real Estate Industry | Industry (30020) |
| 30023 | Technology Industry | Industry (30020) |
| 30006 | Homeownership Status | (Root category) |
| 30007 | Homeowners | Homeownership Status (30006) |
| 30008 | Renters | Homeownership Status (30006) |
| 30015 | Marital Status | (Root category) |
| 30037 | In a Relationship | Marital Status (30015) |
| 30016 | Married | Marital Status (30015) |
| 30017 | Single | Marital Status (30015) |
| 30039 | Parental Status | (Root category) |
| 30009 | Parents | Parental Status (30039) |
| 30010 | Parents of Infants (0-1 years) | Parents (30009) |
| 30011 | Parents of Toddlers (1-3 years) | Parents (30009) |
| 30012 | Parents of Preschoolers (4-5 years) | Parents (30009) |
| 30013 | Parents of Grade-Schoolers (6-12 years) | Parents (30009) |
| 30014 | Parents of Teens (13-17 years) | Parents (30009) |
It is supported for Search Campaigns at least in the UI:
Here is the CSV with the segment names: