How do I import data from CleverTap for analysis using R


#1

I would like to do my own analysis on my data using R. Do you have connector ?


#2

Here is a basic example in R using the package “httr”:

library(httr)
library(jsonlite)

# Create a variable which has your api url as follows:
url <- "https://api.clevertap.com/api/events.json?id=xxx"
f <- "{event_name:'xxxx',from:yyyymmdd,to:yyyymmdd}"
r <- POST(url,body = f)
resp_id <- unlist(content(r, "parsed", "application/json"))["CONTENT.req_id"]
url_resp <- paste0(url, "&req_id=")
file <- paste0(url_resp, resp_id)
jsonData <- fromJSON(file)

#3

Hi, how do I integrate authentication in R?

Documentation says that: “Request Authentication - Every request to the API should include your CleverTap Account ID and CleverTap Account Passcode in the request headers keyed with ‘X-CleverTap-Account-Id’ and ‘X-CleverTap-Passcode’, respectively.”

but I cannot find any examples.
Can you help, please? Thanks


#4

Hi Laura,

If you are using the ‘httr’ package, you can use the GET function to add headers to your request.

Example:

url <- `https://api.clevertap.com/1/events.json?`; 

q <- '{"event_name":"App Launched","from":20151201,"to":20151212}';

r  <- GET(url, query = list(query = q), add_headers('X-CleverTap-Account-Id' = "XXXX", 'X-CleverTap-Passcode' = "XXXXX")); cursor <-unlist(content(r, "parsed", "application/json"))['cursor']

Let us know if this works out for you please!


#5

hi, i want use R get data with below code

url <- "https://api.clevertap.com/1/events.json?"; 
q <- '{"event_name":"issue#5492","from":20170718,"to":20170718}';
r  <- GET(url, query = list(query = q), 
          add_headers('X-CleverTap-Account-Id' = "xxxx", 
                      'X-CleverTap-Passcode' = "xxxx")); 
cursor <-unlist(content(r, "parsed", "application/json"))['cursor']
url_resp <- paste0(url, "&req_id=")
file <- paste0(url_resp, cursor)
jsonData <- fromJSON(file)

but it appear

Error in open.connection(con, "rb") : HTTP error 500.

Can you help, please? Thanks


#6

Hey, Iam getting the same error. Were you able to fix this?


#7

Hey,

Please use httr and jsonlite package for better result.

In the above request I can able to see it’s GET request so you can’t pass any Payload in GET request.

Please refer the code mention below for GET request

API call GET request with header

library(httr)
library(jsonlite)
urlNew <- paste(“https://api.clevertap.com/1/events.json","?",“cursor”,"=”,cursor,sep = “”)
urlNew
newRes <- httr::GET(url = urlNew,
httr::add_headers(‘Content-Type’ = ‘application/json’),
httr::add_headers(‘Accept’ = ‘application/json’),
httr::add_headers(‘X-CleverTap-Account-Id’ = ‘XXX-XXX-XXXX’),
httr::add_headers(‘X-CleverTap-Passcode’ = ‘xxxxxx’),
encode = “json”)

jsonData <- content(newRes,“text”)

key points: if you want to bind any data to URL please refer the formatting mentioned below:
urlNew <- paste(“https://api.clevertap.com/1/events.json","?",“cursor”,"=”,cursor, sep = “”)

API call POST request with header

library(httr)
library(jsonlite)
url <- “https://api.clevertap.com/1/events.json?batch_size=5000
requestBody <- paste0(’{
“event_name”: “App Launched”,
“from”: 20171030,
“to”: 20171130
}’)

res <- httr::POST(url = url,
httr::add_headers(‘Content-Type’ = ‘application/json’),
httr::add_headers(‘Accept’ = ‘application/json’),
httr::add_headers(‘X-CleverTap-Account-Id’ = ‘XXX-XXX-XXXX’),
httr::add_headers(‘X-CleverTap-Passcode’ = ‘XXXXXXXXX’),
body = requestBody,
encode = “json”)

response <- content(res,“text”)
response

Support Docs: https://support.clevertap.com/docs/api/getting-started.html

Hope this helps!

Please let us know if you need any clarification on the same.


#8

Thanks for the reply. But i want to import the data from clevertap for a event. Please find the code below:
library(httr)
library(RJSONIO)

url <- ‘https://api.clevertap.com/1/events.json
f = “{‘event_name’: ‘Video Player Error’, ‘from’: 20171201, ‘to’: 20171205}”
r <- POST(url, body=f, add_headers(‘X-CleverTap-Account-Id’ = “XXX-XXX-XXXX”,
‘X-CleverTap-Passcode’ = “XXX-XXX-XXXX”))

cursor <-unlist(content(r, “parsed”, “application/json”))[‘cursor’]
url_resp <- paste0(url, “?cursor=”)
file <- paste0(url_resp, cursor)
RJSONIO::fromJSON(file)

I get error: Error in file(con, “r”) :
cannot open the connection to 'https://api.clevertap.com/1/events.json?cursor=XXXXXXXX


#9

Hey Mirza,

You’ll need to hit a POST request code is mentioned below:

library(httr)
library(jsonlite)
url <- “https://api.clevertap.com/1/events.json?batch_size=5000
requestBody <- paste0(’{
“event_name”: “App Launched”,
“from”: 20171030,
“to”: 20171130
}’)

res <- httr::POST(url = url,
httr::add_headers(‘Content-Type’ = ‘application/json’),
httr::add_headers(‘Accept’ = ‘application/json’),
httr::add_headers(‘X-CleverTap-Account-Id’ = ‘XXX-XXX-XXXX’),
httr::add_headers(‘X-CleverTap-Passcode’ = ‘XXXXXXXXX’),
body = requestBody,
encode = “json”)

response <- content(res,“text”)
response

Now you’ll need to parse the data and get the cursor here.

Now in the next API you’ll need to bind the cursor in URL and hit a GET request code is mentioned below:

library(httr)
library(jsonlite)
urlNew <- paste(“https://api.clevertap.com/1/events.json","?",“cursor”,"=”,cursor,sep = “”)
urlNew
newRes <- httr::GET(url = urlNew,
httr::add_headers(‘Content-Type’ = ‘application/json’),
httr::add_headers(‘Accept’ = ‘application/json’),
httr::add_headers(‘X-CleverTap-Account-Id’ = ‘XXX-XXX-XXXX’),
httr::add_headers(‘X-CleverTap-Passcode’ = ‘xxxxxx’),
encode = “json”)

jsonData <- content(newRes,“text”)

Please refer our support docs for the better understanding of CleverTap APIs: https://support.clevertap.com/docs/api/getting-started.html

You need to pass the proper payload to proper endpoint here. The error you’re getting above because you’re passing the wrong payload to wrong endpoint.

Hope this helps!


#10

How to add event property and session property at the same time in requestBody?


#11

Hey @Vipul_Agrawal,

You can include event property and session property in the following manner:

{
    "event_name": "Product viewed",
    "event_properties": [
            {
                "name": "Product Name",
                "operator": "contains",
                "value": "Nike shoes"
            }
            ],
    "session_properties": [
        {
            "name": "first_time",
            "value": "True"
        }
    ],
    "from": 20171201,
    "to": 20180111
}

Documentation Link: https://support.clevertap.com/docs/api/understanding-clevertap-query-language.html#session-property-filters

Hope this helps.


#12

Hey, I need to know how I can input

“An Event” between some date
“Session Property”
with
Filter by users who
DID
App Launched
First time equals yes

in requestbody?


#13

Hey Palash,

response <- content(res,“text”) - Here the typeof response is character and i want it in a tabular format like in a dataframe. How do i get in dataframe format?

thanks


#14

Hi @Mirza_Hashim,

Once you get the response then you can simply parse the data in DataFrame. Please find the code snippet.

data_frame_variable_json <- fromJSON(jsonData, flatten = TRUE)
final_data_frame <- as.data.frame(data_frame_variable_json)
View(final_data_frame)```

Hope this helps!

#15

Hi @Palash
Can we update the cursor itself instead of manually binding it in the next cursor if there are records??