RGA package Lexical Error

623 views Asked by At

I am using RGA to extract Google Analytics data into R. I am having difficulties with segments that have spaces. When running this code everything works fine:

kw <- "iPhone"
seg <- paste('sessions::condition::ga:searchKeyword=@',kw,sep="")
data <- ga$getData(ids, start.date="2014-12-14", end.date="2014-12-14", 
                   metrics = "ga:sessions", dimensions = "", 
                   sort = "", filters = "", segment = seg)

But when running this code (only difference is in kw):

kw <- "iPhone 6"
seg <- paste('sessions::condition::ga:searchKeyword=@',kw,sep="")
data <- ga$getData(ids, start.date="2014-12-14", end.date="2014-12-14", 
                   metrics = "ga:sessions", dimensions = "", 
                   sort = "", filters = "", segment = seg)

I get this error:

Error in parseJSON(txt) : lexical error: invalid char in json text.
                                       <!DOCTYPE html> <html lang=en> 
                     (right here) ------^

Thanks a lot for your help!

1

There are 1 answers

0
Linda Lawton - DaImTo On BEST ANSWER

I think the problem is that the string you are sending in seg needs to be HTML encoded

iPhone 6 vs iPhone%206

The error message that it cant parse the JSon I think it because its returning a HTML error message to you. You can test segments here Query Explorer

Sorry I don't know R I just just worked extensively with the Google Analytics API