Search in sources :

Example 1 with Member

use of com.google.api.ads.adwords.axis.v201809.rm.Member in project googleads-java-lib by googleads.

the class AddCrmBasedUserList method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 * @throws UnsupportedEncodingException if encoding the hashed email failed.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException, UnsupportedEncodingException {
    // Get the UserListService.
    AdwordsUserListServiceInterface userListService = adWordsServices.get(session, AdwordsUserListServiceInterface.class);
    // Create a user list.
    CrmBasedUserList userList = new CrmBasedUserList();
    userList.setName("Customer relationship management list #" + System.currentTimeMillis());
    userList.setDescription("A list of customers that originated from email addresses");
    // CRM-based user lists can use a membershipLifeSpan of 10000 to indicate unlimited; otherwise
    // normal values apply.
    userList.setMembershipLifeSpan(30L);
    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);
    // Hash normalized email addresses based on SHA-256 hashing algorithm.
    List<Member> members = new ArrayList<>(EMAILS.size());
    for (String email : EMAILS) {
        String normalizedEmail = toNormalizedString(email);
        Member member = new Member();
        member.setHashedEmail(toSHA256String(normalizedEmail));
        members.add(member);
    }
    String firstName = "John";
    String lastName = "Doe";
    String countryCode = "US";
    String zipCode = "10011";
    AddressInfo addressInfo = new AddressInfo();
    // First and last name must be normalized and hashed.
    addressInfo.setHashedFirstName(toSHA256String(toNormalizedString(firstName)));
    addressInfo.setHashedLastName(toSHA256String(toNormalizedString(lastName)));
    // Country code and zip code are sent in plaintext.
    addressInfo.setCountryCode(countryCode);
    addressInfo.setZipCode(zipCode);
    Member memberByAddress = new Member();
    memberByAddress.setAddressInfo(addressInfo);
    members.add(memberByAddress);
    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 });
    // 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", EMAILS.size(), userListResult.getName(), userListResult.getId());
    }
}
Also used : AdwordsUserListServiceInterface(com.google.api.ads.adwords.axis.v201809.rm.AdwordsUserListServiceInterface) ArrayList(java.util.ArrayList) UserListOperation(com.google.api.ads.adwords.axis.v201809.rm.UserListOperation) UserListReturnValue(com.google.api.ads.adwords.axis.v201809.rm.UserListReturnValue) AddressInfo(com.google.api.ads.adwords.axis.v201809.rm.AddressInfo) CrmBasedUserList(com.google.api.ads.adwords.axis.v201809.rm.CrmBasedUserList) MutateMembersOperation(com.google.api.ads.adwords.axis.v201809.rm.MutateMembersOperation) MutateMembersOperand(com.google.api.ads.adwords.axis.v201809.rm.MutateMembersOperand) UserList(com.google.api.ads.adwords.axis.v201809.rm.UserList) CrmBasedUserList(com.google.api.ads.adwords.axis.v201809.rm.CrmBasedUserList) Member(com.google.api.ads.adwords.axis.v201809.rm.Member) MutateMembersReturnValue(com.google.api.ads.adwords.axis.v201809.rm.MutateMembersReturnValue)

Aggregations

AddressInfo (com.google.api.ads.adwords.axis.v201809.rm.AddressInfo)1 AdwordsUserListServiceInterface (com.google.api.ads.adwords.axis.v201809.rm.AdwordsUserListServiceInterface)1 CrmBasedUserList (com.google.api.ads.adwords.axis.v201809.rm.CrmBasedUserList)1 Member (com.google.api.ads.adwords.axis.v201809.rm.Member)1 MutateMembersOperand (com.google.api.ads.adwords.axis.v201809.rm.MutateMembersOperand)1 MutateMembersOperation (com.google.api.ads.adwords.axis.v201809.rm.MutateMembersOperation)1 MutateMembersReturnValue (com.google.api.ads.adwords.axis.v201809.rm.MutateMembersReturnValue)1 UserList (com.google.api.ads.adwords.axis.v201809.rm.UserList)1 UserListOperation (com.google.api.ads.adwords.axis.v201809.rm.UserListOperation)1 UserListReturnValue (com.google.api.ads.adwords.axis.v201809.rm.UserListReturnValue)1 ArrayList (java.util.ArrayList)1