use of com.google.api.ads.adwords.axis.v201809.rm.BasicUserList in project googleads-java-lib by googleads.
the class AddAudience 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.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException {
// Get the UserListService.
AdwordsUserListServiceInterface userListService = adWordsServices.get(session, AdwordsUserListServiceInterface.class);
// Get the ConversionTrackerService.
ConversionTrackerServiceInterface conversionTrackerService = adWordsServices.get(session, ConversionTrackerServiceInterface.class);
// 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 });
// You can optionally provide these field(s).
userList.setStatus(UserListMembershipStatus.OPEN);
// Create operations.
UserListOperation operation = new UserListOperation();
operation.setOperand(userList);
operation.setOperator(Operator.ADD);
UserListOperation[] operations = new UserListOperation[] { operation };
// Add user list.
UserListReturnValue result = userListService.mutate(operations);
// Display results.
// Capture the ID(s) of the conversion.
List<String> conversionIds = new ArrayList<>();
for (UserList userListResult : result.getValue()) {
if (userListResult instanceof BasicUserList) {
BasicUserList remarketingUserList = (BasicUserList) userListResult;
for (UserListConversionType userListConversionType : remarketingUserList.getConversionTypes()) {
conversionIds.add(userListConversionType.getId().toString());
}
}
}
// Create predicate and selector.
Selector selector = new SelectorBuilder().fields("Id", "GoogleGlobalSiteTag", "GoogleEventSnippet").in(AdwordsUserListField.Id, conversionIds.toArray(new String[0])).build();
// Get all conversion trackers.
Map<Long, AdWordsConversionTracker> conversionTrackers = new HashMap<Long, AdWordsConversionTracker>();
ConversionTrackerPage page = conversionTrackerService.get(selector);
if (page != null && page.getEntries() != null) {
conversionTrackers = Arrays.stream(page.getEntries()).collect(Collectors.toMap(conversionTracker -> conversionTracker.getId(), conversionTracker -> (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 remarketingUserList = (BasicUserList) userListResult;
for (UserListConversionType userListConversionType : remarketingUserList.getConversionTypes()) {
ConversionTracker conversionTracker = conversionTrackers.get(userListConversionType.getId());
System.out.printf("Google global site tag:%n%s%n%n", conversionTracker.getGoogleGlobalSiteTag());
System.out.printf("Google event snippet:%n%s%n%n", conversionTracker.getGoogleEventSnippet());
}
}
}
}
Aggregations