skardhamar / rga

R Google Analytics
186 stars 89 forks source link

Fix #36 #37

Closed artemklevtsov closed 10 years ago

artemklevtsov commented 10 years ago

Handling white spaces only around operators (issue #36).

artemklevtsov commented 10 years ago

Seems I found more universal solution:

filters <- "ga:eventCategory == Purchase Button || ga:sessions > 10000"
# available operators
ops <- c("==", "!=", ">", "<", ">=", "<=", "=@", "!@", "=-", "!-", "\\|\\|", "&&", "OR", "AND")
# make pattern for gsub
opsw <- paste("(\\ )+(", paste(ops, collapse = "|"), ")(\\ )+", sep = "")
# remove whitespaces around operators
filters <- gsub(opsw, "\\2", filters)
# replace logical operators
filters <- gsub("OR|\\|\\|", ",", filters)
filters <- gsub("AND|&&", ";", filters)
filters
#> [1] "ga:eventCategory==Purchase Button,ga:sessions>10000"
skardhamar commented 10 years ago

Looks great, could you commit it?

It's a good feature to have, however, I'm pretty sure that the official GA API does not allow whitespace in filters, that does not mean that rga shouldn't support it though.

artemklevtsov commented 10 years ago

Ok. commit and push it now.

artemklevtsov commented 10 years ago

I also have some ideas for further development:

  1. use httr::oauth2.0_token in .rga.getToken and send this token with GET(url, config = config(token = token)) inrga` methods.
  2. Extract class definition code from ..extend.* functions and export rga class with package load.
  3. Add docs for the methods. Get help: rga$help("getData"). Doc source example:
rga$methods(
    list(
        getData = function(ids, start.date = format(Sys.Date() - 8, "%Y-%m-%d"),
                           end.date = format(Sys.Date() - 1, "%Y-%m-%d"), date.format = "%Y-%m-%d",
                           metrics = "ga:users,ga:sessions,ga:pageviews", dimensions = "ga:date",
                           sort = "", filters = "", segment = "", fields = "",
                           start = 1, max, messages = TRUE,  batch, walk = FALSE,
                           output.raw, output.formats, return.url = FALSE, rbr = FALSE, envir = .GlobalEnv) {
'Description
This method provide query the Core Reporting API for Google Analytics report data.

Arguments
ids: Google Analytics profile ID. Can be character (with or without "ga:" prefix) or integer.
start.date: Start date for fetching Analytics data in YYYY-MM-DD format.
end.date: End date for fetching Analytics data in YYYY-MM-DD format.
metrics: A comma-separated list of Analytics metrics, such as ga:sessions,ga:bounces.
dimensions: A comma-separated list of Analytics dimensions, such as ga:browser,ga:city.
sort: A comma-separated list of dimensions or metrics that determine the sort order for Analytics data.
filters: A comma-separated list of dimension or metric filters to be applied to Analytics data
segment: An Analytics segment to be applied to data.
fields: Selector specifying which fields to include in a partial response.
start: An index of the first entity to retrieve.
max: The maximum number of entries to include in this feed.
messages: Show message during process data.
batch: Extracting more then 10000 observations per pull. Can be logical or integer.
walk: Get the data unsampled.
output.raw: Specify variable for a copy ouput raw data without processing.
output.formats: Specify variable for a copy ouput data formats.
return.url: return request URL only.'

I already tested points 2 and 3. What do you think?

skardhamar commented 10 years ago

Woa, good idea.

I've been wanting to update a lot of the core functions of this package for a while now, adding the help is a good idea!

The package is using the S4, which is not really documented, so I was thinking about downgrading it to S3. Which would make it more likely to be accepted by CRAN as well.

artemklevtsov commented 10 years ago

Your package based on R5 (refClass) as I inderstand. And it realy good choise. I'll make pull request with changes about I sayd to above.

On github there is package based on S4 - https://github.com/jdeboer/ganalytics. Although it has less functionality.

Also I think we should exract duplicated code in methods to separate funcitons (for example, mcf and core have some duplications).

Yet interesting thing for a tab completion within rga class (source - http://stackoverflow.com/a/12545344/1863950):

# Tab completions for rga clas
.DollarNames.rga <- function(x, pattern) {
    grep(pattern, getRefClass(class(x))$methods(), value = TRUE)
}