Checking Availability with rdfp

Steven M. Mortimer


First, we load rdfp and specify the DFP network we would like to connect to. Then we authenticate by using dfp_auth(). Any existing cached token would be used or we will be prompted to authenticate via the browser.

options(rdfp.network_code = 123456789)

Availability by Month

Ad traffickers or display advertising reporters might get requests to determine the availability of a line item by month for a multi-month contract proposal. A LineItem has one InventoryTargeting object that describes which AdUnit and Placement objects it can target, and optional additional Targeting subclass objects that represent geographical, custom, or other criteria. You can either:

  1. Create your own line item from scratch
  2. Use dfp_getLineItemsByStatement() to pull down details on a line item and modify any fields that need to be different for your line item.

A LineItem from Scratch

Creating your own line item is easy. Some line item fields are absolutely required in order to forecast so you may need to try different lines to see how they forecast. Personally, I’ve found that most line items need the fields we create in the example below (e.g primaryGoal, targeting, etc). One quirk with the DFP API is that the fields on your line item must be provided in the same order as the reference documentation. You can review the documentation for this object at

You’ll notice that this example line item does not have targeting, which you can add. It also does not have a startDateTime and endDateTime. We’ll write a loop and pass those in each time to get availability for each month across multiple months.

Modifying an Existing LineItem

If you want to use an existing line item because it’s already got so many details, then here is an example of how you would pull that item down and use it. You will need to change out the filterStatement to pick the line item you want.

Creating Datetimes in DFP

DFP keeps track of dates and times by separating things like the year, month, and day into 3 separate integers. The special DFP format requires a function to convert Date objects in R into the corresponding DFP list. The following function is helpful in doing just that.

Generating Forecasts

Here is an example of how to loop through individual months and determine availability 6 months out for the sample line item created previously. It is fairly straightforward to take the month start and end dates and substitute them into the hypothetical line item. The one quirk is that, in order to follow the API, we must create lists of lists that are redundant at times. For example, the forecast request has a lineItem field that takes a ProspectiveLineItem that contains its own lineItem field: list(lineItem=list(lineItem=this_sample_line) .... As long as you follow the object fields according to the Google reference documentation, then everything should work. Most everything needs to be formed as a list of lists with field names.

Availability by Targeting Criteria

The previous example shows how to determine availability across multiple months for a single line item. It is also possible to determine the availability for each targeting segment of your forecast. The dfp_getAvailabilityForecast() function takes a second or two, so it is very inefficient to submit each county individually. Instead, you can submit a line item with targeting that says County A or County B or County C … until you’ve covered each county that you need a forecast for. If you ask for targeting criteria breakdowns in the forecastOptions, then you can retrieve the availability contribution from each county while only using one call to dfp_getAvailabilityForecast().

Determining Geo Ids

In this example, we’ll determine availability for all counties in Texas. First, you will need to determine the geography codes for all of the counties in Texas. Luckily, DFP provides a table of geographic codes for the entire world. Codes can be retrieved like so:

Next, we need to format the geographies into objects of type Location that at least contain an id denoting the area.

The sample line we created earlier does not contain the start and end datetimes, so we’ll consider a 90-day period in the future.

Setting up The Targeting

Specifying the targeting can be a little tricky. We need to NULL the original targeting field we created in the sample line and put geoTargeting first because the documentation and the API needs elements in the order that they are specified. For example, the ForecastService needs the targeting fields to be in the order listed at

Requesting Targeting Criteria Breakdowns

Finally, we’ll make the request to determine availability. There are 2 important details in getting back the breakdown of availability for each county in this example.

  1. Ensure that includeTargetingCriteriaBreakdown = 'true'
  2. Ensure that as_df = FALSE in dfp_getAvailabilityForecast()

Targeting criteria breakdowns are omitted by default so we need to tell DFP to return them. Those breakdowns are formatted as a nested list, so if you do not specify, you’ll be stuck with a very wide data.frame containing one column for every element in the nested list and it is not easy to work with.

Parsing Targeting Criteria Breakdowns

Once you’ve got the result, you’ll see an element in the list called targetingCriteriaBreakdowns. This element contains a breakdown for each targeting field specified, including the inventory targeting that is for the ad unit. We are not interested in that breakdown and it is not mutually exclusive from the geoTargeting, so we need to exclude. There are many ways to determine the break downs you want. In the example below we use sapply to find all breakdowns that have a dimension of 'GEOGRAPHY', but we could also check that the targetingCriteriaId is in the list of county ids. This all depends on which breakdowns you’re interested in and how you want to pull them out of the forecasted response.

The breakdowns are not necessarily mutually exclusive, so be careful when totaling them up. The counties are exclusive, so it’s okay to sum them.

Availability for an Existing Line

Sometimes you just want to check the availability on an existing line, say, to see if you can renew an existing contract. DFP makes this very easy, by providing the function dfp_getAvailabilityForecastById(). You’ll need to first determine the Id of the line item you would like to check, then plug it into the function. Note the empty list() provided to the forecastOptions argument. The API will error out if you do not provide it, but an empty list will quell those error messages and utilize the default forecast options.

# request for this id with no special forecastOptions
forecast_request <- list(lineItemId=single_item$id,
                         forecastOptions = list())

this_result <- dfp_getAvailabilityForecastById(forecast_request)
this_result[,c('lineItemId', 'orderId', 'availableUnits', 'deliveredUnits', 'matchedUnits')]
#> # A tibble: 1 x 5
#>   lineItemId    orderId availableUnits deliveredUnits matchedUnits
#>        <dbl>      <dbl>          <dbl>          <dbl>        <dbl>
#> 1 4889155175 2445937625          39470         120363        93375

Check out the Tests

The rdfp package has quite a bit of unit test coverage to track any changes made between newly released versions of DFP (typically 4 each year). These tests are an excellent source of examples because they cover most all cases of utilizing the package functions.

For example, if you’re not sure on how to use custom date ranges when requesting a report through the ReportService, just check out the tests at

If you want to know how to create a user, just look at the test for dfp_createUsers()

request_data <- list(users=list(name="TestUser - 1",
dfp_createUsers_result <- dfp_createUsers(request_data)