forked from kfayzullin/RMixpanel
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmixpanelJQLQuery.R
72 lines (63 loc) · 2.02 KB
/
mixpanelJQLQuery.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
mixpanelJQLQuery <- function(
account, # Mixpanel account.
jqlString, # JQL script as string.
jqlScripts, # List of JQL script file names.
paths=".", # Paths to search JS files.
columnNames, # Column names for the resulting data.frame.
toNumeric=c() # Column indices which should be converted to numeric.
) {
## Write all JQL code into 1 script file.
filePath = paste("temp_", uuid::UUIDgenerate(), ".js", sep="")
on.exit( { unlink(filePath) } )
append <- FALSE
if(!missing(jqlString)) {
cat(jqlString, file=filePath, append=append)
append <- TRUE
}
if(!missing(jqlScripts)) {
for(i in 1:length(jqlScripts))
for(path in paths) {
fn <- file.path(path, jqlScripts[i])
if(file.exists((fn)))
cat(readLines(fn), file=filePath, append=append)
append <- TRUE
}
}
## Perform query by CURL.
curlCall <- paste0("curl https://mixpanel.com/api/2.0/jql ",
"-u ", account$apiSecret, ": ",
"--data-urlencode script@", filePath)
## Results could be truncated.
options(warn=-1)
results <- system(curlCall, intern=TRUE, ignore.stderr=TRUE)
options(warn=0)
jsonRes <- paste(results, collapse="")
## Parse to data.frame.
rawRes <- jsonlite::fromJSON(jsonRes)
## Error Handling.
if("error" %in% names(rawRes))
stop(paste0("\nJQL query stopped. Message:\n", rawRes$error))
if(length(rawRes) == 0)
return(as.data.frame(rawRes))
res <- c()
for(i in 1:length(rawRes)) {
ri <- rawRes[[i]]
if (class(ri) == "data.frame") {
if(length(res))
res <- cbind(res, ri)
else
res <- ri
} else {
x <- unlist(ri)
res <- cbind(res, matrix(x, length(ri), byrow=TRUE))
}
}
res <- as.data.frame(res, stringsAsFactors=FALSE)
if(!missing(columnNames))
colnames(res) <- columnNames
if (length(toNumeric) && toNumeric[1] < 0)
toNumeric <- (1:ncol(res))[toNumeric]
for(i in toNumeric)
res[, i] <- as.numeric(res[, i])
res
}