尝试使用Ruby on Rails中的Savon gem根据广告系列状态过滤Bing Ads API数据

时间:2019-05-10 14:12:14

标签: xml savon bing-ads-api

自从Savon gem将Ruby代码转换为XML以来,我已经能够在不了解XML的情况下做到这一点,但是现在我陷入了困境。我从API请求一个CampaignPerformanceReport,并且能够成功检索所需的数据,但是现在我需要添加一个过滤器以仅获取“已暂停”广告系列。

documentation之后,我将此消息发送到了API:

<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="https://bingads.microsoft.com/Reporting/v13" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns="https://bingads.microsoft.com/Reporting/v13">
  <env:Header>
    <AuthenticationToken>foo</AuthenticationToken>
    <CustomerAccountId>foo</CustomerAccountId>
    <CustomerId>foo</CustomerId>
    <DeveloperToken>foo</DeveloperToken>
  </env:Header>
  <env:Body>
    <tns:SubmitGenerateReportRequest>
      <ReportRequest xsi:nil="false" xsi:type="CampaignPerformanceReportRequest">
        <ExcludeColumnHeaders>true</ExcludeColumnHeaders>
        <ExcludeReportFooter>true</ExcludeReportFooter>
        <ExcludeReportHeader>true</ExcludeReportHeader>
        <Format>Csv</Format>
        <Language>English</Language>
        <ReportName>CampaignPerformanceReportRequest</ReportName>
        <ReturnOnlyCompleteData>false</ReturnOnlyCompleteData>
        <Aggregation>Summary</Aggregation>
        <Columns>
          <CampaignPerformanceReportColumn>CampaignName</CampaignPerformanceReportColumn>
          <CampaignPerformanceReportColumn>CampaignStatus</CampaignPerformanceReportColumn>
          <CampaignPerformanceReportColumn>Spend</CampaignPerformanceReportColumn>
          <CampaignPerformanceReportColumn>Impressions</CampaignPerformanceReportColumn>
          <CampaignPerformanceReportColumn>Clicks</CampaignPerformanceReportColumn>
          <CampaignPerformanceReportColumn>Conversions</CampaignPerformanceReportColumn>
          <CampaignPerformanceReportColumn>Revenue</CampaignPerformanceReportColumn>
          <CampaignPerformanceReportColumn>PhoneCalls</CampaignPerformanceReportColumn>
        </Columns>
        <Filter>
          <CampaignStatusReportFilter>Paused</CampaignStatusReportFilter>
        </Filter>
        <Scope>
          <AccountIds xmlns:a1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <a1:long>foo</a1:long>
          </AccountIds>
        </Scope>
        <Time>
          <CustomDateRangeEnd>
            <Day>31</Day>
            <Month>01</Month>
            <Year>2019</Year>
          </CustomDateRangeEnd>
          <CustomDateRangeStart>
            <Day>01</Day>
            <Month>01</Month>
            <Year>2019</Year>
          </CustomDateRangeStart>
          <PredefinedTime xsi:nil="true"/>
          <ReportTimeZone>EasternTimeUSCanada</ReportTimeZone>
        </Time>
      </ReportRequest>
    </tns:SubmitGenerateReportRequest>
  </env:Body>
</env:Envelope>

该请求被接受并处理,但是返回的数据仅显示“有效”广告系列。我确定我的过滤方式存在问题。这是我用来生成请求的Ruby代码。

report_request = {
      '@xsi:nil' => false,
      '@xsi:type' => 'CampaignPerformanceReportRequest',
      'ExcludeColumnHeaders' => true,
      'ExcludeReportFooter' => true,
      'ExcludeReportHeader' => true,
      'Format' => 'Csv',
      'Language' => 'English',
      'ReportName' => 'CampaignPerformanceReportRequest',
      'ReturnOnlyCompleteData' => false,
      'Aggregation' => 'Summary',
      'Columns' => {
         'CampaignPerformanceReportColumn' => [
           'CampaignName',
           'CampaignStatus',
           'Spend',
           'Impressions',
           'Clicks',
           'Conversions',
           'Revenue',
           'PhoneCalls'
         ]
      },
      'Filter' => { 'CampaignStatusReportFilter' => 'Paused' },
      'Scope' => {
        'AccountIds' => {
          '@xmlns:a1' => 'http://schemas.microsoft.com/2003/10/Serialization/Arrays',
          'a1:long' => account_id,
        }
      },
   etc...
}

我已尝试完全匹配上面链接的文档中的语法,但是它仍然仅返回“有效”广告系列,这使我相信这是默认设置,并且API只会忽略我的不当过滤。

如果有人可以帮助我弄清楚我在做什么错,那将不胜感激。

1 个答案:

答案 0 :(得分:1)

请注意,有多个子过滤器类型可用,例如AccountStatus,AdDistribution,DeviceOS,DeviceType和Status

您可以使用以下语法(我今天进行了测试并确认)仅包含设备类型为Tablet且广告系列状态为已暂停的报告数据。您可以删除设备数位板过滤器,因为我只是添加了它以显示如何添加多个过滤器。

<Filter>
    <AccountStatus i:nil="true"/>
    <AdDistribution i:nil="true"/>
    <DeviceOS i:nil="true"/>
    <DeviceType>Tablet</DeviceType>
    <Status>Paused</Status>
</Filter>

很抱歉,我手头没有Ruby示例,但这是一个C#代码段,可帮助在SOAP请求中生成上述XML:

Filter = new CampaignPerformanceReportFilter {
    DeviceType = DeviceTypeReportFilter.Tablet,
    Status = CampaignStatusReportFilter.Active
},

提示:每个Bing Ads API服务操作都包含一个语法模板,例如SubmitGenerateReport。您只需要精简适用于您的请求的部分,例如一次仅一种报告请求类型。

也请随时通过Bing Ads API Development forumcontact support链接与我们的团队联系。

我希望这会有所帮助!