rdfp

Overview

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.

GitHub - rdfp View source code on GitHub at: https://github.com/StevenMMortimer/rdfp

Features

Anything that the DFP API supports, you can do with this R package!

  • Basic CRUD operations on DFP objects (Create, Read, Update, Delete)
  • Forecasting/Inventory Management/Reporting
  • Simple Administrative Tools

Quickstart Guide

Install rdfp Library

# 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")

Setup and Authenticate

I recommend using the dplyr and 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 Info

# Check current user or network
user_info <- dfp_getCurrentUser()
user_info
network_info <- dfp_getCurrentNetwork()
network_info

Setup Custom Fields for Items

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)

Setup Custom Targeting Keys and Values

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)

Create an Order

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)

Get Line Items By A Filter

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)

Run a Report

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)

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 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 dfp_createUsers()

request_data <- list(users=list(name="TestUser - 1",
                                email="testuser123456789@gmail.com",
                                roleId=-1)
                     )
dfp_createUsers_result <- dfp_createUsers(request_data)

Credits

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.

License

The rdfp package is licensed under the MIT License (http://choosealicense.com/licenses/mit/)

Questions

If you have further questions please submit them via email or issue on GitHub at https://github.com/StevenMMortimer/rdfp/issues. Thank you!

Support

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!