Target Metropolitan Areas in C#

The following example shows how to create a target for a specific metropolitan area, and add it to a customer’s library using the Campaign Management AddTargetsToLibrary 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.Threading;
using System.ServiceModel;

// BingAds.CampaignManagement is the application-defined namespace that 
// this example used when it added the Campaign Management service reference. 

using TargetByMetroArea.BingAds.CampaignManagement;


namespace TargetByMetroArea
{
    class Program
    {
        private static CampaignManagementServiceClient service = null;

        // Specify your credentials.

        private static string m_password = "<PasswordGoesHere>";
        private static string m_username = "<UsernameGoesHere>";
        private static string m_token = "<DevTokeGoesHere>";

        // Specify the customer ID to add the target to.

        private static long m_customerId = <CustomerIdGoesHere>;


        static void Main(string[] args)
        {
            long[] targetIds = null;

            try
            {
                service = new CampaignManagementServiceClient();

                targetIds = AddTarget(m_customerId);

                if (null != targetIds)
                {
                    foreach (long id in targetIds)
                    {
                        Console.WriteLine("Added target with ID {0}.", id);
                    }
                }

                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();
                }
            }
        }

        // Create a target that targets a specific metropolitan area.

        private static long[] AddTarget(long customerId)
        {
            AddTargetsToLibraryRequest request = new AddTargetsToLibraryRequest();
            AddTargetsToLibraryResponse response = null;

            try
            {
                request.UserName = m_username;
                request.Password = m_password;
                request.DeveloperToken = m_token;
                request.CustomerId = customerId.ToString();  // Customer ID is required for this operation.

                request.Targets = new Target[] { 
                    new Target {
                        Location = new LocationTarget {
                            MetroAreaTarget = new MetroAreaTarget {
                                Bids = new MetroAreaTargetBid[] {
                                    new MetroAreaTargetBid{
                                        MetroArea = "Seattle, WA US",
                                        IncrementalBid = IncrementalBidPercentage.TenPercent
                                    }
                                },
                            },
                            TargetAllLocations = false
                        },
                        Name = "SeattleMetroTarget",
                    }
                };

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

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

                foreach (AdApiError error in fault.Detail.Errors)
                {
                    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("AddTargetsToLibrary failed with the following faults:\n");

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

                foreach (BatchError error in fault.Detail.BatchErrors)
                {
                    if (1405 == error.Code) //CampaignServiceInvalidLocationTarget
                    {
                        Console.WriteLine("\"{0}\" contains an invalid location code.\n",
                            request.Targets[error.Index].Name);
                    }
                    else
                    {
                        Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\nIndex: {4}",
                            error.ErrorCode, error.Code, error.Message, error.Details, error.Index);
                    }
                }
            }

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



Community Additions

Show: