Export (0) Print
Expand All

Get Estimated Bid By Position in C#

The following example shows how to get the minimum suggested bid value of one or more keywords that could result in an ad appearing in the targeted position in the search results using the Ad Intelligence GetEstimatedBidByKeywords service operation.

This example has been developed and run within the environment described in Getting Started Using C# and Visual Basic with Bing Ads Services.

System_CLiX_note Note

This example uses the UserName and Password elements for authentication. For Managing User Authentication with OAuth, replace the UserName and Password elements with the AuthenticationToken, which is your OAuth access token.


using System;
using System.Collections.Generic;
using System.Globalization;
using System.ServiceModel;

using GetEstimatedBid.BingAds.AdIntelligence;

namespace GetEstimatedBid
{
    class Program
    {
        private static AdIntelligenceServiceClient _service;

        // Specify your credentials.

        private const string UserName = "<UserNameGoesHere>";
        private const string Password = "<PasswordGoesHere>";
        private const string DeveloperToken = "<DeveloperTokenGoesHere>";
        private const long AccountId = <AccountIdGoesHere>;

        static void Main()
        {
            try
            {
                _service = new AdIntelligenceServiceClient();

                IEnumerable<KeywordEstimatedBid> bids = GetBids(AccountId);

                foreach (KeywordEstimatedBid bid in bids)
                {
                    if (null == bid)
                    {
                        Console.WriteLine("The keyword is not valid.");
                    }
                    else
                    {
                        Console.WriteLine(bid.Keyword);

                        if (bid.EstimatedBids.Length == 0)
                        {
                            Console.WriteLine("  There is no bid information available for the keyword.\n");
                        }
                        else
                        {
                            foreach (EstimatedBidAndTraffic bidByMatchType in bid.EstimatedBids)
                            {
                                Console.WriteLine("  " + bidByMatchType.MatchType);
                                Console.WriteLine("    Estimated Minimum Bid: {0:c}", bidByMatchType.EstimatedMinBid);
                                Console.WriteLine("    Average CPC: " + bidByMatchType.AverageCPC);
                                Console.WriteLine("    Estimated clicks per week: {0} to {1}",
                                                  bidByMatchType.MinClicksPerWeek, bidByMatchType.MaxClicksPerWeek);
                                Console.WriteLine("    Estimated impressions per week: {0} to {1}",
                                                  bidByMatchType.MinImpressionsPerWeek,
                                                  bidByMatchType.MaxImpressionsPerWeek);
                                Console.WriteLine("    Estimated cost per week: {0} to {1}",
                                                  bidByMatchType.MinTotalCostPerWeek, bidByMatchType.MaxTotalCostPerWeek);
                                Console.WriteLine();
                            }
                        }
                    }
                }

                _service.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);

                if (e.InnerException != null)
                {
                    Console.WriteLine("\n" + e.InnerException.Message);
                }

                if (_service != null)
                {
                    _service.Abort();
                }
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);

                if (_service != null)
                {
                    _service.Abort();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                if (_service != null)
                {
                    _service.Abort();
                }
            }
        }

        // Get the estimated bid that could place your ad in the targeted position. 

        static IEnumerable<KeywordEstimatedBid> GetBids(long accountId)
        {
            var request = new GetEstimatedBidByKeywordsRequest
                {
                    // Set the header information.
                    CustomerAccountId = accountId.ToString(CultureInfo.InvariantCulture),
                    DeveloperToken = DeveloperToken,
                    UserName = UserName,
                    Password = Password,

                    // Set the request information.
                    Currency = Currency.USDollar,
                    Keywords = new[] {"flower delivery"},
                    Language = "English",
                    MatchTypes = new[] {MatchType.Exact, MatchType.Broad, MatchType.Phrase},
                    PublisherCountries = new[] {"US"},
                    TargetPositionForAds = TargetAdPosition.SideBar
                };

            try
            {
                var response = _service.GetEstimatedBidByKeywords(request);

                return (response != null) ? response.KeywordEstimatedBids : null;
            }
            // Ad Intelligence service operations can throw AdApiFaultDetail.
            catch (FaultException<AdApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("The operation failed with the following faults:\n");

                // If the AdApiError array is not null, the following are examples of error codes that may be found.
                foreach (AdApiError error in fault.Detail.Errors)
                {
                    Console.WriteLine("AdApiError");
                    Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

                    switch (error.Code)
                    {
                        case 0:     // InternalError
                            break;
                        case 105:   // InvalidCredentials
                            break;
                        case 117:   // CallRateExceeded
                            break;
                        default:
                            Console.WriteLine("Please see MSDN documentation for more details about the error code output above.");
                            break;
                    }
                }

                throw new Exception("", fault);
            }
            // Ad Intelligence service operations can throw ApiFaultDetail.
            catch (FaultException<ApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("The operation failed with the following faults:\n");

                // If the BatchError array is not null, the following are examples of error codes that may be found.
                foreach (BatchError error in fault.Detail.BatchErrors)
                {
                    Console.WriteLine("BatchError at Index: {0}", error.Index);
                    Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

                    switch (error.Code)
                    {
                        case 0:     // InternalError
                            break;
                        default:
                            Console.WriteLine("Please see MSDN documentation for more details about the error code output above.");
                            break;
                    }
                }

                // If the OperationError array is not null, the following are examples of error codes that may be found.
                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    Console.WriteLine("OperationError");
                    Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

                    switch (error.Code)
                    {
                        case 0:     // InternalError
                            break;
                        case 106:   // UserIsNotAuthorized
                            break;
                        case 1505:  // CampaignServiceKeywordsArrayShouldNotBeNullOrEmpty
                            break;
                        case 3408:  // CampaignServiceLanguageAndCountryNotSupported
                            break;
                        case 3409:  // CampaignServiceInvalidMatchTypes
                            break;
                        case 3412:  // CampaignServiceInvalidLanguage
                            break;
                        case 3413:  // CampaignServiceInvalidPublisherCountry
                            break;
                        case 5001:  // CampaignServiceUnsupportedMatchType
                            break;
                        default:
                            Console.WriteLine("Please see MSDN documentation for more details about the error code output above.");
                            break;
                    }
                }

                throw new Exception("", fault);
            }
        }
    }
}

Community Additions

Show:
© 2014 Microsoft