8-bit-sheep / googleAnalyticsR

Use the Google Analytics API from R
https://8-bit-sheep.com/googleAnalyticsR/
Other
259 stars 76 forks source link

GA4/ga_data(): Error in if (grepl("^#", mm)) { : the condition has length > 1 #403

Closed elalbaicin closed 2 years ago

elalbaicin commented 2 years ago

What goes wrong

Whenever I try to extract data from GA4 with ga_data(), I get the following error:

Error in if (grepl("^#", mm)) { : the condition has length > 1
In addition: Warning message:
In pq$concurrentRequests < 10 || verbose :
  'length(x) = 2 > 1' in coercion to 'logical(1)'
Error: API Data failed to parse.  
             Wrote diagnostic object to 'gar_parse_error.rds', use googleAuthR::gar_debug_parsing('gar_parse_error.rds') to 
             debug the data_parse_function.

Steps to reproduce the problem

All session info is at the "Session Info" section; then it should suffice to run:

ga_data(propertyId = propertyId,
              date_range = c("2022-10-10", "2022-10-10"),
              dimensions = c("date"),
              metrics = c("sessions"),
              limit = -1)

By the way, I've tried omitting the limit argument to see if the issue persisted, and it still threw an error.

Expected output

A dataframe.

Actual output

The aforementioned error message.

Running options(googleAuthR.verbose=2) does not yield any output.

'API Data failed to parse' diagnostics

I've written the output of googleAuthR::gar_debug_parsing("gar_parse_error.rds") on this file: gar_parse_error.txt

Session Info

This file contains the output of sessionInfo(): session_info.txt

eniefaC commented 2 years ago

Same error here, I switched to the raw json method in order to do some urgent work, and it was working for a while, but know I also have an error while parsing the response :

Error in if (pq$tokensPerDay$consumed > (care_factor * tpd_remaining) || : missing value where TRUE/FALSE needed Error: API Data failed to parse.

I can see the correct API response in the debug file (so I guess the request is fine and I'm not over quotas), but I'm not technical enough so I have no idea if it is connected to the firt issue or why it stopped working suddenly. Given I've just started using the package and I'm not sure what I'm doing half of the time, I would guess the problem might be unrelated!

Might add debug files later if needed.

MarkEdmondson1234 commented 2 years ago

Thanks, sessionInfo will be helpful - upgraded to a new version of R? It may have caught an error that was a warning but raises an error now in new version.

Was the same query working in a previous version of R?

MarkEdmondson1234 commented 2 years ago

From the error messages, it looks like part of the api response that is expected to be a scalar (length 1) is now receiving a vector (length 2)

elalbaicin commented 2 years ago

Thanks, sessionInfo will be helpful - upgraded to a new version of R? It may have caught an error that was a warning but raises an error now in new version.

Was the same query working in a previous version of R?

I did upgrade to R 4.2.1 from R 4.2.0 recently, but when I downgraded back to 4.2.0 the exact same error ocurred. By the way, it stopped working on October 10, if it's any help.

eniefaC commented 2 years ago

upgraded to a new version of R?

I'm on a brand new install (R, R Studio, googleanalyticsr)

sessionInfo.txt

Thanks for your work.

MarkEdmondson1234 commented 2 years ago

It may be the API changed it's response, but will check it out

XoliloX commented 2 years ago

Hi all,

I have the same issue with same error message. The error gets triggered with ga_data() à but not with google_analytics()

sessioninfo.txt

Thanks for your support !

Best, D.

MarkEdmondson1234 commented 2 years ago

Should be fixed now, it was a change in the API response related to quotas.

> options(googleAuthR.verbose = 2)
> ga_data(ga_id,
+         date_range = c("2022-10-10", "2022-10-10"),
+         dimensions = c("date"),
+         metrics = c("sessions"),
+         limit = -1)
ℹ 2022-10-12 11:14:45 > Request:  https://analyticsdata.googleapis.com/v1beta/properties/206670707:runReport/
ℹ 2022-10-12 11:14:45 > Reading cache
ℹ 2022-10-12 11:14:45 > tokensPerDay: Query Cost [ 1 ] / Remaining [ 24992 ]
ℹ 2022-10-12 11:14:45 > tokensPerHour: Query Cost [ 1 ] / Remaining [ 4996 ]
ℹ 2022-10-12 11:14:45 > concurrentRequests Consumed / Remaining:  0  /  10
ℹ 2022-10-12 11:14:45 > serverErrorsPerProjectPerHour Consumed / Remaining:  0  /  10
ℹ 2022-10-12 11:14:45 > Downloaded [ 1 ] of total [ 1 ] rows
# A tibble: 1 × 2
  date       sessions
  <date>        <dbl>
1 2022-10-10       86

To install the fix now use remotes::install_github("MarkEdmondson1234/googleAnalyticsR")and I'll submit it to CRAN ASAP

XoliloX commented 2 years ago

Fixed, thanks a lot !!!

elalbaicin commented 2 years ago

It's working now. Thank you very much, Mark.

elliedigitalanalytics commented 2 years ago

Thanks for the fix!!!

MarkEdmondson1234 commented 2 years ago

This fix is on CRAN now so you can install normally, v1.1.0

eniefaC commented 2 years ago

Thank you for the great software and support!