/// <summary> /// Run the code example. /// </summary> public void Run(AdManagerUser user) { using (CustomTargetingService customTargetingService = user.GetService <CustomTargetingService>()) { // Set the ID of the predefined custom targeting value to update. long customTargetingValueId = long.Parse(_T("INSERT_CUSTOM_TARGETING_VALUE_ID_HERE")); // Create a statement to only select predefined custom targeting values // for a given key. StatementBuilder statementBuilder = new StatementBuilder() .Where("id = :customTargetingValueId").OrderBy("id ASC").Limit(1) .AddValue("customTargetingValueId", customTargetingValueId); try { // Get custom targeting values by statement. CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement( statementBuilder.ToStatement()); CustomTargetingValue customTargetingValue = page.results[0]; // Update the local custom targeting value object by changing its display name. if (customTargetingValue.displayName == null) { customTargetingValue.displayName = customTargetingValue.displayName; } customTargetingValue.displayName = customTargetingValue.displayName + " (Deprecated)"; // Update the custom targeting values on the server. CustomTargetingValue[] customTargetingValues = customTargetingService.updateCustomTargetingValues( new CustomTargetingValue[] { customTargetingValue }); foreach (CustomTargetingValue updatedCustomTargetingValue in customTargetingValues) { Console.WriteLine( "Custom targeting value with ID \"{0}\", name \"{1}\", and " + "display name \"{2}\" was updated.", updatedCustomTargetingValue.id, updatedCustomTargetingValue.name, updatedCustomTargetingValue.displayName); } } catch (Exception e) { Console.WriteLine( "Failed to update display names of custom targeting values. " + "Exception says \"{0}\"", e.Message); } } }
/// <summary> /// Run the code example. /// </summary> public void Run(AdManagerUser user) { using (CustomTargetingService customTargetingService = user.GetService <CustomTargetingService>()) { // Create predefined key. CustomTargetingKey genderKey = new CustomTargetingKey(); genderKey.displayName = "gender"; genderKey.name = "g"; genderKey.type = CustomTargetingKeyType.PREDEFINED; // Create predefined key that may be used for content targeting. CustomTargetingKey genreKey = new CustomTargetingKey(); genreKey.displayName = "genre"; genreKey.name = "genre"; genreKey.type = CustomTargetingKeyType.PREDEFINED; // Create free-form key. CustomTargetingKey carModelKey = new CustomTargetingKey(); carModelKey.displayName = "car model"; carModelKey.name = "c"; carModelKey.type = CustomTargetingKeyType.FREEFORM; try { // Create the custom targeting keys on the server. CustomTargetingKey[] keys = customTargetingService.createCustomTargetingKeys( new CustomTargetingKey[] { genderKey, genreKey, carModelKey }); if (keys != null) { foreach (CustomTargetingKey key in keys) { Console.WriteLine( "A custom targeting key with ID \"{0}\", name \"{1}\", and " + "display name \"{2}\" was created.", key.id, key.name, key.displayName); } } else { Console.WriteLine("No keys were created."); } // Create custom targeting value for the predefined gender key. CustomTargetingValue genderMaleValue = new CustomTargetingValue(); genderMaleValue.customTargetingKeyId = keys[0].id; genderMaleValue.displayName = "male"; // Name is set to 1 so that the actual name can be hidden from website // users. genderMaleValue.name = "1"; genderMaleValue.matchType = CustomTargetingValueMatchType.EXACT; CustomTargetingValue genderFemaleValue = new CustomTargetingValue(); genderFemaleValue.customTargetingKeyId = keys[0].id; genderFemaleValue.displayName = "female"; // Name is set to 2 so that the actual name can be hidden from website // users. genderFemaleValue.name = "2"; genderFemaleValue.matchType = CustomTargetingValueMatchType.EXACT; // Create custom targeting value for the predefined genre key. CustomTargetingValue genreComedyValue = new CustomTargetingValue(); genreComedyValue.customTargetingKeyId = keys[1].id; genreComedyValue.displayName = "comedy"; genreComedyValue.name = "comedy"; genreComedyValue.matchType = CustomTargetingValueMatchType.EXACT; CustomTargetingValue genreDramaValue = new CustomTargetingValue(); genreDramaValue.customTargetingKeyId = keys[1].id; genreDramaValue.displayName = "drama"; genreDramaValue.name = "drama"; genreDramaValue.matchType = CustomTargetingValueMatchType.EXACT; // Create custom targeting value for the free-form age key. These are // values that would be suggested in the UI or can be used when // targeting with a FreeFormCustomCriteria. CustomTargetingValue carModelHondaCivicValue = new CustomTargetingValue(); carModelHondaCivicValue.customTargetingKeyId = keys[2].id; carModelHondaCivicValue.displayName = "honda civic"; carModelHondaCivicValue.name = "honda civic"; // Setting match type to exact will match exactly "honda civic". carModelHondaCivicValue.matchType = CustomTargetingValueMatchType.EXACT; // Create the custom targeting values on the server. CustomTargetingValue[] returnValues = customTargetingService.createCustomTargetingValues( new CustomTargetingValue[] { genderMaleValue, genderFemaleValue, genreComedyValue, genreDramaValue, carModelHondaCivicValue }); if (returnValues != null) { foreach (CustomTargetingValue value in returnValues) { Console.WriteLine( "A custom targeting value with ID \"{0}\", belonging to key " + "with ID \"{1}\", name \"{2}\", and display name \"{3}\" " + "was created.", value.id, value.customTargetingKeyId, value.name, value.displayName); } } else { Console.WriteLine("No values were created."); } } catch (Exception e) { Console.WriteLine( "Failed to create custom targeting keys and values. Exception " + "says \"{0}\"", e.Message); } } }
/// <summary> /// Run the code example. /// </summary> /// <param name="user">The DFP user object running the code example.</param> public override void Run(DfpUser user) { // Get the CustomTargetingService. CustomTargetingService customTargetingService = (CustomTargetingService) user.GetService(DfpService.v201511.CustomTargetingService); // Create predefined key. CustomTargetingKey genderKey = new CustomTargetingKey(); genderKey.displayName = "gender"; genderKey.name = "g"; genderKey.type = CustomTargetingKeyType.PREDEFINED; // Create predefined key that may be used for content targeting. CustomTargetingKey genreKey = new CustomTargetingKey(); genreKey.displayName = "genre"; genreKey.name = "genre"; genreKey.type = CustomTargetingKeyType.PREDEFINED; // Create free-form key. CustomTargetingKey carModelKey = new CustomTargetingKey(); carModelKey.displayName = "car model"; carModelKey.name = "c"; carModelKey.type = CustomTargetingKeyType.FREEFORM; try { // Create the custom targeting keys on the server. CustomTargetingKey[] keys = customTargetingService.createCustomTargetingKeys( new CustomTargetingKey[] {genderKey, genreKey, carModelKey}); if (keys != null) { foreach (CustomTargetingKey key in keys) { Console.WriteLine("A custom targeting key with ID \"{0}\", name \"{1}\", and display " + "name \"{2}\" was created.", key.id, key.name, key.displayName); } } else { Console.WriteLine("No keys were created."); } // Create custom targeting value for the predefined gender key. CustomTargetingValue genderMaleValue = new CustomTargetingValue(); genderMaleValue.customTargetingKeyId = keys[0].id; genderMaleValue.displayName = "male"; // Name is set to 1 so that the actual name can be hidden from website // users. genderMaleValue.name = "1"; genderMaleValue.matchType = CustomTargetingValueMatchType.EXACT; CustomTargetingValue genderFemaleValue = new CustomTargetingValue(); genderFemaleValue.customTargetingKeyId = keys[0].id; genderFemaleValue.displayName = "female"; // Name is set to 2 so that the actual name can be hidden from website // users. genderFemaleValue.name = "2"; genderFemaleValue.matchType = CustomTargetingValueMatchType.EXACT; // Create custom targeting value for the predefined genre key. CustomTargetingValue genreComedyValue = new CustomTargetingValue(); genreComedyValue.customTargetingKeyId = keys[1].id; genreComedyValue.displayName = "comedy"; genreComedyValue.name = "comedy"; genreComedyValue.matchType = CustomTargetingValueMatchType.EXACT; CustomTargetingValue genreDramaValue = new CustomTargetingValue(); genreDramaValue.customTargetingKeyId = keys[1].id; genreDramaValue.displayName = "drama"; genreDramaValue.name = "drama"; genreDramaValue.matchType = CustomTargetingValueMatchType.EXACT; // Create custom targeting value for the free-form age key. These are // values that would be suggested in the UI or can be used when // targeting with a FreeFormCustomCriteria. CustomTargetingValue carModelHondaCivicValue = new CustomTargetingValue(); carModelHondaCivicValue.customTargetingKeyId = keys[2].id; carModelHondaCivicValue.displayName = "honda civic"; carModelHondaCivicValue.name = "honda civic"; // Setting match type to exact will match exactly "honda civic". carModelHondaCivicValue.matchType = CustomTargetingValueMatchType.EXACT; // Create the custom targeting values on the server. CustomTargetingValue[] returnValues = customTargetingService.createCustomTargetingValues( new CustomTargetingValue[] {genderMaleValue, genderFemaleValue, genreComedyValue, genreDramaValue, carModelHondaCivicValue}); if (returnValues != null) { foreach (CustomTargetingValue value in returnValues) { Console.WriteLine("A custom targeting value with ID \"{0}\", belonging to key with " + "ID \"{1}\", name \"{2}\", and display name \"{3}\" was created.", value.id, value.customTargetingKeyId, value.name, value.displayName); } } else { Console.WriteLine("No values were created."); } } catch (Exception e) { Console.WriteLine("Failed to create custom targeting keys and values. Exception " + "says \"{0}\"", e.Message); } }