forked from 8-bit-sheep/googleAnalyticsR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ganalytics.Rmd
125 lines (91 loc) · 4.21 KB
/
ganalytics.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---
title: "ganalytics - Advanced dynamic segment and Filter examples"
author: "Johann de Boer"
---
[`ganalytics`](https://CRAN.R-project.org/package=ganalytics) provides functions that makes it easy to define filters and segments using natural R language comparison and logical operators. This example demonstrates how to define dynamic segments using functions from the `ganalytics` package and using those segments with the `googleAnalyticsR` package. You need `googleAnalyticsR>=0.6.0` and `ganalytics>=0.10.6`.
More examples are available at the [ganalytics README](https://github.com/jdeboer/ganalytics).
## Setup
Load the `ganalytics` library as well as `googleAnalyticsR` to make use of the new syntax.
```r
library(googleAnalyticsR)
library(ganalytics)
# authenticate
ga_auth()
# set to your view Id
view_id <- 81416156
```
## Advanced Filter syntax
In this example, we'll define the following filters:
* Device category is desktop or tablet - a dimension filter using an OR condition.
* New visitors using either a desktop or tablet device - a dimension filter involving both an AND and an OR condition.
* At least one goal completion or transaction - a metric filter using an OR condition.
The above list of filters will be defined using `ganalytics` expressions as follows:
```r
# Device category is desktop or tablet - a dimension filter using an OR condition.
desktop_or_mobile <- Expr(~deviceCategory == "desktop") | Expr(~deviceCategory == "tablet")
# New visitors using either a desktop or tablet device - a dimension filter involving both an AND and an OR condition.
new_desktop_and_mobile_visitors <- Expr(~userType == "new") & desktop_or_mobile
# At least one goal completion or transaction - a metric filter using an OR condition.
at_least_one_conversion <- Expr(~goalCompletionsAll > 0) | Expr(~transactions > 0)
```
We can now use `googleAnalyticsR` to query the data with the above filters:
```r
results <- google_analytics(
viewId = view_id,
date_range = c("30daysAgo", "yesterday"),
metrics = c("users", "sessions", "goalCompletionsAll"),
dimensions = c("deviceCategory", "userType"),
dim_filters = new_desktop_and_mobile_visitors,
met_filters = at_least_one_conversion
)
results
# deviceCategory userType users sessions goalCompletionsAll
#1 desktop New Visitor 2721 2726 600
#2 tablet New Visitor 67 67 13
```
## Advanced Segment Syntax
In this example, we'll define a list of six segments:
* Bounced sessions: Sessions where the bounces metric is not zero.
* Mobile or tablet sessions: Sessions by mobile and tablet users.
* Multi-session users: Users who have visited more than once during the defined date range.
The above list of dynamic segments is defined using `ganalytics` expressions as follows:
```r
bounces <- Expr(~bounces != 0)
mobile_or_tablet <- Expr(~deviceCategory %in% c("mobile", "tablet"))
multi_session_users <- Include(PerUser(Expr(~sessions > 1)), scope = "users")
my_segment_list <- list(
bounced_sessions = PerSession(bounces),
mobile_or_tablet = mobile_or_tablet,
multi_session_users = multi_session_users)
results <- google_analytics(
viewId = view_id,
date_range = c("30daysAgo", "yesterday"),
metrics = c("users", "sessions"),
dimensions = c("segment"),
segments = Segments(my_segment_list)
)
results
# segment users sessions
#1 bounced_sessions 3080 4070
#2 mobile_or_tablet 631 899
#3 multi_session_users 45 84
```
### More than 4 segments
The Google Analytics Reporting API can only be used to query 4 segments at a time, so if you have more than 4 you need to break the list segments into chunks:
```r
segment_chunks <- split(my_segment_list, (seq_along(my_segment_list) - 1L) %/% 4L)
```
We can now use `googleAnalyticsR` to query each chunk of segments and bind the results into a single `data.frame`. For each segment, we will request a count of users and sessions.
```r
library(purrr)
library(dplyr)
results <- map_df(segment_chunks, function(chunk) {
google_analytics(
viewId = view_id,
date_range = c(start_date, end_date),
metrics = c("users", "sessions"),
dimensions = c("segment"),
segments = Segments(chunk)
)
})
```