Target Age and Gender in C#

The following example shows how to create age and gender targets and add the combined target 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 TargetByAgeGender.BingAds.CampaignManagement;


namespace TargetByAgeGender
{
    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 = "<DevTokenGoesHere>";

        // 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 age and gender.

        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 {
                        Age = new AgeTarget {
                            Bids = new AgeTargetBid[] {
                                new AgeTargetBid {
                                    Age = AgeRange.TwentyFiveToThirtyFive,
                                    IncrementalBid = IncrementalBidPercentage.TenPercent
                                },
                                new AgeTargetBid {
                                    Age = AgeRange.ThirtyFiveToFifty,
                                    IncrementalBid = IncrementalBidPercentage.TwentyPercent
                                }
                            }
                        },
                        Gender = new GenderTarget {
                            Bids = new GenderTargetBid[] {
                                new GenderTargetBid {
                                    Gender = GenderType.Female,
                                }
                            }
                        },
                        Name = "AgeGenderTarget",
                    }
                };

                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)
                {
                    Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                        error.ErrorCode, error.Code, error.Message, error.Details);
                }
            }

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


Community Additions

Show: