use of com.google.api.ads.adwords.axis.v201809.cm.Selector 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());
}
}
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Selector in project googleads-java-lib by googleads.
the class GetCampaignTargetingCriteria 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 CampaignService.
CampaignCriterionServiceInterface campaignCriterionService = adWordsServices.get(session, CampaignCriterionServiceInterface.class);
int offset = 0;
// Create selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(CampaignCriterionField.CampaignId, CampaignCriterionField.Id, CampaignCriterionField.CriteriaType, CampaignCriterionField.PlatformName, CampaignCriterionField.LanguageName, CampaignCriterionField.LocationName, CampaignCriterionField.KeywordText).in(CampaignCriterionField.CriteriaType, "KEYWORD", "LANGUAGE", "LOCATION", "PLATFORM").offset(0).limit(PAGE_SIZE).build();
CampaignCriterionPage page = null;
do {
page = campaignCriterionService.get(selector);
if (page.getEntries() != null) {
// Display campaigns.
for (CampaignCriterion campaignCriterion : page.getEntries()) {
System.out.printf("Campaign criterion with campaign ID %d, criterion ID %d, " + "and type '%s' was found.%n", campaignCriterion.getCampaignId(), campaignCriterion.getCriterion().getId(), campaignCriterion.getCriterion().getCriterionType());
}
} else {
System.out.println("No campaign criteria were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
} while (offset < page.getTotalNumEntries());
}
use of com.google.api.ads.adwords.axis.v201809.cm.Selector in project googleads-java-lib by googleads.
the class SelectorBuilderTest method testPredicatesBuild.
/**
* Tests the predicate build for the ID field.
*/
@Test
public void testPredicatesBuild() {
SelectorBuilder builder = new SelectorBuilder();
builder = builder.equalsId(10L);
Selector selector = builder.build();
assertNotNull(selector.getPredicates());
assertEquals(1, selector.getPredicates().length);
Predicate predicate = selector.getPredicates()[0];
assertEquals("Id", predicate.getField());
assertEquals(PredicateOperator.EQUALS, predicate.getOperator());
assertNotNull(predicate.getValues());
assertEquals(1, predicate.getValues().length);
assertEquals("10", predicate.getValues()[0]);
checkUtilitiesState(false);
}
use of com.google.api.ads.adwords.axis.v201809.cm.Selector in project googleads-java-lib by googleads.
the class SelectorBuilderTest method testUniqueInternalOrderByState.
/**
* Tests that the built selectors don't share internal OrderBy state.
*/
@Test
public void testUniqueInternalOrderByState() {
SelectorBuilder builder = new SelectorBuilder();
Selector selectorOne = builder.orderAscBy(CampaignField.Id).build();
Selector selectorTwo = builder.build();
OrderBy orderTwo = selectorTwo.getOrdering(0);
orderTwo.setField("Status");
assertEquals("Id", selectorOne.getOrdering(0).getField());
assertEquals("Status", selectorTwo.getOrdering(0).getField());
checkUtilitiesState(true);
}
use of com.google.api.ads.adwords.axis.v201809.cm.Selector in project googleads-java-lib by googleads.
the class SelectorBuilderTest method testFieldsBuild.
/**
* Tests the adding of a new field.
*/
@Test
public void testFieldsBuild() {
SelectorBuilder builder = new SelectorBuilder();
builder = builder.fields("Id", "Name", "Status", "StartDate", "EndDate", "ServingStatus", "Settings");
Selector selector = builder.build();
assertEquals(7, selector.getFields().length);
assertEquals("Id", selector.getFields()[0]);
assertEquals("Name", selector.getFields()[1]);
assertEquals("Status", selector.getFields()[2]);
assertEquals("StartDate", selector.getFields()[3]);
assertEquals("EndDate", selector.getFields()[4]);
assertEquals("ServingStatus", selector.getFields()[5]);
assertEquals("Settings", selector.getFields()[6]);
selector = builder.fields(CampaignField.Id, CampaignField.Name, CampaignField.Status, CampaignField.StartDate, CampaignField.EndDate, CampaignField.ServingStatus, CampaignField.Settings, CampaignField.Id, CampaignField.Amount).build();
assertEquals(8, selector.getFields().length);
assertEquals("Id", selector.getFields()[0]);
assertEquals("Name", selector.getFields()[1]);
assertEquals("Status", selector.getFields()[2]);
assertEquals("StartDate", selector.getFields()[3]);
assertEquals("EndDate", selector.getFields()[4]);
assertEquals("ServingStatus", selector.getFields()[5]);
assertEquals("Settings", selector.getFields()[6]);
assertEquals("Amount", selector.getFields()[7]);
checkUtilitiesState(true);
}
Aggregations