Exemplo n.º 1
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        public void Run(AdManagerUser user)
        {
            using (CustomTargetingService customTargetingService =
                       user.GetService <CustomTargetingService>())
            {
                // Set the ID of the custom targeting key to update.
                int customTargetingKeyId = int.Parse(_T("INSERT_CUSTOM_TARGETING_KEY_ID_HERE"));

                // Create a statement to get the custom targeting key.
                StatementBuilder statementBuilder = new StatementBuilder()
                                                    .Where("id = :id")
                                                    .OrderBy("id ASC")
                                                    .Limit(1)
                                                    .AddValue("id", customTargetingKeyId);

                try
                {
                    // Get custom targeting keys by statement.
                    CustomTargetingKeyPage page =
                        customTargetingService.getCustomTargetingKeysByStatement(
                            statementBuilder.ToStatement());

                    CustomTargetingKey customTargetingKey = page.results[0];

                    // Update each local custom targeting key object by changing its display name.
                    if (customTargetingKey.displayName == null)
                    {
                        customTargetingKey.displayName = customTargetingKey.name;
                    }

                    customTargetingKey.displayName =
                        customTargetingKey.displayName + " (Deprecated)";

                    // Update the custom targeting keys on the server.
                    CustomTargetingKey[] customTargetingKeys =
                        customTargetingService.updateCustomTargetingKeys(new CustomTargetingKey[]
                    {
                        customTargetingKey
                    });

                    foreach (CustomTargetingKey updatedCustomTargetingKey in customTargetingKeys)
                    {
                        Console.WriteLine(
                            "Custom targeting key with ID \"{0}\", name \"{1}\", and " +
                            "display name \"{2}\" was updated.", updatedCustomTargetingKey.id,
                            updatedCustomTargetingKey.name, updatedCustomTargetingKey.displayName);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(
                        "Failed to update display name of custom targeting keys. 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);
      }
    }