8-bit-sheep / googleAnalyticsR

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

JSON gets parsed to a bunch of garbage when two queries are ran with the same view ID #401

Closed owowouwu closed 2 years ago

owowouwu commented 2 years ago

What goes wrong

When I run a couple of queries everything is fine, but when I go to run a query again with the same view ID I used before I get an error "Unknown name "viewID" at 'report_requests[0]' : Proto field is not repeating, cannot start list".

Steps to Reproduce

I run

XXXX <- google_analytics(viewId = XXXX,
                   date_range = c('2022-05-01', '2022-07-31'),
                   metrics = c("users", "sessions", "newUsers", "percentNewSessions", "bounceRate", "goal1completions",
                               "goal4completions", "goal6completions"),
                   dimensions = c("segment","date"),
                   segments = list(organic_seg, paid_seg) 

then run

XXXX <- google_analytics(viewId = XXXX,
                   date_range = c('2021-05-01', '2021-07-31'),
                   metrics = c("users", "sessions", "newUsers", "percentNewSessions", "bounceRate", "goal1completions",
                               "goal4completions", "goal6completions"),
                   dimensions = c("segment","date"),
                   segments = list(organic_seg, paid_seg) 

same view ID, but different date. Segments are built in GA segments. I will get the error.

Output

Console output with options(googleAuthR.verbose=2) - image it goes even longer than this and is obviously an invalid payload.

Session Info

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C                       LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.1          stringr_1.4.0          dplyr_1.0.7            purrr_0.3.4            readr_2.0.2           
 [6] tidyr_1.1.4            tibble_3.1.6           ggplot2_3.3.5          tidyverse_1.3.1        googleAnalyticsR_1.0.1
[11] googleAuthR_2.0.0     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7                lubridate_1.8.0           assertthat_0.2.1          digest_0.6.29             utf8_1.2.2               
 [6] R6_2.5.1                  cellranger_1.1.0          backports_1.3.0           reprex_2.0.1              evaluate_0.14            
[11] httr_1.4.2                pillar_1.6.4              rlang_0.4.12              curl_4.3.2                readxl_1.3.1             
[16] rstudioapi_0.13           jquerylib_0.1.4           whisker_0.4               rmarkdown_2.11            munsell_0.5.0            
[21] broom_0.7.10              httpuv_1.6.5              compiler_4.1.2            modelr_0.1.8              xfun_0.27                
[26] pkgconfig_2.0.3           askpass_1.1               htmltools_0.5.2           openssl_1.4.6             tidyselect_1.1.1         
[31] fansi_0.5.0               later_1.3.0               crayon_1.4.2              tzdb_0.2.0                dbplyr_2.1.1             
[36] withr_2.4.3               rappdirs_0.3.3            grid_4.1.2                jsonlite_1.7.2            gtable_0.3.0             
[41] lifecycle_1.0.1           DBI_1.1.1                 magrittr_2.0.1            scales_1.1.1              cli_3.1.0                
[46] stringi_1.7.6             cachem_1.0.6              promises_1.2.0.1          fs_1.5.2                  bslib_0.4.0              
[51] xml2_1.3.2                ellipsis_0.3.2            generics_0.1.1            vctrs_0.3.8               tools_4.1.2              
[56] measurementProtocol_0.1.0 glue_1.6.0                hms_1.1.1                 fastmap_1.1.0             yaml_2.2.1               
[61] colorspace_2.0-2          gargle_1.2.0              rvest_1.0.2               memoise_2.0.1             knitr_1.36               
[66] haven_2.4.3               sass_0.4.2                usethis_2.1.5            
MarkEdmondson1234 commented 2 years ago

Hmm, so running the same query twice you get different results? But you can reliably replicate the issue? Can you talk a little more on what you do before your queries, such as segment definition and authentication steps order?

Trying to make sure if it's a bug I can fix vs an API issue that needs to be kicked up to Google.

Thanks for the report!

owowouwu commented 2 years ago

Turns out it was an issue on my end, I was overwritting the viewId somewhere resulting in that garbage being sent through as the view ID.

Sorry for this, I'm feeling kind of stupid now but it is what it is

MarkEdmondson1234 commented 2 years ago

No worries! It could still be a bug if I can't defensively check what is sent as the viewId - was it a number or string?

owowouwu commented 2 years ago

I basically did something like this

viewId <- blah
viewId <- google_analytics(...)

viewID was the name of the property I was looking at so I thought it natural to do it this way but obviously it overwrote the actual ID so if I ran the query again it wouldn't work.

So I basically sent through a whole dataframe and it looks like it got turned into that json object I attached in the screenshot.