ploner / RMixpanel

RMixpanel - Mixpanel API client package for R
Other
18 stars 6 forks source link

Issue with function mixpanelJQLQuery #9

Open alvared opened 3 years ago

alvared commented 3 years ago

Hi,

I am having an issue when I call mixpanelJQLQuery() - I get an error as follows image

this is the code snippet:

jql_que <- "
function main() {
  return Events({
    from_date: '2021-06-01',
    to_date:   '2021-06-01',
    event_selectors: [
        {event : 'appOpened'}
      ]
  })
}" 

appOpened <- mixpanelJQLQuery(account, jqlString = jql_que)

The credentials for "account" are set up correctly as I am able to run mixpanelGetEvents() with no issue. I can also run the JQL code in Mixpanel and it returns results. Please let me know if there is something I am missing with this function call - I am running R version 4.1.1

Thanks very much!

jdeboer commented 3 years ago

The mixpanelJQLQuery function invokes an operating system call to curl, which you can see here in the package source code. I suspect that this error might occur if the bundled certificates located by curl are not maintained within the operating system, such as if they are outdated.

Removing this dependency in RMixpanel might be a way to avoid such issues from preventing the proper functioning of this package. Here is an example I have written of how the Mixpanel API could be queried within R without invoking the operating system's shell. This example uses the httr package to handle the request instead:

library(httr)
library(readr)

mp_jql_as_json <- function(jql_filename, api_secret) {
  response <- POST(
    url = "https://mixpanel.com/api/2.0/jql",
    authenticate(api_secret, ""),
    encode = "form",
    body = list(
      script = read_file(jql_filename)
    )
  )
  content(response)
}

results_json <- mp_jql_as_json(jql_filename = "test.jql", api_secret = "################################")

print(results_json)

The above could be used as a skeleton to rework the mixpanelJQLQuery function (as well as other functions within the RMixpanel package) to remove the need to invoke curl via the operating system's shell.