0

I am using the RGoogleAnalytics library to get all the data from my Google Analytics Account into R. However, complex queries deliver 0 results.

My code looks like:

  query.list <- Init(start.date = paste(c(lastmonth.startdate)),
                     end.date = paste(c(lastmonth.enddate)),
                     metrics = "ga:goalCompletionsAll",
                     dimensions = "ga:countryIsoCode,ga:yearMonth",
                     filters = "ga:goalCompletionsAll>0",
                     max.results = 10000,
                     table.id = sprintf("ga:%s", sites$profile.id[i]))

  # Create the Query Builder object so that the query parameters are validated
  ga.query <- QueryBuilder(query.list)

  # Extract the data and store it in a data-frame
  ga.countriesConversions1 <- GetReportData(ga.query, token)

Everything is inside a "for", and the script stops if one of the queries end in 0 results, because GetReportData(ga.query, token) cannot create a dataframe if there is no data.

I would like to know if there is a way use the warning message ("Your query matched 0 results. Please verify your query using the Query Feed Explorer and re-run it") fired by the library to the console, assign it to a variable and use this as an if condition. So I could create a dummy data.frame before the next function comes.

1 Answer 1

1

Assuming getReportData is throwing an error, then you can try:

ga.countriesConversions1 <- try(GetReportData(ga.query, token), silent=TRUE)
if(inherits(ga.countriesConversions1, "try-error")) {
  warning(geterrmessage())
  ... error handling logic ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Really intersting this try function. I didn't know that R had a proper function for that task. Let me see if I understand the logic: you try to excute GetReportData, and if this results in an error, R assign an invisible object class "try-error". You print the warning message at the console, but it is handled with the creation of a dummy data.frame. Isn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.