Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement Request: Return All Segments via ga_segment_list() #174

Closed
justinjm opened this issue May 16, 2018 · 3 comments
Closed

Enhancement Request: Return All Segments via ga_segment_list() #174

justinjm opened this issue May 16, 2018 · 3 comments

Comments

@justinjm
Copy link

Hello,

Please find below an enhancement/feature request for returning all GA segments :)

Thank you in advance!
Justin

What goes wrong

The ga_segment_list() function returns only the first 1,000 advanced segments (source)

Steps to reproduce the problem

# authentication ----------------------------------------------------------
## load R package to export data from GA API
## more info on setup here: http://code.markedmondson.me/googleAnalyticsR/setup.html
library(googleAnalyticsR)

## authenticate with GA API via OAuth2.0
## web browswer will open and choose appropriate GA account to login with
ga_auth()

# extract data from GA API ------------------------------------------------
## get list of segments from GA API
## save as nested list to preserve original data exported
my_segments <- ga_segment_list()

## extract relevant segement data stored in nested dataframe
## and save as a new dataframe to preserve the original data
data_out <- my_segments$items

## total available segments
my_segments$total

## total extracted segments
nrow(data_out)

Expected output

Total number of advanced segments

Actual output

Only first 1,000 (ascending order by segment title)

Session Info

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.4

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] googleAnalyticsR_0.5.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16      tidyr_0.8.0       digest_0.6.15     dplyr_0.7.4       assertthat_0.2.0  R6_2.2.2         
 [7] jsonlite_1.5      magrittr_1.5      pillar_1.2.2      httr_1.3.1        rlang_0.2.0       curl_3.2         
[13] bindrcpp_0.2.2    googleAuthR_0.6.2 tools_3.5.0       glue_1.2.0        purrr_0.2.4       yaml_2.1.19      
[19] compiler_3.5.0    pkgconfig_2.0.1   memoise_1.1.0     openssl_1.0.1     bindr_0.1.1       tibble_1.4.2     
@justinjm
Copy link
Author

justinjm commented Jun 6, 2018

In case anyone else is in a pinch, I was able to cobble together a passable solution to get up to 2k advanced segments but it's likely not worthy of a pull request. It's also likely this use case is uncommon :)

Summary

Use the get_all_segments() function below in place of ga_segment_list() function to return up to 2k advanced segments.

Source Code

## load custom function
#' Get segments user has access to
#'
#' @return Segment list
#' @importFrom googleAuthR gar_api_generator
#' @family managementAPI functions
#' @export
get_all_segments <- function(){
  
  url <- "https://www.googleapis.com/analytics/v3/management/segments"
  
  segs <- googleAuthR::gar_api_generator(baseURI = url,
                                         http_header = "GET",
                                         data_parse_function = function(x) x)
  req <- segs()
  
  ## page through list if necessary
  if(req$totalResults > 1000){
    
    nl <- req$nextLink
    
    segs2 <- googleAuthR::gar_api_generator(baseURI = url,
                                            http_header = "GET",
                                            pars_args = c(list(`start-index` = 1001)),
                                            data_parse_function = function(x) x)
    req2 <- segs2()
    
    req_all <- rbind(req$items, req2$items)
  }
  
  req_all
  
}

## load package
library(googleAnalyticsR)

## authenticate 
ga_auth()

## use new function get up to 2k segments
my_segments <- get_all_segments()

@MarkEdmondson1234
Copy link
Collaborator

MarkEdmondson1234 commented Nov 10, 2018

This is working but you need to install the "paging" gitHub branch of googleAuthR and googleAnalyticsR - can you see if it works with ga_segment_list() ?

Install via:

remotes::install_github("MarkEdmondson1234/googleAuthR", ref = "paging")
remotes::install_github("MarkEdmondson1234/googleAnalyticsR", ref = "paging")

Code to test:

library(googleAnalyticsR)
# should return as many segments you have, even if over 1000
ga_segment_list()

@justinjm
Copy link
Author

@MarkEdmondson1234 thank you for the fix and following up on this. Confirm this works and ga_segment_list() returns all 1,393 segments for the GA user account I used :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants