// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v201306;
import com.google.api.adwords.lib.AdWordsService;
import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.v201306.cm.AdWordsConversionTracker;
import com.google.api.adwords.v201306.cm.ConversionTracker;
import com.google.api.adwords.v201306.cm.ConversionTrackerPage;
import com.google.api.adwords.v201306.cm.ConversionTrackerServiceInterface;
import com.google.api.adwords.v201306.cm.Operator;
import com.google.api.adwords.v201306.cm.Predicate;
import com.google.api.adwords.v201306.cm.PredicateOperator;
import com.google.api.adwords.v201306.cm.Selector;
import com.google.api.adwords.v201306.rm.AdwordsUserListServiceInterface;
import com.google.api.adwords.v201306.rm.BasicUserList;
import com.google.api.adwords.v201306.rm.UserList;
import com.google.api.adwords.v201306.rm.UserListConversionType;
import com.google.api.adwords.v201306.rm.UserListOperation;
import com.google.api.adwords.v201306.rm.UserListReturnValue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This example adds a remarketing user list.
*
* Tags: AdwordsUserListService.mutate
*
* @author thagikura@google.com (Takeshi Hagikura)
*/
public class AddUserList {
public static void main(String[] args) {
try {
// Log SOAP XML request and response.
AdWordsServiceLogger.log();
// Get AdWordsUser from "~/adwords.properties".
AdWordsUser user = new AdWordsUser();
// Get the AdwordsUserListService.
AdwordsUserListServiceInterface adwordsUserListService =
user.getService(AdWordsService.V201306.ADWORDS_USER_LIST_SERVICE);
// Get the ConversionTrackerService.
ConversionTrackerServiceInterface conversionTrackerService =
user.getService(AdWordsService.V201306.CONVERSION_TRACKER_SERVICE);
// 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());
}
}
}
} else {
System.out.println("No user lists were added.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}