/// <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 AudienceSegmentService.
      AudienceSegmentService audienceSegmentService = (AudienceSegmentService) user.GetService(
          DfpService.v201505.AudienceSegmentService);

      // Get the NetworkService.
      NetworkService networkService = (NetworkService) user.GetService(
          DfpService.v201505.NetworkService);

      long customTargetingKeyId = long.Parse(_T("INSERT_CUSTOM_TARGETING_KEY_ID_HERE"));
      long customTargetingValueId = long.Parse(_T("INSERT_CUSTOM_TARGETING_VALUE_ID_HERE"));

      try {
        // Get the root ad unit ID used to target the whole site.
        String rootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

        // 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.adUnitId = rootAdUnitId;
        adUnitTargeting.includeDescendants = true;

        inventoryTargeting.targetedAdUnits = 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.keyId = customTargetingKeyId;
        customCriteria.@operator = CustomCriteriaComparisonOperator.IS;
        customCriteria.valueIds = new long[] {customTargetingValueId};

        // Create the custom criteria expression.
        CustomCriteriaSet topCustomCriteriaSet = new CustomCriteriaSet();
        topCustomCriteriaSet.logicalOperator = CustomCriteriaSetLogicalOperator.AND;
        topCustomCriteriaSet.children = new CustomCriteriaNode[] {customCriteria};

        // Create the audience segment rule.
        FirstPartyAudienceSegmentRule rule = new FirstPartyAudienceSegmentRule();
        rule.inventoryRule = inventoryTargeting;
        rule.customCriteriaRule = topCustomCriteriaSet;

        // Create an audience segment.
        RuleBasedFirstPartyAudienceSegment audienceSegment =
            new RuleBasedFirstPartyAudienceSegment();
        audienceSegment.name = "Sports enthusiasts audience segment #" + this.GetTimeStamp();
        audienceSegment.description = "Sports enthusiasts between the ages of 20 and 30.";
        audienceSegment.pageViews = 6;
        audienceSegment.recencyDays = 6;
        audienceSegment.membershipExpirationDays = 88;
        audienceSegment.rule = rule;

        // Create the audience segment on the server.
        AudienceSegment[] audienceSegments = audienceSegmentService.createAudienceSegments(
            new FirstPartyAudienceSegment[] {audienceSegment});

        foreach (AudienceSegment createdAudienceSegment in audienceSegments) {
          Console.WriteLine("An audience segment with ID \"{0}\", name \"{1}\", and type \"{2}\" " +
              "was created.", createdAudienceSegment.id, createdAudienceSegment.name,
              createdAudienceSegment.type);
        }
      } catch (Exception e) {
        Console.WriteLine("Failed to get audience segment. 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 LineItemService.
      LineItemService lineItemService = (LineItemService) user.GetService(
          DfpService.v201505.LineItemService);

      long lineItemId = long.Parse(_T("INSERT_LINE_ITEM_ID_HERE"));
      long[] customCriteriaIds1 =
          new long[] {long.Parse(_T("INSERT_CUSTOM_TARGETING_KEY_ID_HERE")),
              long.Parse(_T("INSERT_CUSTOM_TARGETING_VALUE_ID_HERE"))};
      long[] customCriteriaIds2 =
        new long[] {long.Parse(_T("INSERT_CUSTOM_TARGETING_KEY_ID_HERE")),
            long.Parse(_T("INSERT_CUSTOM_TARGETING_VALUE_ID_HERE"))};
      long[] customCriteriaIds3 =
        new long[] {long.Parse(_T("INSERT_CUSTOM_TARGETING_KEY_ID_HERE")),
            long.Parse(_T("INSERT_CUSTOM_TARGETING_VALUE_ID_HERE"))};

      // Create custom criteria.
      CustomCriteria customCriteria1 = new CustomCriteria();
      customCriteria1.keyId = customCriteriaIds1[0];
      customCriteria1.valueIds = new long[] {customCriteriaIds1[1]};
      customCriteria1.@operator = CustomCriteriaComparisonOperator.IS;

      CustomCriteria customCriteria2 = new CustomCriteria();
      customCriteria2.keyId = customCriteriaIds2[0];
      customCriteria2.valueIds = new long[] {customCriteriaIds2[1]};
      customCriteria2.@operator = CustomCriteriaComparisonOperator.IS_NOT;

      CustomCriteria customCriteria3 = new CustomCriteria();
      customCriteria3.keyId = customCriteriaIds3[0];
      customCriteria3.valueIds = new long[] {customCriteriaIds3[1]};
      customCriteria3.@operator = CustomCriteriaComparisonOperator.IS;

      // Create the custom criteria set that will resemble:
      //
      // (customCriteria1.key == customCriteria1.value OR
      //     (customCriteria2.key != customCriteria2.value AND
      //         customCriteria3.key == customCriteria3.value))
      CustomCriteriaSet topCustomCriteriaSet = new CustomCriteriaSet();
      topCustomCriteriaSet.logicalOperator = CustomCriteriaSetLogicalOperator.OR;

      CustomCriteriaSet subCustomCriteriaSet = new CustomCriteriaSet();
      subCustomCriteriaSet.logicalOperator = CustomCriteriaSetLogicalOperator.AND;
      subCustomCriteriaSet.children =
         new CustomCriteriaNode[] {customCriteria2, customCriteria3};
      topCustomCriteriaSet.children =
         new CustomCriteriaNode[] {customCriteria1, subCustomCriteriaSet};

      try {
        StatementBuilder statementBuilder = new StatementBuilder()
            .Where("id = :id")
            .OrderBy("id ASC")
            .Limit(1)
            .AddValue("id", lineItemId);
        // Set the custom criteria targeting on the line item.
        LineItemPage page = lineItemService.getLineItemsByStatement(statementBuilder.ToStatement());
        LineItem lineItem = page.results[0];
        lineItem.targeting.customTargeting = topCustomCriteriaSet;

        // Update the line items on the server.
        LineItem[] updatedLineItems = lineItemService.updateLineItems(new LineItem[] {lineItem});

        foreach (LineItem updatedLineItem in updatedLineItems) {
          // Display the updated line item.
          Console.WriteLine("Line item with ID {0} updated with custom criteria targeting \n{1}\n",
              updatedLineItem.id,
              getCustomCriteriaSetString(updatedLineItem.targeting.customTargeting, 0));
        }
      } catch (Exception e) {
        Console.WriteLine("Failed to add custom target criteria. Exception says \"{0}\"",
            e.Message);
      }
    }