"INSERT_CUSTOM_TARGETING_VALUE_ID_HERE";
public static void runExample(DfpServices dfpServices, DfpSession session,
long customTargetingKeyId, long customTargetingValueId) throws Exception {
// Get the AudienceSegmentService.
AudienceSegmentServiceInterface audienceSegmentService =
dfpServices.get(session, AudienceSegmentServiceInterface.class);
// Get the NetworkService.
NetworkServiceInterface networkService =
dfpServices.get(session, NetworkServiceInterface.class);
// Get the root ad unit ID used to target the whole site.
String rootAdUnitId = networkService.getCurrentNetwork().getEffectiveRootAdUnitId();
// Create inventory targeting.
InventoryTargeting inventoryTargeting = new InventoryTargeting();
// Create ad unit targeting for the root ad unit (i.e. the whole network).
AdUnitTargeting adUnitTargeting = new AdUnitTargeting();
adUnitTargeting.setAdUnitId(rootAdUnitId);
adUnitTargeting.setIncludeDescendants(true);
inventoryTargeting.setTargetedAdUnits(new AdUnitTargeting[] {adUnitTargeting});
// Create the custom criteria to be used in the segment rule.
// CUSTOM_TARGETING_KEY_ID == CUSTOM_TARGETING_VALUE_ID
CustomCriteria customCriteria = new CustomCriteria();
customCriteria.setKeyId(customTargetingKeyId);
customCriteria.setOperator(CustomCriteriaComparisonOperator.IS);
customCriteria.setValueIds(new long[] {customTargetingValueId});
// Create the custom criteria expression.
CustomCriteriaSet topCustomCriteriaSet = new CustomCriteriaSet();
topCustomCriteriaSet.setLogicalOperator(CustomCriteriaSetLogicalOperator.AND);
topCustomCriteriaSet.setChildren(new CustomCriteriaNode[] {customCriteria});
// Create the audience segment rule.
FirstPartyAudienceSegmentRule rule = new FirstPartyAudienceSegmentRule();
rule.setInventoryRule(inventoryTargeting);
rule.setCustomCriteriaRule(topCustomCriteriaSet);
// Create an audience segment.
RuleBasedFirstPartyAudienceSegment audienceSegment = new RuleBasedFirstPartyAudienceSegment();
audienceSegment.setName(
"Sports enthusiasts audience segment #" + new Random().nextInt(Integer.MAX_VALUE));
audienceSegment.setDescription("Sports enthusiasts between the ages of 20 and 30.");
audienceSegment.setPageViews(6);
audienceSegment.setRecencyDays(6);
audienceSegment.setMembershipExpirationDays(88);
audienceSegment.setRule(rule);
// Create the audience segment on the server.
AudienceSegment[] audienceSegments = audienceSegmentService.createAudienceSegments(
new FirstPartyAudienceSegment[] {audienceSegment});
for (AudienceSegment createdAudienceSegment : audienceSegments) {
System.out.printf(
"An audience segment with ID \"%d\", name \"%s\", and type \"%s\" was created.\n",