// Create conversion type (tag).
UserListConversionType conversionType = new UserListConversionType();
conversionType.setName("Mars cruise customers #" + System.currentTimeMillis());
// Create remarketing user list.
BasicUserList userList = new BasicUserList();
userList.setName("Mars cruise customers #" + System.currentTimeMillis());
userList.setDescription("A list of mars cruise customers in the last year");
userList.setMembershipLifeSpan(365L);
userList.setConversionTypes(new UserListConversionType[] {conversionType});
// Create operations.
UserListOperation operation = new UserListOperation();
operation.setOperand(userList);
operation.setOperator(Operator.ADD);
UserListOperation[] operations = new UserListOperation[] {operation};
// Add user list.
UserListReturnValue result = adwordsUserListService.mutate(operations);
// Display results.
if (result != null && result.getValue() != null) {
// Capture the ID(s) of the conversion.
List<String> conversionIds = new ArrayList<String>();
for (UserList userListResult : result.getValue()) {
if (userListResult instanceof BasicUserList) {
BasicUserList remarketingAdwordsUserList = (BasicUserList) userListResult;
for (UserListConversionType userListConversionType :
remarketingAdwordsUserList.getConversionTypes()) {
conversionIds.add(userListConversionType.getId().toString());
}
}
}
// Create predicate and selector.
Predicate predicate = new Predicate();
predicate.setField("Id");
predicate.setOperator(PredicateOperator.IN);
predicate.setValues(conversionIds.toArray(new String[0]));
Selector selector = new Selector();
selector.setFields(new String[] {"Id"});
selector.setPredicates(new Predicate[] {predicate});
// Get all conversion trackers.
Map<Long, AdWordsConversionTracker> conversionTrackers =
new HashMap<Long, AdWordsConversionTracker>();
ConversionTrackerPage page = conversionTrackerService.get(selector);
if (page != null && page.getEntries() != null) {
for (ConversionTracker conversionTracker : page.getEntries()) {
conversionTrackers.put(conversionTracker.getId(),
(AdWordsConversionTracker) conversionTracker);
}
}
// Display user lists.
for (UserList userListResult : result.getValue()) {
System.out.printf("User list with name '%s' and id '%d' was added.\n",
userListResult.getName(), userListResult.getId());
// Display user list associated conversion code snippets.
if (userListResult instanceof BasicUserList) {
BasicUserList remarketingAdwordsUserList = (BasicUserList) userListResult;
for (UserListConversionType userListConversionType :
((BasicUserList) userListResult).getConversionTypes()) {
System.out.printf("Conversion type code snippet associated to the list:\n%s\n",
conversionTrackers.get(userListConversionType.getId()).getSnippet());
}