How to get hourly data for campaigns with Bing Ads API

Mariano Buglione 0 Reputation points
2023-07-31T16:24:09.5766667+00:00

Good afternoon team,

Currently at work we are facing the difficulty of getting the hourly data for the campaigns through the API. We can bring the data but not the same as shown in the Bing Ads UI.

We currently have a process for this written with the Python SDK that works, but we need to be able to replicate this with the API. Below are both examples both the python code and the Xml request in the API.
Python code:

days = int(os.environ.get("DAYS", 0))
    time = reporting_service.factory.create('ReportTime')
    time.CustomDateRangeEnd = reporting_service.factory.create('Date')
    today = extract_time - timedelta(days= days)
    today_est = extract_time.astimezone(pytz.timezone('US/Eastern'))
    time.CustomDateRangeEnd.Day = today_est.day
    time.CustomDateRangeEnd.Month = today_est.month
    time.CustomDateRangeEnd.Year = today_est.year
    beginning = today_est - timedelta(days=days)
    time.CustomDateRangeStart = reporting_service.factory.create('Date')
    time.CustomDateRangeStart.Day = beginning.day if extract_time.hour > 3 else (beginning -    timedelta(days=days)).day
    time.CustomDateRangeStart.Month = beginning.month
    time.CustomDateRangeStart.Year = beginning.year
    # time.PredefinedTime = 'LastSevenDays'
    time.ReportTimeZone = 'EasternTimeUSCanada'    report_request = reporting_service.factory.create('CampaignPerformanceReportRequest')
    report_request.Aggregation = 'Hourly'
    report_request.Format = report_file_format
    report_request.ReturnOnlyCompleteData = return_only_complete_data
    report_request.Time = time
    report_request.ReportName = "Hourly Bing Report"
    scope = reporting_service.factory.create('AccountThroughCampaignReportScope')
    scope.AccountIds = {'long': [account_id]}
    report_request.Scope = scope
    report_columns = reporting_service.factory.create('ArrayOfCampaignPerformanceReportColumn')
    report_columns.CampaignPerformanceReportColumn.append([
         'TimePeriod'
        'CampaignId'
        'CampaignName'
        'DeviceType'
        'Network'
        'Impressions'
        'Clicks'
        'Spend'
        'AbsoluteTopImpressionRatePercent'
        'AccountId'
        'AccountName'
        'AccountNumber'
        'AccountStatus'
        'AdDistribution'
        'AdRelevance'
        'AllConversionRate'
        'AllConversions'
        'AllCostPerConversion'
        'AllReturnOnAdSpend'
        'AllRevenue'
        'AllRevenuePerConversion'
        'Assists'
        'AverageCpc'
        'AverageCpm'
        'AveragePosition'
        'BaseCampaignId'
        'CampaignLabels'
        'CampaignStatus'
        'CampaignType'
        'ConversionRate'
        'Conversions'
        'CostPerAssist'
        'CostPerConversion'
        'Ctr'
        'CurrencyCode'
        'CustomerId'
        'CustomerName'
        'CustomParameters'
        'DeliveredMatchType'
        'ExpectedCtr'
        'FinalUrlSuffix'
        'LandingPageExperience'
        'LowQualityClicks'
        'LowQualityClicksPercent'
        'LowQualityConversionRate'
        'LowQualityConversions'
        'LowQualityGeneralClicks'
        'LowQualityImpressions'
        'LowQualityImpressionsPercent'
        'LowQualitySophisticatedClicks'
        'PhoneCalls'
        'Ptr'
        'QualityScore'
        'ReturnOnAdSpend'
        'Revenue'
        'RevenuePerAssist'
        'RevenuePerConversion</
    ])
    report_request.Columns = report_columns 

Xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>auth_token</AuthenticationToken>
    <CustomerAccountId>account_id</CustomerAccountId>
    <CustomerId>customer_id</CustomerId>
    <DeveloperToken>developer_token</DeveloperToken>
  </env:Header>
  <env:Body>
    <tns:SubmitGenerateReportRequest>
     <ReportRequest xsi:nil="false" xsi:type="CampaignPerformanceReportRequest">
                <ExcludeColumnHeaders>false</ExcludeColumnHeaders>
                <ExcludeReportFooter>true</ExcludeReportFooter>
                <ExcludeReportHeader>true</ExcludeReportHeader>
                <Format>Csv</Format>
                <Language>English</Language>
                <ReportName>Hourly Bing Report</ReportName>
                <ReturnOnlyCompleteData>false</ReturnOnlyCompleteData>
                <Aggregation>Hourly</Aggregation>
                <Columns>
                    <CampaignPerformanceReportColumn>TimePeriod</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CampaignId</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CampaignName</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>DeviceType</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>Network</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>Impressions</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>Clicks</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>Spend</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AbsoluteTopImpressionRatePercent</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AccountId</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AccountName</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AccountNumber</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AccountStatus</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AdDistribution</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AdRelevance</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AllConversionRate</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AllConversions</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AllCostPerConversion</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AllReturnOnAdSpend</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AllRevenue</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AllRevenuePerConversion</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>Assists</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AverageCpc</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AverageCpm</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>AveragePosition</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>BaseCampaignId</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CampaignLabels</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CampaignStatus</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CampaignType</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>ConversionRate</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>Conversions</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CostPerAssist</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CostPerConversion</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>Ctr</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CurrencyCode</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CustomerId</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CustomerName</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>CustomParameters</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>DeliveredMatchType</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>ExpectedCtr</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>FinalUrlSuffix</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>LandingPageExperience</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>LowQualityClicks</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>LowQualityClicksPercent</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>LowQualityConversionRate</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>LowQualityConversions</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>LowQualityGeneralClicks</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>LowQualityImpressions</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>LowQualityImpressionsPercent</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>LowQualitySophisticatedClicks</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>PhoneCalls</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>Ptr</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>QualityScore</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>ReturnOnAdSpend</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>Revenue</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>RevenuePerAssist</CampaignPerformanceReportColumn>
                    <CampaignPerformanceReportColumn>RevenuePerConversion</CampaignPerformanceReportColumn>
                </Columns>
                <Filter xsi:nil="true"/>
                <Scope>
                    <AccountIds xmlns:a1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                        <a1:long>account_id</a1:long>
                    </AccountIds>
                </Scope>
                <Time>
                    <CustomDateRangeEnd>
                        <Day>27</Day>
                        <Month>07</Month>
                        <Year>2023</Year>
                    </CustomDateRangeEnd>
                    <CustomDateRangeStart>
                        <Day>27</Day>
                        <Month>07</Month>
                        <Year>2023</Year>
                    </CustomDateRangeStart>
                    <PredefinedTime xsi:nil="true"/>
                    <ReportTimeZone>EasternTimeUSCanada</ReportTimeZone>
                </Time>
            </ReportRequest>
    </tns:SubmitGenerateReportRequest>
  </env:Body>
</env:Envelope>

We want to know if our API request replicates what we have in the Python code, if you need more specific information we could continue by mail, thank you for your attention.

Best regards,

Microsoft Advertising API
Microsoft Advertising API
A Microsoft API that provides programmatic access to Microsoft Advertising to manage large campaigns or to integrate your marketing with other in-house systems.
371 questions
0 comments No comments
{count} votes