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.
query = f"SELECT detailed_demographic.id, detailed_demographic.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) |
https://developers.google.com/google-ads/api/reference/rpc/v17/DetailedDemographic
It is supported for Search Campaigns at least in the UI:
https://support.google.com/google-ads/answer/2497941?hl=en
Here is the CSV with the segment names:
https://storage.googleapis.com/support-kms-prod/A9BBxm08pGU5fgtqucYqiEIUOqlhs9C8vMWR
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/adwords-api/a0456c24-b112-459b-ad55-97d170f61dabn%40googlegroups.com.