The rdfp package is the R implementation of the Double Click for Publishers (DFP) API and similar in comparison to the existing client libraries supported by Google. I created the package because no other tools had been created to support integration of R with the Doubleclick for Publishers platform and wanted something on par with existing, supported client libraries.
View source code on GitHub at: https://github.com/StevenMMortimer/rdfp
Anything that the DFP API supports, you can do with this R package!
# install from CRAN install.packages("rdfp") # or get the latest version available on GitHub using the devtools package # install.packages("devtools") devtools::install_github("StevenMMortimer/rdfp")
I recommend using the
lubridate packages along with
rdfp. The only required authentication parameter is specifying the Id of the DFP network you would like to connect to.
library(dplyr) library(lubridate) library(rdfp) options(rdfp.network_code = 123456789)
# Check current user or network user_info <- dfp_getCurrentUser() user_info network_info <- dfp_getCurrentNetwork() network_info
Custom fields are helpful for “tagging” DFP items with metadata that can later be used filtering or reporting. See the following link for Google’s explanation on their uses at https://support.google.com/dfp_premium/answer/2694303?hl=en.
# this creates an extra field on the USER entity type that denotes what shift # the user works during the day. First we create the field, then populate # with potential options since it is a dropdown field. request_data <- tibble(name='Shift', description='The shift that this user usually works.', entityType='USER', dataType='DROP_DOWN', visibility='FULL') dfp_createCustomFields_result <- dfp_createCustomFields(request_data) request_data <- tibble(customFieldId = rep(dfp_createCustomFields_result$id, 3), displayName = c('Morning', 'Afternoon', 'Evening')) dfp_createCustomFieldOptions_result <- dfp_createCustomFieldOptions(request_data)
DFP allows traffickers to create custom tags to better target line items on their site. For example, a certain section of the site or search term used by a visitor can be encoded as custom targeting keys and values that can later be used when creating orders and line items, and evaluating potential inventory. See the following link for Google’s explanation on their uses at https://support.google.com/dfp_premium/answer/188092?hl=en.
# create the key request_data <- list(keys=list(name='Test1', displayName='TestKey1', type='FREEFORM')) dfp_createCustomTargetingKeys_result <- dfp_createCustomTargetingKeys(request_data) # create the values request_data <- tibble(customTargetingKeyId = rep(dfp_createCustomTargetingKeys_result$id, 2), name = c('TestValue1', 'TestValue2'), displayName = c('TestValue1', 'TestValue2'), matchType = rep('EXACT', 2)) dfp_createCustomTargetingValues_result <- dfp_createCustomTargetingValues(request_data)
This example uses a test company as an advertiser and yourself as the trafficker, to create an order.
request_data <- list('filterStatement'=list('query'="WHERE name = 'TestCompany1'")) dfp_getCompaniesByStatement_result <- dfp_getCompaniesByStatement(request_data) request_data <- list(list(name=paste0('TestOrder'), startDateTime=list(date=list(year=2017, month=12, day=1), hour=0, minute=0, second=0, timeZoneID='America/New_York'), endDateTime=list(date=list(year=2017, month=12, day=31), hour=23, minute=59, second=59, timeZoneID='America/New_York'), notes='API Test Order', externalOrderId=99999, advertiserId=dfp_getCompaniesByStatement_result$id, traffickerId=dfp_getCurrentUser()$id)) dfp_createOrders_result <- dfp_createOrders(request_data)
Below is an example of how to get objects by Publishers Query Language (PQL) statement. The statement is constructed as a list of lists that are nested to emulate the hierarchy of the XML to be created. The example uses the
dfp_getLineItemsByStatement function from the LineItemService.
# Retrieve all Line Items that have a status of "DELIVERING" request_data <- list('filterStatement'=list('query'="WHERE status='DELIVERING'")) dfp_getLineItemsByStatement_result <- dfp_getLineItemsByStatement(request_data)
Below is an example of how to make a simple report request.
# create a reportJob object # reportJobs consist of a reportQuery # Documentation for the reportQuery object can be found in R using # ?dfp_ReportService_object_factory and searching for ReportQuery # Also online documentation is available that lists available child elements for reportQuery # https://developers.google.com/doubleclick-publishers/docs/reference/v201802/ReportService.ReportQuery request_data <- list(reportJob=list(reportQuery=list(dimensions='MONTH_AND_YEAR', dimensions='AD_UNIT_ID', adUnitView='FLAT', columns='TOTAL_INVENTORY_LEVEL_IMPRESSIONS', startDate=list(year=2018, month=3, day=1), endDate=list(year=2018, month=3, day=30), dateRangeType='CUSTOM_DATE' ))) # a convenience function has been provided to you to manage the report process workflow # if you would like more control, see the example below which moves through each step in the process report_data <- dfp_full_report_wrapper(request_data) head(report_data)
The rdfp package has quite a bit of unit test coverage to track any changes made between newly released versions of DFP (typically 3 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 https://github.com/StevenMMortimer/rdfp/blob/master/tests/testthat/test_ReportService.R.
If you want to know how to create a user, just look at the test for
request_data <- list(users=list(name="TestUser - 1", email="firstname.lastname@example.org", roleId=-1) ) dfp_createUsers_result <- dfp_createUsers(request_data)
This application uses other open source software components. The authentication components are mostly verbatim copies of the routines established in the googlesheets package https://github.com/jennybc/googlesheets. We acknowledge and are grateful to these developers for their contributions to open source.
The rdfp package is licensed under the MIT License (http://choosealicense.com/licenses/mit/)
If you have further questions please submit them via email or issue on GitHub at https://github.com/StevenMMortimer/rdfp/issues. Thank you!
This project was made with love and coffee. Studies show that I write R code 3x faster after drinking one cup coffee and that productivity scales linearly. Imagine what I could accomplish if you bought me 10 cups of coffee…
Thank you for your support!