Hey there I am getting FaultMessage: Too Many when adding more than 4 ads in the ad group using google sdk python and FaultMessage: Too few when adding less than 3 ads in the ad group using google sdk python.
import argparse
import sys
import uuid
import pandas as pd
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
def main(client, customer_id, df):
"""Creates ad group ads from a dataframe.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID.
df: a dataframe containing ad group names, headlines, and descriptions.
"""
ad_group_ad_service = client.get_service("AdGroupAdService")
# Group the rows by ad group
grouped = df.groupby('Ad Group')
for ad_group_name, group in grouped:
print(f"Creating ads for ad group: {ad_group_name}")
ad_group_ad_operation = client.get_type("AdGroupAdOperation")
ad_group_ad = ad_group_ad_operation.create
ad_group_ad.status = client.enums.AdGroupAdStatusEnum.ENABLED
ad_group_ad.ad_group = ad_group_name
# Set responsive search ad info
# Collect headlines and descriptions
headlines = []
descriptions = []
for _, row in group.iterrows():
headline = row['Headline']
description = row['Description']
headlines.append(create_ad_text_asset(client, headline))
descriptions.append(create_ad_text_asset(client, description))
# Ensure there are at least 3 headlines and 2 descriptions
while len(headlines) < 3:
headlines.append(create_ad_text_asset(client, "Default Headline"))
while len(descriptions) < 2:
descriptions.append(create_ad_text_asset(client, "Default Description"))
# Add headlines and descriptions to the ad
ad_group_ad.ad.responsive_search_ad.headlines.extend(headlines)
ad_group_ad.ad.responsive_search_ad.descriptions.extend(descriptions)
# Send a request to the server to add a responsive search ad
ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads(
customer_id=customer_id, operations=[ad_group_ad_operation]
)
for result in ad_group_ad_response.results:
print(
f"Created responsive search ad with resource name "
f'"{result.resource_name}".'
)
def create_ad_text_asset(client, text, pinned_field=None):
"""Create an AdTextAsset.
Args:
client: an initialized GoogleAdsClient instance.
text: text for headlines and descriptions.
pinned_field: to pin a text asset so it always shows in the ad.
Returns:
An AdTextAsset.
"""
ad_text_asset = client.get_type("AdTextAsset")
ad_text_asset.text = text
if pinned_field:
ad_text_asset.pinned_field = pinned_field
return ad_text_asset
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=("Creates Responsive Search Ads for specified customer.")
)
# The following argument(s) should be provided to run the example.
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
args = parser.parse_args()
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
googleads_client = GoogleAdsClient.load_from_storage(version="v17", path="./google-ads.yaml")
try:
# Load the dataframe from the provided CSV file
df = pd.read_csv("ads_df.csv")
main(
googleads_client,
args.customer_id,
df,
)
except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'Error with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)