How to add detailed_demographics as targeting for Search Campaign?

52 views
Skip to first unread message

Jens

unread,
Mar 14, 2025, 12:06:07 PM3/14/25
to Google Ads API and AdWords API Forum
Hi guys,

i am struggling with adding detailed_demographics to a search campaign: This is my code. Any ideas what I am doing wrong?

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 " \
f"WHERE detailed_demographic.name = '{detailed_demo_name}'"
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:
detailed_demo_id = row.detailed_demographic.id
logger.info(f"Found Detailed Demographic segment '{row.detailed_demographic.name}' with ID {detailed_demo_id}.")
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:


Google Ads API Forum Advisor

unread,
Mar 14, 2025, 4:51:58 PM3/14/25
to adwor...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads API support team.

To assist you further, could you please provide us with the following information :
  • Customer Id
  • Confirm if you are able to perform the same operaiton in UI. If yes, provide uncropped UI screenshot
  • Complete API logs (request and response with request-id and request header)
  • Elaborate your issue with the above data
If you are using a client library and haven't enabled the logging yet, I would request you to enable logging for the specific client library that you are using. You can refer to the guides Java, .NetPHPPythonRuby or Perl to enable logging at your end. For REST interface requests, you can enable logging via the curl command by using the -i flag.

You can send the details via Reply privately to the author option, or direct private reply to this email.

Thanks,
 
Google Logo Google Ads API Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5
[2025-03-14 20:51:04Z GMT] This message is in relation to case "ref:!00D1U01174p.!5004Q02vH9gF:ref" (ADR-00294237)



Reply all
Reply to author
Forward
0 new messages