Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
MSDN Library

Add Keywords in Ruby

The following example shows how to add keywords within an ad group using the Campaign Management AddKeywords service operation.

This example has been developed and run within the environment described in Getting Started Using Ruby 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.

require 'soap/wsdlDriver'
require 'soap/header/simplehandler'
require 'soap/baseData'

NS_SHARED = "https://adcenter.microsoft.com/v8"

# This is a helper class that is used
# to construct the request header.
class RequestHeader < SOAP::Header::SimpleHandler
    def initialize (element, value)
        super(XSD::QName.new(NS_SHARED, element))
        @element = element
        @value   = value
    end

    def on_simple_outbound
        @value
    end
end

# This is a helper class that is used to construct a
# Bid data object to use with a keyword.
class Bid
    def initialize(bidAmount)
    {
        @Amount => bidAmount
    }
    end

    def on_simple_outbound
        @Amount
    end
end

# This is a helper class that is used 
# to create a Keyword data object.
class Keyword
    attr_accessor :Text, :BroadMatchBid, 
        :ContentMatchBid, :EditorialStatus, :ExactMatchBid, 
        :Id, :NegativeKeywords, :Param1, 
        :Param2, :Param3, :PhraseMatchBid, :Status
end

def CreateKeywords(
    username, 
    password,
    appToken, 
    devToken,
    customerAccountId, 
    customerId, 
    adGroupId)

    # Create the WSDL driver reference to access the Web service.
    wsdl = SOAP::WSDLDriverFactory.new("https://sandboxapi.adcenter.microsoft.com/Api/Advertiser/v8/CampaignManagement/CampaignManagementService.svc?wsdl")

    # Create an instance of the CampaignManagement Web service.
    service = wsdl.create_rpc_driver

    # For SOAP debugging information,
    # uncomment the following statement.
    # client.wiredump_dev = STDERR

    # Set the header information.
    service.headerhandler << RequestHeader.new('ApplicationToken',
        "#{appToken}")
    service.headerhandler << RequestHeader.new('CustomerAccountId',
        "#{customerAccountId}")
    service.headerhandler << RequestHeader.new('CustomerId',
        "#{customerId}")
    service.headerhandler << RequestHeader.new('DeveloperToken',
        "#{devToken}")
    service.headerhandler << RequestHeader.new('UserName',
        "#{username}")
    service.headerhandler << RequestHeader.new('Password',
        "#{password}")

    keywords = Array.new

    # Create two keywords.
    keywords[0] = Keyword.new
    keywords[1] = Keyword.new

    # Specify the element values for the first keyword.
    keywords[0].Text             = 'mittens'
    keywords[0].BroadMatchBid    = Bid.new(0.50)
    keywords[0].EditorialStatus  = SOAP::SOAPNil.new
    keywords[0].Status           = SOAP::SOAPNil.new

    # Specify the element values for the second keyword.
    keywords[1].Text             = 'gloves'
    keywords[1].EditorialStatus  = SOAP::SOAPNil.new
    keywords[1].ExactMatchBid    = Bid.new(1.50)
    keywords[1].Status           = SOAP::SOAPNil.new

    

    request = 
    {
        :AdGroupId => "#{adGroupId}",
        :Keywords  => keywords
    }

    begin
        # Perform the service operation.
        response = service.AddKeywords(request)

        lists = response.keywordIds.long

        if !lists.respond_to?('each')
            lists = [lists]
        end

        # Display the keyword IDs for the successfully created keywords.
        for i in (0...lists.length)
            print "Keyword #{keywords[i].Text} " \
                "was created with " \
                "ID #{lists[i]}.\n"
        end

    # Exception handling.
    rescue SOAP::FaultError => fault
        detail = fault.detail

        if detail.respond_to?('adApiFaultDetail')

            # Get the AdApiFaultDetail object.
            adApiErrors = detail.adApiFaultDetail.errors.adApiError

            if !adApiErrors.respond_to?('each')
                adApiErrors = [adApiErrors]
            end

            adApiErrors.each do |error|
                print "Ad API error" \
                    " '#{error.message}' (#{error.code}) encountered.\n"
            end

        # Capture API exceptions.
        elsif detail.respond_to?('editorialApiFaultDetail')
            detail = detail.editorialApiFaultDetail

            # Display the editorial validation error information.
            if detail.respond_to?('editorialErrors')
                editErrors = detail.editorialErrors

                if !editErrors.respond_to?('each')
                    editErrors = [editErrors]
                end

                editErrors.each do |editError|
                    print "Editorial validation error." \
                        " Failed text:  '#{editError.disapprovedText}'\n"
                        " Message: '#{editError.message}'\n"
                        " Code: '#{editError.code}'\n"
                end
            end

            # Display the service operation error information.
            if detail.respond_to?('operationErrors')
                operationErrors = detail.operationErrors

                if !operationErrors.respond_to?('each')
                    operationErrors = [operationErrors]
                end
                
                operationErrors.each do |opError|
                    print "Operation error" \
                        " '#{opError.message}' (#{opError.code}) encountered.\n"
                end
            end

            # Display any batch error information.
            # The batchError.index property is the index to the 
            # campaign's array.
            if detail.respond_to?('batchErrors')
                batchErrors = detail.batchErrors.batchError

                if !batchErrors.respond_to?('each')
                    batchErrors = [batchErrors]
                end

                batchErrors.each do |batchError|
                    print "Batch error" \
                        " '#{batchError.message}' (#{batchError.code})" \
                        " encountered for keyword text" \
                        " #{keywords[Integer("#{batchError.index}")].Text}." \
                        " Keyword array index = #{batchError.index}.\n"
                end
            end

        # Capture any generic SOAP exceptions.
        else
            print "Generic SOAP fault" \
                " '#{detail.exceptionDetail.message}' encountered.\n"
        end

    # Capture exceptions on the client that are unrelated to
    # the Bing Ads API. An example would be an 
    # out-of-memory condition on the client.
    rescue Exception => e
        puts "Error '#{e.exception.message}' encountered."
    end
end

Community Additions

Show:
© 2015 Microsoft