This topic has not yet been rated - Rate this topic

Update Ad Groups in Ruby

The following example shows how to update ad groups within a campaign using the Campaign Management UpdateAdGroups service operation.

This example has been developed and run within the environment described in Getting Started Using Ruby with Bing Ads Services.

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

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

def UpdateAdGroupsLib(
    username,
    password,
    devToken,
    customerId,
    accountId,
    campaignId,
    adgroupId)

    # Create the WSDL driver reference to access the Web service.
    wsdl = SOAP::WSDLDriverFactory.new("https://api.sandbox.bingads.microsoft.com/Api/Advertiser/v8/CampaignManagement/CampaignManagementService.svc?wsdl")
    
    service = wsdl.create_rpc_driver
    
    # For SOAP debugging information,
    # uncomment the following statement.
    # service.wiredump_dev = STDERR

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

    # Create a string literal that contains the SOAP request body.
    updateadgroupsrequest = %{
        <UpdateAdGroupsRequest xmlns="https://adcenter.microsoft.com/v8">
          <CampaignId>#{campaignId}</CampaignId>
          <AdGroups>
            <AdGroup>
              <ExactMatchBid>
                <Amount>10.50</Amount>
              </ExactMatchBid>
              <Id>#{adgroupId}</Id>
              <EndDate>
                <Month>12</Month>
                <Day>31</Day>
                <Year>2020</Year>
              </EndDate>
              <Name>Hand Wear Updated</Name>
              <PricingModel>Cpc</PricingModel>
            </AdGroup>
          </AdGroups>
        </UpdateAdGroupsRequest>
    }

    begin
        # Convert the string literal that contains
        # the SOAP request body to an XML document.
        request = REXML::Document.new(updateadgroupsrequest)
        
        # Call the service operation.
        response = service.UpdateAdGroups(request)

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

        # Capture any generic errors.
        if detail.respond_to?('adApiFaultDetail')
            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 any service operation exceptions.
        elsif detail.respond_to?('apiFaultDetail')
            detail = detail.apiFaultDetail

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

                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 ad group's array.
            if detail.batchErrors.respond_to?('batchError')
                batchErrors = detail.batchErrors.batchError

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

                batchErrors.each do |batchError|
                    print "Batch error" \
                        " '#{batchError.message}' (#{batchError.code})" \
                        " encountered at array index = #{batchError.index}.\n"
                end
            end

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

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

begin
    UpdateAdGroupsLib(
    "<usernamegoeshere>",
    "<passwordgoeshere>",
    "<devtokengoeshere>",
    <customeridgoeshere>,
    <accountidgoeshere>,
    <campaignidgoeshere>,
    <adgroupidgoeshere>)
end
Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.