MSDN Library

Get Estimated Position By Bid in Java

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 Java 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.


// Command line usage:
// java GetEstimatedPosition
//

import java.text.*;
import java.rmi.*;
import bingads.adintelligence.*;
import bingads.adintelligence.adapi.*;
import bingads.adintelligence.datacontracts.*;

/**
 *
 */
public class GetEstimatedPosition {

    private static String namespace = null;
    private static BasicHttpBinding_IAdIntelligenceServiceStub 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>;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        AdIntelligenceServiceLocator locator = null;
        KeywordIdEstimatedPosition[] positions = null;
        NumberFormat currencyFmt = NumberFormat.getCurrencyInstance();

        try
        {
            locator = new AdIntelligenceServiceLocator();
            namespace = locator.getServiceName().getNamespaceURI();
            service = (BasicHttpBinding_IAdIntelligenceServiceStub) locator.getBasicHttpBinding_IAdIntelligenceService();

            positions = GetPositions(m_accountId);

            if (positions != null)
            {
                for (KeywordIdEstimatedPosition position : positions)
                {
                    if (null == position)
                    {
                        System.out.println("The keyword ID is not valid.\n");
                    }
                    else
                    {
                        System.out.print(position.getKeywordId());

                        if (null == position.getKeywordEstimatedPosition())
                        {
                            System.out.println(" - There is no information available for the keyword.\n");
                        }
                        else
                        {
                            for (EstimatedPositionAndTraffic positionByMatchType : position.getKeywordEstimatedPosition().getEstimatedPositions())
                            {
                                System.out.println("  " + positionByMatchType.getMatchType());
                                System.out.println("    Estimated Position: " + positionByMatchType.getEstimatedAdPosition());
                                System.out.println("    Average CPC: " + currencyFmt.format(positionByMatchType.getAverageCPC()));
                                System.out.printf("    Estimated clicks per week: %d to %d\n",
                                    positionByMatchType.getMinClicksPerWeek(), positionByMatchType.getMaxClicksPerWeek());
                                System.out.printf("    Estimated impressions per week: %d to %d\n",
                                    positionByMatchType.getMinImpressionsPerWeek(), positionByMatchType.getMaxImpressionsPerWeek());
                                System.out.printf("    Estimated cost per week: %s to %s\n",
                                    currencyFmt.format(positionByMatchType.getMinTotalCostPerWeek()),
                                    currencyFmt.format(positionByMatchType.getMaxTotalCostPerWeek()));
                                System.out.println();
                            }
                        }
                    }
                }
            }
        }
        catch (RemoteException e)
        {
            if (new AdApiFaultDetail().getClass() == e.getClass() ||
                new ApiFaultDetail().getClass() == e.getClass())
            {
                ; // Ignore because we already caught these below.
            }
            else
            {
                System.out.print("Service communication error encountered: ");
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        catch (Exception e)
        {
            System.out.print("Error encountered: ");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * Get the estimated position in the search results given the bid amount.
     */

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

        // Set the header properties.

        service.clearHeaders();
        service.setHeader(namespace, "DeveloperToken", m_token);
        service.setHeader(namespace, "UserName", m_username);
        service.setHeader(namespace, "Password", m_password);
        service.setHeader(namespace, "CustomerAccountId", m_accountId);

        // Specify request information.

        request.setKeywordIds(new long[] { <keywordid1>, <keywordid2> });
        request.setMaxBid(0.47);

        try
        {
            response = service.getEstimatedPositionByKeywordIds(request);
        }
        catch (AdApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("getEstimatedPositionByKeywordIds failed with the following faults:");

            for (AdApiError error : fault.getErrors())
            {
                if (105 == error.getCode()) //  InvalidCredentials
                {
                    System.out.println("The specified credentials are not valid or the account is inactive.");
                }
                else
                {
                    System.out.print("\tError code: " + error.getErrorCode());
                    System.out.println(" (" + error.getCode() + ")");
                    System.out.println("\tMessage: " + error.getMessage());
                    System.out.println("\tDetail: " + error.getDetail());
                }
            }

            throw fault;  // To be caught and ignored in main.
        }
        catch (ApiFaultDetail fault)
        {
            // Log this fault.

            System.out.println("getEstimatedPositionByKeywordIds failed with the following faults:");

            for (OperationError error : fault.getOperationErrors())
            {
                switch (error.getCode())
                {
                    case 106: //  UserIsNotAuthorized
                        System.out.println("The user is not authorized to call this operation.");
                        break;

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

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

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

                    default:
                        System.out.print("\tError code: " + error.getErrorCode());
                        System.out.println(" (" + error.getCode() + ")");
                        System.out.println("\tMessage: " + error.getMessage());
                        System.out.println("\tDetail: " + error.getDetails());
                        break;
                }
            }

            for (BatchError error : fault.getBatchErrors())
            {
                System.out.print("\tError code: " + error.getErrorCode());
                System.out.println(" (" + error.getCode() + ")");
                System.out.println("\tIndex: " + error.getIndex());
                System.out.println("\tMessage: " + error.getMessage());
                System.out.println("\tDetail: " + error.getDetails());
            }

            throw fault;  // To be caught and ignored in main.
        }

        return response.getKeywordEstimatedPositions();
    }
}

Community Additions

Show:
© 2016 Microsoft