Export (0) Print
Expand All

Target Days and Hours in C#

The following example shows how to create day and hour 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 TargetByDayHour.BingAds.CampaignManagement;


namespace TargetByDayHour
{
    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 specific days and hours of the day.

        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 {
                        Day = new DayTarget {
                            Bids = new DayTargetBid[] {
                                new DayTargetBid {
                                    Day = Day.Thursday,
                                    IncrementalBid = IncrementalBidPercentage.TenPercent
                                },
                                new DayTargetBid {
                                    Day = Day.Friday,
                                    IncrementalBid = IncrementalBidPercentage.TwentyPercent
                                },
                                new DayTargetBid {
                                    Day = Day.Saturday,
                                    IncrementalBid = IncrementalBidPercentage.TwentyPercent
                                }
                            },
                            TargetAllDays = true
                        },
                        Hour = new HourTarget{
                            Bids = new HourTargetBid[] {
                                new HourTargetBid {
                                    Hour = HourRange.ElevenAMToTwoPM,
                                    IncrementalBid = IncrementalBidPercentage.TenPercent
                                },
                                new HourTargetBid {
                                    Hour = HourRange.TwoPMToSixPM,
                                    IncrementalBid = IncrementalBidPercentage.TwentyPercent
                                }
                            },
                            TargetAllHours = true
                        },
                        Name = "DayHourTarget",
                    }
                };

                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:
© 2014 Microsoft