Reputation: 751
I have the campaign created with responsive search ads, and have an Affinity Audience im passing here but cannot figure out the right way to do this. The function takes in an audience_type which is either in_market or affinity and is supposed to attach it to my campaign.
The function:
def attach_audience_to_campaign(client, customer_id, campaign_id, audience_id, audience_type):
"""
Attach an audience to a campaign. The audience type must be either "in_market" or "affinity".
:param client: The initialized Google Ads client.
:param customer_id: The customer ID.
:param campaign_id: The campaign ID.
:param audience_id: The audience ID.
:param audience_type: A string indicating the audience type ("in_market" or "affinity").
:return: The resource name of the created campaign criterion.
"""
campaign_criterion_service = client.get_service("CampaignCriterionService")
campaign_criterion_operation = client.get_type("CampaignCriterionOperation")
campaign_criterion = campaign_criterion_operation.create
# Set the campaign resource.
campaign_criterion.campaign = f"customers/{customer_id}/campaigns/{campaign_id}"
# Set the correct field based on the audience type.
if audience_type.lower() == "in_market":
campaign_criterion.in_market.in_market_category = f"customers/{customer_id}/inMarketCategories/{audience_id}"
elif audience_type.lower() == "affinity":
# Create a UserInterestInfo object and set its resource_name
user_interest_info = client.get_type("UserInterestInfo")
user_interest_info.resource_name = f"customers/{customer_id}/userInterests/{audience_id}"
campaign_criterion.user_interest = user_interest_info
else:
raise ValueError("Unsupported audience type. Use 'in_market' or 'affinity'.")
try:
response = campaign_criterion_service.mutate_campaign_criteria(
customer_id=str(customer_id),
operations=[campaign_criterion_operation]
)
return response.results[0].resource_name
except GoogleAdsException as ex:
logging.error(f"Failed to attach audience: {ex}")
return None
The error:
AttributeError: Unknown field for UserInterestInfo: resource_name
2025-02-22 11:01:45,958 - {"status": "error", "message": "An unexpected error
occurred: Unknown field for UserInterestInfo: resource_name"}
Can anyone tell me what im doing wrong here and hopefully how to fix it?
Upvotes: 0
Views: 9