use of com.google.api.ads.admanager.axis.v202205.CustomTargetingValue in project googleads-java-lib by googleads.
the class GetPredefinedCustomTargetingKeysAndValues method runExample.
/**
* Runs the example.
*
* @param adManagerServices 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(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
// Get all predefined custom targeting keys.
List<Long> customTargetingKeyIds = getPredefinedCustomTargetingKeyIds(adManagerServices, session);
// Create a statement to select custom targeting values.
StatementBuilder statementBuilder = new StatementBuilder().where("customTargetingKeyId = :customTargetingKeyId").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
int totalResultsCounter = 0;
for (Long customTargetingKeyId : customTargetingKeyIds) {
// Set the custom targeting key ID to select from.
statementBuilder.withBindVariableValue("customTargetingKeyId", customTargetingKeyId);
// Retrieve a small amount of custom targeting values at a time, paging through
// until all custom targeting values have been retrieved.
int totalResultSetSize = 0;
statementBuilder.offset(0);
do {
CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
// Print out some information for each custom targeting value.
totalResultSetSize = page.getTotalResultSetSize();
for (CustomTargetingValue customTargetingValue : page.getResults()) {
System.out.printf("%d) Custom targeting value with ID %d, name '%s', display name '%s', " + "and custom targeting key ID %d was found.%n", totalResultsCounter++, customTargetingValue.getId(), customTargetingValue.getName(), customTargetingValue.getDisplayName(), customTargetingValue.getCustomTargetingKeyId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
}
System.out.printf("Number of results found: %d%n", totalResultsCounter);
}
use of com.google.api.ads.admanager.axis.v202205.CustomTargetingValue in project googleads-java-lib by googleads.
the class UpdateCustomTargetingValues method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param customTargetingValueId the ID of the custom targeting value to update.
* @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(AdManagerServices adManagerServices, AdManagerSession session, long customTargetingValueId) throws RemoteException {
// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
// Create a statement to get custom targeting values.
StatementBuilder statementBuilder = new StatementBuilder().where("WHERE id = :id").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("id", customTargetingValueId);
// Default for total result set size.
int totalResultSetSize = 0;
do {
// Get custom targeting values by statement.
CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
CustomTargetingValue[] customTargetingValues = page.getResults();
// name.
for (CustomTargetingValue customTargetingValue : customTargetingValues) {
if (customTargetingValue.getDisplayName() == null) {
customTargetingValue.setDisplayName(customTargetingValue.getName());
}
customTargetingValue.setDisplayName(customTargetingValue.getDisplayName() + " (Deprecated)");
}
// Update the custom targeting values on the server.
customTargetingValues = customTargetingService.updateCustomTargetingValues(customTargetingValues);
for (CustomTargetingValue updatedCustomTargetingValue : customTargetingValues) {
System.out.printf("Custom targeting value with ID %d, name '%s', and display name " + "'%s' was updated.%n", updatedCustomTargetingValue.getId(), updatedCustomTargetingValue.getName(), updatedCustomTargetingValue.getDisplayName());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
}
use of com.google.api.ads.admanager.axis.v202205.CustomTargetingValue in project googleads-java-lib by googleads.
the class CreateCustomTargetingKeysAndValues method runExample.
/**
* Runs the example.
*
* @param adManagerServices 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(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
// Create predefined key.
CustomTargetingKey genderKey = new CustomTargetingKey();
genderKey.setDisplayName("gender");
genderKey.setName("g" + new Random().nextInt(1000));
genderKey.setType(CustomTargetingKeyType.PREDEFINED);
// Create predefined key that may be used for content targeting.
CustomTargetingKey genreKey = new CustomTargetingKey();
genreKey.setDisplayName("genre");
genreKey.setName("genre" + new Random().nextInt(1000));
genreKey.setType(CustomTargetingKeyType.PREDEFINED);
// Create free-form key.
CustomTargetingKey carModelKey = new CustomTargetingKey();
carModelKey.setDisplayName("car model");
carModelKey.setName("c" + new Random().nextInt(1000));
carModelKey.setType(CustomTargetingKeyType.FREEFORM);
// Create the custom targeting keys on the server.
CustomTargetingKey[] customTargetingKeys = customTargetingService.createCustomTargetingKeys(new CustomTargetingKey[] { genderKey, genreKey, carModelKey });
for (CustomTargetingKey createdCustomTargetingKey : customTargetingKeys) {
System.out.printf("A custom targeting key with ID %d, name '%s', and display name " + "'%s' was created.%n", createdCustomTargetingKey.getId(), createdCustomTargetingKey.getName(), createdCustomTargetingKey.getDisplayName());
}
// Set the created custom targeting keys.
genderKey = customTargetingKeys[0];
genreKey = customTargetingKeys[1];
carModelKey = customTargetingKeys[2];
// Create custom targeting value for the predefined gender key.
CustomTargetingValue genderMaleValue = new CustomTargetingValue();
genderMaleValue.setCustomTargetingKeyId(genderKey.getId());
genderMaleValue.setDisplayName("male");
// Name is set to 1 so that the actual name can be hidden from website
// users.
genderMaleValue.setName("1");
genderMaleValue.setMatchType(CustomTargetingValueMatchType.EXACT);
CustomTargetingValue genderFemaleValue = new CustomTargetingValue();
genderFemaleValue.setCustomTargetingKeyId(genderKey.getId());
genderFemaleValue.setDisplayName("female");
// Name is set to 2 so that the actual name can be hidden from website
// users.
genderFemaleValue.setName("2");
genderFemaleValue.setMatchType(CustomTargetingValueMatchType.EXACT);
// Create custom targeting value for the predefined genre key.
CustomTargetingValue genreComedyValue = new CustomTargetingValue();
genreComedyValue.setCustomTargetingKeyId(genreKey.getId());
genreComedyValue.setDisplayName("comedy");
genreComedyValue.setName("comedy");
genreComedyValue.setMatchType(CustomTargetingValueMatchType.EXACT);
CustomTargetingValue genreDramaValue = new CustomTargetingValue();
genreDramaValue.setCustomTargetingKeyId(genreKey.getId());
genreDramaValue.setDisplayName("drama");
genreDramaValue.setName("drama");
genreDramaValue.setMatchType(CustomTargetingValueMatchType.EXACT);
// Create custom targeting value for the free-form car model key. These are
// values that would be suggested in the UI or can be used when targeting
// with a FreeFormCustomCriteria.
CustomTargetingValue carModelHondaValue = new CustomTargetingValue();
carModelHondaValue.setCustomTargetingKeyId(carModelKey.getId());
carModelHondaValue.setDisplayName("~honda");
carModelHondaValue.setName("honda");
// A match type of broad will match anything including "honda",
// i.e. "~honda".
carModelHondaValue.setMatchType(CustomTargetingValueMatchType.BROAD);
// Create the custom targeting values on the server.
CustomTargetingValue[] customTargetingValues = customTargetingService.createCustomTargetingValues(new CustomTargetingValue[] { genderMaleValue, genderFemaleValue, genreComedyValue, genreDramaValue, carModelHondaValue });
for (CustomTargetingValue createdCustomTargetingValue : customTargetingValues) {
System.out.printf("A custom targeting value with ID %d, belonging to key with ID %d, " + "name '%s' and display name '%s' was created.%n", createdCustomTargetingValue.getId(), createdCustomTargetingValue.getCustomTargetingKeyId(), createdCustomTargetingValue.getName(), createdCustomTargetingValue.getDisplayName());
}
}
use of com.google.api.ads.admanager.axis.v202205.CustomTargetingValue in project googleads-java-lib by googleads.
the class DeleteCustomTargetingValues method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param customTargetingValueId the ID of the custom targeting value to delete.
* @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(AdManagerServices adManagerServices, AdManagerSession session, long customTargetingValueId) throws RemoteException {
// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
// Create a statement to select custom targeting value.
StatementBuilder statementBuilder = new StatementBuilder().where("WHERE id = :id").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("id", customTargetingValueId);
// Default for total result set size.
int totalResultSetSize = 0;
do {
// Get custom targeting values by statement.
CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (CustomTargetingValue customTargetingValue : page.getResults()) {
System.out.printf("%d) Custom targeting value with ID %d" + " will be deleted.%n", i++, customTargetingValue.getId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of custom targeting values to be deleted: %d%n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.admanager.axis.v202111.DeleteCustomTargetingValues action = new com.google.api.ads.admanager.axis.v202111.DeleteCustomTargetingValues();
// Perform action.
UpdateResult result = customTargetingService.performCustomTargetingValueAction(action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf("Number of custom targeting values deleted: %d%n", result.getNumChanges());
} else {
System.out.println("No custom targeting values deleted.");
}
}
}
use of com.google.api.ads.admanager.axis.v202205.CustomTargetingValue in project googleads-java-lib by googleads.
the class GetAllCustomTargetingKeysAndValues method runExample.
/**
* Runs the example.
*
* @param adManagerServices 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(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
// Get all custom targeting keys.
List<Long> customTargetingKeyIds = getAllCustomTargetingKeyIds(adManagerServices, session);
// Create a statement to get all custom targeting values for a custom
// targeting key.
StatementBuilder statementBuilder = new StatementBuilder().where("customTargetingKeyId = :customTargetingKeyId").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
int totalResultsCounter = 0;
for (Long customTargetingKeyId : customTargetingKeyIds) {
// Set the custom targeting key ID to select from.
statementBuilder.withBindVariableValue("customTargetingKeyId", customTargetingKeyId);
// Default for total result set size and offset.
int totalResultSetSize = 0;
statementBuilder.offset(0);
do {
// Get custom targeting values by statement.
CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
for (CustomTargetingValue customTargetingValue : page.getResults()) {
System.out.printf("%d) Custom targeting value with ID %d, belonging to key " + "with ID %d, name '%s' and display name '%s' was found.%n", totalResultsCounter++, customTargetingValue.getId(), customTargetingValue.getCustomTargetingKeyId(), customTargetingValue.getName(), customTargetingValue.getDisplayName());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
}
System.out.printf("Number of results found: %d%n", totalResultsCounter);
}
Aggregations