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

ganalytics DSL suggestions #2

Open
hadley opened this issue Sep 21, 2013 · 6 comments
Open

ganalytics DSL suggestions #2

hadley opened this issue Sep 21, 2013 · 6 comments

Comments

@hadley
Copy link

@hadley hadley commented Sep 21, 2013

Taking an example from http://www.slideshare.net/johanndeboer/web-analytics-with-r-melb-urn:

GaAnd( 
  GaExpr('keyword', '@', 'buy'), 
  GaOr( 
    GaExpr('city', '~', '^(Sydney|Melbourne)$'), 
    GaExpr('isTablet', '=', ‘Yes’) ) )

it would be relatively straightforward to allow you to write

GaExpr('keyword', '@', 'buy') && 
  (GaExpr('city', '~', '^(Sydney|Melbourne)$') || 
   GaExpr('isTablet', '=', ‘Yes’))

just by adding the appropriate methods for && and ||.

But I think it would be relatively straightforward to go even further and be able to write

GaAutoExpr(keyword %matches% "buy" && 
  (city %in% c("Sydney", "Melbourne") || isTablet == TRUE))

using the principles described in http://adv-r.had.co.nz/dsl.html.

@jdeboer
Copy link
Owner

@jdeboer jdeboer commented Mar 28, 2015

I like this idea very much Hadley and would like to aim for this.

@jdeboer
Copy link
Owner

@jdeboer jdeboer commented May 5, 2015

Hi @hadley, I've implemented your first suggestion above, but needed to add the methods to |, and & instead. This is because R reported an error when I attempted to add methods to ||, && as they are primitive functions.

Looking at the second part of your suggestion I can see how much easier this would make writing expressions. Mapping between the Google Analytics expression operators (comparators) and the equivalent R functions is my first step. Going along the lines of your examples above, I've come up with the following mapping table but would very much appreciate any further suggestions you can offer:

GA comparator Description R function Example for consideration
== Equals == x == y
!= Does not equal != x ! y
< Less than < x < y
<= Less than or equal to > x <= y
> Greater than > x > y
>= Greater than or equal to >= x >= y
=@ Contains ? x %contains% y
!@ Does not contain ? !(x %contains% y)
=~ Matches regex grepl(y, x) (?) x %matches% y
!~ Does not match regex !grepl(y, x) (?) !(x %matches% y)
[] Exact match within list (segments only) %in% x %in% c(a, b, c)
<> Within range (segments only) x >= y1 & x <= y2 x %between% c(y1, y2)

Any suggestions for the equivalent R functions for those where I have put a question mark?

If it helps, details about the above Google Analytics filter operators and segment operators can be found in the Google Analytics Core Reporting API developer reference.

@hadley
Copy link
Author

@hadley hadley commented May 5, 2015

For all of the infix operators that have no equivalent in base R, doing (e.g.) %!@% would be fine, I think. But you need to balance terseness with familiarity - if you think people are already familiar with these operators, I'd go with the minimal infix form. If knowledge of them is uncommon, the longer, more descriptive functions would be better.

@jdeboer
Copy link
Owner

@jdeboer jdeboer commented Aug 30, 2015

Just to keep everything in one place, capturing the suggestion by @hadley in June 2013: "...use the approach of https://github.com/hadley/dplyr/blob/master/R/translate-sql.r … to automatically convert R expressions into GA expressions."

Further to that, I note the use of lazyeval by the rex package - this approach also looks helpful in implementing this suggested enhancement.

@jdeboer
Copy link
Owner

@jdeboer jdeboer commented Aug 30, 2015

First attempt looks promising:

AutoExpr <- function(...) {
  expr <- lazy(...)$expr
  comparator <- as.character(expr[[1]])
  var <- as.character(expr[[2]])
  operand <- expr[[3]]
  Expr(var, comparator, eval(operand))
}

AutoExpr(pageviews > 1)

#> An object of class "gaMetExpr"
#> Slot "var":
#> An object of class "gaMetVar"
#> [1] "ga:pageviews"
#> 
#> Slot "comparator":
#> An object of class "gaMetComparator"
#> [1] ">"
#> 
#> Slot "operand":
#> An object of class "gaMetOperand"
#> [1] 1
@jdeboer
Copy link
Owner

@jdeboer jdeboer commented Aug 30, 2015

I have committed a solution to the dev branch: https://github.com/jdeboer/ganalytics/blob/dev/R/condition.R#L9 ... Any feedback would be welcome. I think there are opportunities to expand on this to go further towards the example expression Hadley provided at the start of this discussion.

Here is a an example to demonstrate use of this new function:

condition(pageviews > 1) &
  (
    condition(keyword %matches% "buy") |
    condition(city %in% c("Sydney", "Melbourne")
  )
)
@jdeboer jdeboer added the help wanted label Dec 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.