Export (0) Print
Expand All

Get Estimated Position By Bid in C#

The following example shows how to get the estimated position in the search results if the specified keywords use the specified bid value using the Ad Intelligence GetEstimatedPositionByKeywordIds 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.Linq;
using System.Text;
using System.ServiceModel;

// BingAds.AdIntelligence is the application-defined namespace that 
// this example uses for the Ad Intelligence service reference. 

using GetEstimatedPosition.BingAds.AdIntelligence;


namespace GetEstimatedPosition
{
    class Program
    {
        private static AdIntelligenceServiceClient service = null;

        // Specify your credentials.

        private static string m_password = "<passwordgoeshere>";
        private static string m_username = "<usernamegoeshere>";
        private static string m_token = "<devtokengoeshere>";

        // Specify the advertiser's account ID.

        private static long m_accountId = <accountidgoeshere>;


        static void Main()
        {
            KeywordIdEstimatedPosition[] positions = null;

            try
            {
                service = new AdIntelligenceServiceClient();

                positions = GetPositions(m_accountId);

                if (positions != null)
                {
                    foreach (KeywordIdEstimatedPosition position in positions)
                    {
                        if (null == position)
                        {
                            Console.WriteLine("The keyword ID is not valid.");
                        }
                        else
                        {
                            Console.WriteLine("{0} ({1})", position.KeywordEstimatedPosition.Keyword, position.KeywordId);

                            foreach (EstimatedPositionAndTraffic positionByMatchType in position.KeywordEstimatedPosition.EstimatedPositions)
                            {
                                Console.WriteLine("  " + positionByMatchType.MatchType);
                                Console.WriteLine("    Estimated Position: {0}", 
                                    positionByMatchType.EstimatedAdPosition);
                                Console.WriteLine("    Average CPC: " + 
                                    positionByMatchType.AverageCPC);
                                Console.WriteLine("    Estimated clicks per week: {0} to {1}", 
                                    positionByMatchType.MinClicksPerWeek, 
                                    positionByMatchType.MaxClicksPerWeek);
                                Console.WriteLine("    Estimated impressions per week: {0} to {1}", 
                                    positionByMatchType.MinImpressionsPerWeek, 
                                    positionByMatchType.MaxImpressionsPerWeek);
                                Console.WriteLine("    Estimated cost per week: {0} to {1}\n", 
                                    positionByMatchType.MinTotalCostPerWeek, 
                                    positionByMatchType.MaxTotalCostPerWeek);
                            }
                        }
                    }
                }

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

                if (null != e.InnerException)
                {
                    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 position in the search results if you bid the specified amount. 

        static KeywordIdEstimatedPosition[] GetPositions(long accountId)
        {
            GetEstimatedPositionByKeywordIdsRequest request = new GetEstimatedPositionByKeywordIdsRequest();
            GetEstimatedPositionByKeywordIdsResponse response = null;

            // Set the header information.

            request.CustomerAccountId = accountId.ToString();
            request.DeveloperToken = m_token;
            request.UserName = m_username;
            request.Password = m_password;

            // Set the request information.

            request.KeywordIds = new long[] { <keywordid1>, <keywordid2>, <etc.> };
            request.MaxBid = 0.47;

            try
            {
                response = service.GetEstimatedPositionByKeywordIds(request);
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                // Log this fault.

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

                foreach (AdApiError error in fault.Detail.Errors)
                {
                    if (105 == error.Code) //  InvalidCredentials
                    {
                        Console.WriteLine("The specified credentials are not valid or the account is inactive.");
                    }
                    else
                    {
                        Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                            error.ErrorCode, error.Code, error.Message, error.Detail);
                    }
                }

            }
            catch (FaultException<ApiFaultDetail> fault)
            {
                // Log this fault.

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

                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    switch (error.Code)
                    {
                        case 106: //  UserIsNotAuthorized
                            Console.WriteLine("The user is not authorized to call this operation.");
                            break;

                        case 1515: // CampaignServiceBidAmountsLessThanFloorPrice
                            Console.WriteLine("The bid amount cannot be less than the minimum bid amount allowed for the account's currency.");
                            break;

                        case 1516: // CampaignServiceBidAmountsGreaterThanCeilingPrice
                            Console.WriteLine("The bid amount cannot be greater than the maximum bid amount allowed for the account's currency.");
                            break;

                        case 1529: // CampaignServiceKeywordIdsArrayShouldNotBeNullOrEmpty
                            Console.WriteLine("The list of keyword IDs cannot be null or empty.");
                            break;

                        default:
                            Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                                error.ErrorCode, error.Code, error.Message, error.Details);
                            break;
                    }
                }

                foreach (BatchError error in fault.Detail.BatchErrors)
                {
                    Console.WriteLine("Error code: {0} ({1})\nIndex: {2}\nMessage: {3}\nDetail: {4}\n",
                        error.ErrorCode, error.Code, error.Index, error.Message, error.Details);
                }

            }

            return (null == response) ? null : response.KeywordEstimatedPositions;
        }
    }
}

Community Additions

Show:
© 2014 Microsoft