rlocalytics

How do I extract data using data from Localytics using the httr package in R?


I am trying to extract data from Localytics using R. Here is the snippet of code I'm using:

library(httr)
localytics_url = 'https://api.localytics.com/v1/query'

r <- POST(url = localytics_url, 
      body=list(
        app_id=app_id, 
        metrics=c("users","revenue"), 
        dimensions=c("day","birth_day"), 
        conditions=list(
          day=c("between", "2015-02-01", "2015-04-01")
          )
        ),
      encode="json", 
    authenticate(key,secret),
    accept("application/json"),
    content_type("application/json")
  )
stop_for_status(r)
content(r)

But the output I get from content is binary, and not a json. I'm confused. Furthermore if I try to look at the object 'r', I see

Response [https://api.localytics.com/v1/query]
Date: 2015-04-14 15:18
Status: 200
Content-Type: application/vnd.localytics.v1+hal+json;type=ResultSet; charset=utf-8
Size: 1.02 MB
<BINARY BODY>

I don't understand why it's a binary body or how to convert it back. Can anyone provide me any help/clues?

I've also tried this with Rcurl using the following code:

cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")
object <- getForm(uri=localytics_url, app_id=app_id, metrics="customers", dimensions="day", conditions = toJSON(list(day=c("between", "2015-01-01", "2015-04-09"))), .opts=curlOptions(userpwd=sprintf("%s:%s", key, password))

But that generates the error

Error in function (type, msg, asError = TRUE)  : 
SSL certificate problem: unable to get local issuer certificate

So I'm a bit stumped.

######## Added April 15, 2015

First thanks to MrFlick for his help so far. I got it to work with

contents=content(r, as="text")

Thanks very much for your help. I (think I) had tried that before and then went on to try and extract it to an R data format using fromJSON, but I was using the rjson library, and the jsonlite package worked for me.

I appreciate your patience.


Solution

  • Here's a complete sample of code on how you would get the data, and then extract the results and view them as a table.

    library(httr)
    library(jsonlite)
    
    response <- POST(url = 'https://api.localytics.com/v1/query', 
                     body=list(
                       app_id='APP_ID',
                       metrics='sessions',
                       conditions=list(
                         day=c("between", format(Sys.Date() - 31, "%Y-%m-%d"), format(Sys.Date() - 1, "%Y-%m-%d"))
                       ),
                       dimensions=c('new_device','day')
                     ),
                     encode="json",
                     authenticate('KEY','SECRET'),
                     accept("application/json"),
                     content_type("application/json"))
    
    stop_for_status(response)
    # Convert the content of the result to a string, you can load with jsonlite
    result <- paste(rawToChar(response$content), collapse = "")
    # Useful to print your result incase you are getting any errors
    print(result)
    # Load your data with jsonlite
    document <- fromJSON(result)
    # The results tag contains the table of data you need
    View(document$results)