我正在尝试创建一个再营销用户列表,使用 Google Ads Api 的 Java 实现将一些用户添加到其中。
自定义受众创建部分看起来不错,我可以看到它是在广告平台中创建的,但看起来用户并未包含在其中。
我正在发送一个带有 2000 个用户作为参数的 JsonArray,并在此函数中对其进行散列,并使用此示例作为参考。
我不确定我是否误解了文档,或者我是否以错误的方式包含了 userList 或类似的东西。
public JsonArray uploadJsonList(String customerId, JsonArray jsonUsers) throws Exception {
List<Member> members = new ArrayList<>();
JsonObject hashedObj = new JsonObject();
JsonArray arrayJsonHashed = new JsonArray();
for (JsonValue jsonValue : jsonUsers) {
JsonObject obj = jsonValue.asObject();
hashedObj = new JsonObject();
//Getting user data
String normalizedEmail = textUtils.toNormalizedString(obj.get("PESSOA_EMAIL1").toString());
String normalizedPhone = textUtils.toNormalizedString(obj.get("PESSOA_CELULAR").toString());
String normalizedId = obj.get("PESSOA_ID").toString();
normalizedId = removeFirstandLast(normalizedId);
//Hashing user data
hashedObj.add("pessoa_email1", textUtils.toSHA256String(normalizedEmail));
hashedObj.add("pessoa_celular", textUtils.toSHA256String(normalizedPhone));
hashedObj.add("pessoa_id",normalizedId);
arrayJsonHashed.add(hashedObj);
//Creating a member list
Member member = new Member();
member.setHashedEmail(textUtils.toSHA256String(normalizedEmail));
member.setHashedPhoneNumber(textUtils.toSHA256String(normalizedPhone));
members.add(member);
}
//starting ads services
AdWordsServices adWordsServices = new AdWordsServices();
Customer[] customers = getCustomers(adWordsServices, session);
session.setClientCustomerId(customerId);
AdwordsUserListServiceInterface userListService = adWordsServices.get(session, AdwordsUserListServiceInterface.class);
// Create a user list.
CrmBasedUserList userList = new CrmBasedUserList();
userList.setName("Test Remarketing Custom Audience - " + System.currentTimeMillis());
userList.setDescription("A list of customers that was readed from big query");
// CRM-based user lists can use a membershipLifeSpan of 10000 to indicate unlimited; otherwise
// normal values apply.
userList.setMembershipLifeSpan(100L);
userList.setUploadKeyType(CustomerMatchUploadKeyType.CONTACT_INFO);
// Create operation.
UserListOperation operation = new UserListOperation();
operation.setOperand(userList);
operation.setOperator(Operator.ADD);
// Add user list.
UserListReturnValue result = userListService.mutate(new UserListOperation[]{operation});
// Display user list.
UserList userListAdded = result.getValue(0);
System.out.printf(
"User list with name '%s' and ID %d was added.%n",
userListAdded.getName(), userListAdded.getId());
// Get user list ID.
Long userListId = userListAdded.getId();
// Create operation to add members to the user list based on email addresses.
MutateMembersOperation mutateMembersOperation = new MutateMembersOperation();
MutateMembersOperand operand = new MutateMembersOperand();
operand.setUserListId(userListId);
operand.setMembersList(members.toArray(new Member[members.size()]));
mutateMembersOperation.setOperand(operand);
mutateMembersOperation.setOperator(Operator.ADD);
// Add members to the user list based on email addresses.
MutateMembersReturnValue mutateMembersResult =
userListService.mutateMembers(new MutateMembersOperation[]{mutateMembersOperation});
// Display results.
// Reminder: it may take several hours for the list to be populated with members.
for (UserList userListResult : mutateMembersResult.getUserLists()) {
System.out.printf(
"%d email addresses were uploaded to user list with name '%s' and ID %d "
+ "and are scheduled for review.%n",
members.size(), userListResult.getName(), userListResult.getId());
}
return arrayJsonHashed;
}