/// <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.v201311.AudienceSegmentService);

            // Set defaults for page and Statement.
            AudienceSegmentPage page      = new AudienceSegmentPage();
            Statement           statement = new Statement();
            int offset = 0;

            try {
                do
                {
                    // Create a Statement to get all audience segments.
                    statement.query = string.Format("LIMIT 500 OFFSET {0}", offset);

                    // Get audience segment by Statement.
                    page = audienceSegmentService.getAudienceSegmentsByStatement(statement);

                    // Display results.
                    if (page.results != null && page.results.Length > 0)
                    {
                        int i = page.startIndex;
                        foreach (AudienceSegment segment in page.results)
                        {
                            Console.WriteLine("{0}) 'Audience segment with id \"{1}\" and name \"{2}\" of " +
                                              "size {3} was found.", i, segment.id, segment.name, segment.size);
                            i++;
                        }
                    }

                    offset += 500;
                } while (offset < page.totalResultSetSize);
                Console.WriteLine("Number of results found: {0}", page.totalResultSetSize);
            } catch (Exception ex) {
                Console.WriteLine("Failed to get audience segment. Exception says \"{0}\"", ex.Message);
            }
        }
        /// <summary>
        /// Run the code example.
        /// </summary>
        public void Run(AdManagerUser user)
        {
            using (AudienceSegmentService audienceSegmentService =
                       user.GetService <AudienceSegmentService>())
            {
                // Get the NetworkService.
                NetworkService networkService =
                    user.GetService <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);
                }
            }
        }