Hi all,
I hope everybody is doing find.
I am trying to create new audience segment via google ads api, but it returns following error:
{
"message": "com.google.ads.googleads.v10.errors.GoogleAdsException: errors {\n error_code {\n string_format_error: INVALID_FORMAT\n }\n message: \"The input string value is invalid for the associated field.\"\n trigger {\n string_value: \"\"\n }\n location {\n field_path_elements {\n field_name: \"operations\"\n index: 0\n }\n field_path_elements {\n field_name: \"create\"\n }\n field_path_elements {\n field_name: \"rule_based_user_list\"\n }\n field_path_elements {\n field_name: \"expression_rule_user_list\"\n }\n field_path_elements {\n field_name: \"rule\"\n }\n field_path_elements {\n field_name: \"rule_item_groups\"\n index: 0\n }\n field_path_elements {\n field_name: \"rule_items\"\n index: 0\n }\n field_path_elements {\n field_name: \"name\"\n }\n }\n}\nrequest_id: \"r1S1AGdqZiXblIYEcFZMEA\"\n",
"status": "INTERNAL_SERVER_ERROR",
"timestamp": "2023-02-13T10:33:51.601+00:00"
}
Following is the code i using to create Audience segment, it's working fine if i provide segment name without space, ex: "dynx_category" but with name "Page URL" it give error:
public class AudienceSegmentService
{
private final Logger LOGGER = LoggerFactory.getLogger(AudienceSegmentService.class);
private final GoogleAdsServiceFactory googleAdsServiceFactory;
private final GoogleAdsContextSettingsUtil context;
@Autowired
public AudienceSegmentService(GoogleAdsServiceFactory googleAdsServiceFactory, GoogleAdsContextSettingsUtil context)
{
this.googleAdsServiceFactory = googleAdsServiceFactory;
this.context = context;
}
public AudienceSegmentDto create(long accountId, AudienceSegmentDto audienceSegmentDto)
{
if (audienceSegmentDto == null)
{
return audienceSegmentDto;
}
LOGGER.info("Audience Segment input: {}", audienceSegmentDto.toString());
UserListRuleItemGroupInfo.Builder ruleItemGroupInfoBuilder = UserListRuleItemGroupInfo.newBuilder();
Map<String, String> rulesParam = audienceSegmentDto.getRulesParam();
rulesParam.forEach((key, value) -> ruleItemGroupInfoBuilder.addRuleItems(createContainsRule(key, value)));
// Specifies that the user list targets visitors of a page based on the provided rule.
ExpressionRuleUserListInfo expressionRuleUserListInfo = ExpressionRuleUserListInfo.newBuilder()
.setRule(UserListRuleInfo.newBuilder().addRuleItemGroups(ruleItemGroupInfoBuilder.build()).setRuleType(
UserListRuleTypeEnum.UserListRuleType.OR_OF_ANDS).build())
.build();
// Defines a representation of a user list that is generated by a rule.
RuleBasedUserListInfo ruleBasedUserListInfo = RuleBasedUserListInfo.newBuilder()
.setPrepopulationStatus(UserListPrepopulationStatusEnum.UserListPrepopulationStatus.REQUESTED)
.setExpressionRuleUserList(expressionRuleUserListInfo)
.build();
// Creates the user list.
UserList userList = UserList.newBuilder()
.setName(audienceSegmentDto.getName())
.setMembershipStatus(UserListMembershipStatusEnum.UserListMembershipStatus.OPEN)
.setMembershipLifeSpan(audienceSegmentDto.getDuration())
.setRuleBasedUserList(ruleBasedUserListInfo)
.build();
// Creates the operation.
UserListOperation operation = UserListOperation.newBuilder().setCreate(userList).build();
try
{
MutateUserListsRequest.Builder builder = MutateUserListsRequest.newBuilder();
builder.addOperations(operation);
builder.setCustomerId(accountId + "");
MutateUserListsResponse response = googleAdsServiceFactory.userListServiceClient()
.mutateUserListsCallable()
.call(builder.build(), context.defaultContext());
String resourceName = response.getResults(0).getResourceName();
audienceSegmentDto.setResourceName(resourceName);
LOGGER.info("Created: " + audienceSegmentDto.getName() + " Resource: " + resourceName);
}
catch (ApiException e)
{
LOGGER.error("EXCEPTION: ", e);
GoogleAdsOperationsUtil.logApiErrors(e, LOGGER);
throw new AudienceSegmentException(e);
}
return audienceSegmentDto;
}
private UserListRuleItemInfo createContainsRule(String name, String value)
{
return UserListRuleItemInfo.newBuilder()
.setName(name)
.setStringRuleItem(UserListStringRuleItemInfo.newBuilder()
.setOperator(UserListStringRuleItemOperatorEnum.UserListStringRuleItemOperator.CONTAINS)
.setValue(value)
.build())
.build();
}
}
I hope someone will be able to help, thanks :)