-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWAW Demo.R
327 lines (273 loc) · 8.22 KB
/
WAW Demo.R
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# Which package should you use?
# Available through CRAN
# ======================
install.packages("RGoogleAnalytics")
library(RGoogleAnalytics)
# library(RGoogleAnalyticsPremium) # Alternative version for GA Premium.
# library(bigrquery) # CRAN version does not work; use GitHub version instead.
# Available through GitHub
# ========================
devtools::install_github("MarkEdmondson1234/googleAnalyticsR_public")
library(googleAnalyticsR)
devtools::install_github("andrewgeisler/GAR")
library(GAR) # Authentication does not seem to work.
devtools::install_github("jdeboer/ganalytics")
library(ganalytics)
devtools::install_github("rstats-db/bigrquery")
library(bigrquery)
# Two packages with the same name by two different people
# In Windows OS, these will overwrite each other.
install.packages("RGA")
library(RGA)
devtools::install_github("skardhamar/rga")
library(rga)
# To saves lots of retyping, let's define some parameters we'll be using.
user_name <- "[email protected]"
client_id <- "144394141628-8m5i5icva7akegi3tp6215d9eg9o5cln.apps.googleusercontent.com"
client_secret <- "wlFmhluHqTdZw6UG22h5A2nr"
view_id <- "ga:106134121"
start_date <- "2016-01-01"
end_date <- "2016-01-31"
# Some other packages we'll be using for this demonstration
install.packages(c("tidyr", "ggplot2", "dplyr", "lubridate"))
library(tidyr)
library(ggplot2)
library(dplyr)
library(lubridate)
# A function we'll reuse to plot the data from each GA package being compared.
ga_plot <- function(ga_data) {
ga_data <- complete(
ga_data, dayOfWeek, hour, deviceCategory,
fill = list(sessions = 0, bounces = 0)
)
ga_data <- transform(
ga_data,
bounceRate = (bounces + 1) / (sessions + 1)
)
ggplot(ga_data) +
aes(dayOfWeek, hour, fill = bounceRate) +
geom_tile() + facet_wrap(~deviceCategory)
}
### RGA ###
# https://github.com/artemklevtsov/RGA
#
token <- authorize(
username = user_name,
client.id = client_id,
client.secret = client_secret
)
ga_data <- get_ga(
profileId = view_id,
start.date = start_date,
end.date = end_date,
metrics = "ga:sessions,ga:bounces",
dimensions = "ga:dayOfWeek,ga:hour,ga:deviceCategory",
sort = "ga:dayOfWeek,ga:hour",
token = token
)
head(ga_data)
summary(ga_data)
ga_plot(ga_data)
detach("package:RGA", unload = TRUE)
### RGoogleAnalytics and RGoogleAnalyticsPremium ###
# https://github.com/Tatvic/RGoogleAnalytics
# https://github.com/Tatvic/RGoogleAnalyticsPremium
#
token <- Auth(
client.id = client_id,
client.secret = client_secret
)
query_list <- Init(
start.date = start_date,
end.date = end_date,
dimensions = "ga:dayOfWeek,ga:hour,ga:deviceCategory",
metrics = "ga:sessions,ga:bounces",
sort = "ga:dayOfWeek,ga:hour",
table.id = view_id
)
ga_query <- QueryBuilder(query_list)
ga_data <- GetReportData(ga_query, token)
head(ga_data)
summary(ga_data)
ga_plot(ga_data)
### bigrquery ###
# https://github.com/rstats-db/bigrquery
sql <- "
SELECT device.isMobile AS isMobile,
hits.hour AS hour,
date,
SUM ( totals.visits ) AS sessions,
SUM ( totals.bounces ) AS bounces
FROM [google.com:analytics-bigquery:LondonCycleHelmet.ga_sessions_20130910]
GROUP BY isMobile, hour, date
ORDER BY date, hour;
"
ga_data <- query_exec(sql, project = "960582788010")
head(ga_data)
ga_data <- ga_data %>%
mutate(
isTablet = FALSE,
deviceCategory = c("Desktop", "Mobile", "Tablet")[isMobile + isTablet + 1][1],
dayOfWeek = wday(ymd(date)) - 1
)
ga_data <- ga_data %>% select(dayOfWeek, hour, deviceCategory, sessions, bounces)
summary(ga_data)
ga_plot(ga_data)
### rga ###
# https://github.com/skardhamar/rga
rga.open(instance = "ga", where = "rga_auth.RData")
ga_data <- ga$getData(
view_id, start.date = start_date, end.date = end_date,
metrics = "ga:sessions,ga:bounces",
dimensions = "ga:dayOfWeek,ga:hour,ga:deviceCategory",
sort = "ga:dayOfWeek,ga:hour"
)
head(ga_data)
summary(ga_data)
ga_plot(ga_data)
# googleAnalyticsR
# https://github.com/MarkEdmondson1234/googleAnalyticsR_public
ga_auth()
ga_data <- google_analytics(
id = view_id,
start = start_date, end = end_date,
metrics = c("sessions", "bounces"),
dimensions = c("dayOfWeek", "hour", "deviceCategory"),
sort = c("ga:dayOfWeek,ga:hour")
)
head(ga_data)
summary(ga_data)
ga_plot(ga_data)
# ganalytics
# https://github.com/jdeboer/ganalytics
GoogleApiCreds(userName = user_name, appCreds = "client_secret.json")
query <- GaQuery(
view = view_id,
startDate = start_date, endDate = end_date,
metrics = c("sessions", "bounces"),
dimensions = c("dayOfWeek", "hour", "deviceCategory"),
sortBy = c("dayOfWeek", "hour")
)
ga_data <- GetGaData(query)
head(ga_data)
summary(ga_data)
ga_plot(ga_data)
### ganalytics examples ###
# Installation and "hello world!"
devtools::install_github("jdeboer/ganalytics")
library(ganalytics)
creds <- GoogleApiCreds(
"[email protected]", # Your Google username
"client_secret.json" # From Google APIs console
)
# Default view of first account and property
view_id <- NA
query <- GaQuery(view_id, creds)
GetGaData(query)
# Example 1
q <- GaQuery(view_id, creds)
DateRange(q) <- c("2015-10-27", "2015-11-26")
Dimensions(q) <- c("userGender", "deviceCategory", "channelGroup")
Metrics(q) <- c("users", "sessions", "pageviews")
GetGaData(q)
# Example 2
played_video <-
Expr(~EventCategory == "Video") &
Expr(~EventAction == "Play")
purchased_ticket <-
PerHit(
Expr(~goal1completions > 0)
)
journey <- PerUser(Sequence(
played_video,
Then(purchased_ticket)
))
cat(as(journey, "character"))
# Example 3
query <- GaQuery(
view = view_id,
startDate = "2015-01-01", endDate = "2015-01-31",
metrics = c("users", "sessions", "pageviews"),
dimensions = c("deviceCategory", "dayOfWeekName"),
filter = Expr(~deviceCategory %matches% "mobile|tablet"),
segment = Expr(~country == "Australia")
)
response <- GetGaData(query)
# Example 4
e1 <- Expr(~keyword %starts_with% 'buy')
e2 <- Expr(~city %matches% '^(Sydney|Melbourne)$')
e3 <- Expr(~deviceCategory == 'tablet')
e4 <- e1 & (e2 | e3)
as(e4, 'character')
# Example 5
# Average number of visits per hour and day – split by desktop, mobile and tablet
query <- GaQuery(view_id)
DateRange(query) <- c("2015-01-01", "2015-12-31")
Dimensions(query) <- c(
"deviceCategory", "dayOfWeekName", "hour", "date"
)
Metrics(query) <- "sessions"
MaxResults(query) <- 30000
data <- GetGaData(query)
library(dplyr)
weekly_data <- tbl_df(data) %>%
group_by(deviceCategory, dayOfWeekName, hour) %>%
summarise(avg_sessions_per_day = mean(sessions))
library(ggplot2)
ggplot(weekly_data) +
aes(
x = hour,
y = avg_sessions_per_day,
fill = deviceCategory,
group = deviceCategory
) +
geom_area(position = "stack") +
facet_wrap(~dayOfWeekName)
# Example 6
# Define sequences to segment users by permutations of device type and compare against total users of each type of device.
Dimensions(query) <- NULL
Metrics(query) <- c("users")
DateRange(query) <- c("2015-01-01", "2015-03-31")
# Maximum of 90 days for user-based segmentation
devices <- list(
desktop = Expr(~deviceCategory == "desktop"),
mobile = Expr(~deviceCategory == "mobile"),
tablet = Expr(~deviceCategory == "tablet")
)
device_sequences <- lapply(devices, function(from) {
lapply(devices, function(to) {
SegmentFilters(
Sequence(First(from), Then(to)),
scope = "users"
)
})
})
data <- lapply(seq_along(device_sequences), function(from_index){
from_name <- names(device_sequences)[from_index]
from_seq <- device_sequences[[from_index]]
lapply(seq_along(from_seq), function(to_index){
to_name <- names(from_seq)[to_index]
Segment(query) <- from_seq[[to_index]]
df <- GetGaData(query)
df <- cbind(from = from_name, to = to_name, df)
})
})
data <- unlist(data, recursive = FALSE)
data <- do.call(rbind, data)
library(dplyr)
benchmarks <- data %>%
subset(from == to) %>%
select(from, benchmark = users)
data <- data %>%
subset(from != to) %>%
inner_join(benchmarks, by = "from") %>%
group_by(from, to) %>%
summarise(transitioned = users / benchmark)
library(ggplot2)
library(scales)
ggplot(data) +
aes(x = from, y = to, fill = transitioned) +
geom_tile() +
scale_fill_continuous(labels = percent) +
theme_minimal(base_size = 18) +
theme(axis.title.y = element_text(angle = 0))