rhttrlocalytics

How to include / exclude filter statement in R httr query for Localytics


I can successfully query data from Localytics using R, such as the following example:

r <- POST(url = "https://api.localytics.com/v1/query,
           body=list(app_id=<APP_ID>,
                     metrics=c("occurrences","users"),
                     dimensions=c('a:URI'),
                     conditions=list(day = c("between", "2020-02-11", "2020-03-12"),
                                     event_name = "Content Viewed",
                                     "a:Item URI" = "testing")
           ),
           encode="json",
           authenticate(Key,Secret),
           accept("application/json"),
           content_type("application/json"))
stop_for_status(r)

But what I would like to do is create a function so I can do this quickly and not have to copy/paste data.

The issue I am running into is with the line "a:Item URI" = "testing", where I am filtering all searches by the Item URI where they all equal "testing", but sometimes, I don't want to include the filter statement, so I just remove that line entirely.

When I wrote my function, I tried something like the following:

get_localytics <- function(appID, metrics, dimensions, from = Sys.Date()-30, 
                           to = Sys.Date(), eventName = "Content Viewed",
                           Key, Secret, filterDim = NULL, filterCriteria = NULL){

  r <- httr::POST(url = "https://api.localytics.com/v1/query",
                  body = list(app_id = appID,
                              metrics = metrics,
                              dimensions = dimensions,
                              conditions = list(day = c("between", as.character(from), as.character(to)),
                                                event_name = eventName,
                                                filterDim = filterCriteria)
                              ),
                  encode="json",
                  authenticate(Key, Secret),
                  accept("application/json"),
                  content_type("application/json"))
  stop_for_status(r)
  result <- paste(rawToChar(r$content),collapse = "")  
  document <- fromJSON(result)                                      
  df <- document$results
  return(df)
}

But my attempt at adding filterDim and filterCriteria only produce the error Unprocessable Entity. (Keep in mind, there are lots of variables I can filter by, not just "a:Item URI" so I need to be able to manipulate that as well.

How can I include a statement, where if I need to filter, I can incorporate that line, but if I don't need to filter, that line isn't included?


Solution

  • conditions is just a list, so you can conditionally add elements to it. Here we just use an if statement to test of the values are passed and if so, add them in.

    get_localytics <- function(appID, metrics, dimensions, from = Sys.Date()-30, 
                               to = Sys.Date(), eventName = "Content Viewed",
                               Key, Secret, filterDim = NULL, filterCriteria = NULL){
    
      conditions <- list(day = c("between", as.character(from), as.character(to)),
                         event_name = eventName)
      if (!is.null(filterDim)  & !is.null(filterCriteria)) {
        conditions[[filterDim]] <- filterCriteria)
      }
    
      r <- httr::POST(url = "https://api.localytics.com/v1/query",
                      body = list(app_id = appID,
                                  metrics = metrics,
                                  dimensions = dimensions,
                                  conditions = conditions),
                      encode="json",
                      authenticate(Key, Secret),
                      accept("application/json"),
                      content_type("application/json"))
      stop_for_status(r)
      result <- paste(rawToChar(r$content),collapse = "")  
      document <- fromJSON(result)                                      
      df <- document$results
      return(df)
    }