-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogle Analytics - script v1_0.R
466 lines (405 loc) · 19 KB
/
Google Analytics - script v1_0.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# ---- 0. Setup ----
rm(list = ls())
library(googleAnalyticsR)
#library(googleAuthR)
library(dplyr)
library(tidyr)
library(magrittr)
library(ggplot2)
library(ggrepel)
library(modelr)
library(viridis)
library(scales)
library(forcats)
library(stringr)
library(FactoMineR)
rerun_yn <- FALSE
# ---- 1. Get data from Google Analytics ----
if(rerun_yn == TRUE){
# Connect R and Google Analytics
ga_auth()
# Generate a list of all the Google Analytics accounts you have access to
ga_accounts <- ga_account_list()
# ga_id contains the View ID that you want to query
ga_id <- pull(ga_accounts[2,1])
ga_id <- "ga:228717430"
# Download all data and store it in a dataframe
# blog was launched on September 11, 2020
ga_df <- google_analytics(ga_id,
date_range = c("2020-09-11", as.character(Sys.Date()-1)),
metrics = c("users", "sessions"),
dimensions = "date",
anti_sample = TRUE) # avoids sampling data
# add blog posts when they were launched
ga_df$event <- ""
ga_df %<>%
mutate(event = ifelse(date == "2020-09-11",
"You can't put a number to love, but researchers estimated one (blog launched)",
event),
event = ifelse(date == "2020-10-05",
"Are we chasing people 'out of our league'? (share in Instagram & Facebook story)",
event),
event = ifelse(date == "2020-10-12",
"You can't put a number to love, but researchers estimated one (share in Facebook post)",
event),
event = ifelse(date == "2020-10-23",
"Are we chasing people 'out of our league'? (share in Facebook post)",
event),
event = ifelse(date == "2020-11-20",
"People use online dating platforms just for fun, *but lying is fun too* (share in Facebook post)",
event),
event = ifelse(date == "2020-11-23",
"People use online dating platforms just for fun, *but lying is fun too* (share in Instagram & Facebook story)",
event),
event = ifelse(date == "2020-12-24",
"Things you need to know about Christmas gift-giving (share in Instagram & Facebook story)",
event),
event = ifelse(date == "2020-12-28",
"Things you need to know about Christmas gift-giving (share in Facebook post)",
event),
event = ifelse(date == "2021-01-26",
"Covid-19 changes everything, including dating (share in Facebook post)",
event),
event = ifelse(date == "2021-01-27",
"Covid-19 changes everything, including dating (share in Instagram & Facebook story)",
event)
)
# Obtain Google Analytics data by day of week
ga_byDayofWeek_df <- google_analytics(ga_id,
date_range = c("2020-09-11", as.character(Sys.Date()-1)),
metrics = "sessions",
dimensions = c("dayOfWeek","date")) %>%
mutate(dayOfWeek = factor(dayOfWeek,
levels = c(0:6),
labels = c("Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday")
)
)
# Obtain Google Analytics data by device category
ga_byDevice_df <- google_analytics(ga_id,
date_range = c("2020-09-11", as.character(Sys.Date()-1)),
metrics = c("sessions", "avgSessionDuration"),
dimensions = c("date", "deviceCategory")
)
# Obtain Google Analytics data by browser
ga_byBrowser_df <- google_analytics(ga_id,
date_range = c("2020-09-11",as.character(Sys.Date()-1)),
metrics = c("sessions", "users", "goalCompletionsAll"),
dimensions = c("browser",
"browserVersion",
"browserSize",
"screenResolution",
"mobileDeviceinfo")
)
# Obtain Google Analytics data by channel
ga_byChannel_df <- google_analytics(ga_id,
date_range = c("2020-09-11",as.character(Sys.Date()-1)),
metrics = "users",
dimensions = c("date",
"deviceCategory",
"channelGrouping")
)
# Obtain Google Analytics data by continent and country
ga_byCountry_df <- google_analytics(ga_id,
date_range = c("2020-09-11",as.character(Sys.Date()-1)),
metrics = "sessions",
dimensions = c("date",
"continent",
"country",
"city",
"latitude",
"longitude"
)
)
# Some observations have missing latitude and longitude (no city was recorded)
# Recode missing coordinates for Romania
ga_byCountry_df$latitude <- ifelse(ga_byCountry_df$latitude == "0.0000" &
ga_byCountry_df$country == "Romania",
"45.9443",
ga_byCountry_df$latitude)
ga_byCountry_df$longitude<- ifelse(ga_byCountry_df$longitude == "0.0000" &
ga_byCountry_df$country == "Romania",
"25.0094",
ga_byCountry_df$longitude)
# Recode missing coordinates for United States
ga_byCountry_df$latitude <- ifelse(ga_byCountry_df$latitude == "0.0000" &
ga_byCountry_df$country == "United States",
"37.6000",
ga_byCountry_df$latitude)
ga_byCountry_df$longitude<- ifelse(ga_byCountry_df$longitude == "0.0000" &
ga_byCountry_df$country == "United States",
"-95.6650",
ga_byCountry_df$longitude)
# Recode missing coordinates for Trinidad & Tobago
ga_byCountry_df$latitude <- ifelse(ga_byCountry_df$latitude == "0.0000" &
ga_byCountry_df$country == "Trinidad & Tobago",
"10.4437",
ga_byCountry_df$latitude)
ga_byCountry_df$longitude<- ifelse(ga_byCountry_df$longitude == "0.0000" &
ga_byCountry_df$country == "Trinidad & Tobago",
"-61.4191",
ga_byCountry_df$longitude)
# Recode missing coordinates for United Kingdom
ga_byCountry_df$latitude <- ifelse(ga_byCountry_df$latitude == "0.0000" &
ga_byCountry_df$country == "United Kingdom",
"55.3618",
ga_byCountry_df$latitude)
ga_byCountry_df$longitude<- ifelse(ga_byCountry_df$longitude == "0.0000" &
ga_byCountry_df$country == "United Kingdom",
"-3.4433",
ga_byCountry_df$longitude)
# Recode missing coordinates for Moldova
ga_byCountry_df$latitude <- ifelse(ga_byCountry_df$latitude == "0.0000" &
ga_byCountry_df$country == "Moldova",
"46.9804",
ga_byCountry_df$latitude)
ga_byCountry_df$longitude<- ifelse(ga_byCountry_df$longitude == "0.0000" &
ga_byCountry_df$country == "Moldova",
"28.3897",
ga_byCountry_df$longitude)
# Recode missing coordinates for Colombia
ga_byCountry_df$latitude <- ifelse(ga_byCountry_df$latitude == "0.0000" &
ga_byCountry_df$country == "Colombia",
"4.1157",
ga_byCountry_df$latitude)
ga_byCountry_df$longitude<- ifelse(ga_byCountry_df$longitude == "0.0000" &
ga_byCountry_df$country == "Colombia",
"-72.9301",
ga_byCountry_df$longitude)
# Recode missing coordinates for Norway
ga_byCountry_df$latitude <- ifelse(ga_byCountry_df$latitude == "0.0000" &
ga_byCountry_df$country == "Norway",
"64.5783",
ga_byCountry_df$latitude)
ga_byCountry_df$longitude<- ifelse(ga_byCountry_df$longitude == "0.0000" &
ga_byCountry_df$country == "Norway",
"17.8882",
ga_byCountry_df$longitude)
ga_byCountry_df %<>%
mutate(latitude = as.numeric(latitude),
longitude = as.numeric(longitude))
# Obtain Google Analytics data across multiple dimensions
ga_data_df <- google_analytics(
ga_id,
date_range = c("2020-09-11", as.character(Sys.Date()-1)),
metrics = c("sessions", "avgSessionDuration"),
dimensions = c("date",
"dayOfWeek",
"deviceCategory",
"channelGrouping",
"browser",
"continent",
"country",
"latitude",
"longitude" # maximum 9 dimensions are allowed
)) %>%
mutate(dayOfWeek = factor(dayOfWeek,
levels = c(0:6),
labels = c("Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday")
)
)
# Obtain Google Analytics data by page
ga_page_df <- google_analytics(
ga_id,
date_range = c("2020-09-11", as.character(Sys.Date()-1)),
metrics = "pageviews",
dimensions = c("date", "pagePath"))
# Save data
save(ga_df,
ga_byDayofWeek_df,
ga_byDevice_df,
ga_byBrowser_df,
ga_byChannel_df,
ga_byCountry_df,
ga_data_df,
ga_page_df,
file = "Google Analytics data - Feb-2021.Rdata"
)
} else load("Google Analytics data - Feb-2021.Rdata")
# ---- 2. Describe Google analytics data ----
# ---- Sessions evolution ----
# How did sessions evolve through time?
sessions.plot <- ga_df %>%
ggplot(aes(x = date, y = sessions, label = event)) +
geom_line() +
theme_light() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
plotly::ggplotly(sessions.plot)
# peaks were achieved when sharing the post on Facebook
# Daily users with labels
ggplot(ga_df, aes(x = date, y = users, label = users)) +
geom_line() +
geom_point()+
geom_label(aes(label = users),
size = 2,
fill = viridis(3)[2],
colour = "white",
fontface = "bold") +
scale_x_date(labels = scales::date_format("%m-%Y")) +
theme_light() +
ylab("Users") +
xlab("Date") +
ggtitle("Daily users between September 2020 and February 2021") +
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 15))
# ---- Page views ----
ga_page_df %>%
group_by(pagePath) %>%
summarize(total = sum(pageviews)) %>%
arrange(total) %>%
filter(total > 60) %>%
mutate(Page = c("Jan 2021 - Covid-19 changes everything, including dating",
"Dec 2020 - Thins you need to know about Christmas gift giving",
"Nov 2020 - People use online dating platforms just for fun, but *lying is fun too",
"Oct 2020 - Are we chasing people out of our league?",
"Sep 2020 - You can't put a number to love, but researchers estimated one",
"Home page"
),
Page = forcats::fct_reorder(factor(Page), total, .desc = FALSE)) %>%
ggplot(aes(x = Page, y = total)) +
geom_bar(aes(fill = Page), stat = "identity", show.legend = FALSE) +
coord_flip() +
scale_fill_manual(values = viridis::viridis(7), aesthetics = "fill") +
scale_x_discrete(labels = scales::wrap_format(50)) +
scale_y_continuous("Total page views") +
theme_light() +
theme(panel.grid = element_blank())
# ---- Day of week ----
ga_byDayofWeek_df %>%
group_by(dayOfWeek) %>%
summarize(totSessions = sum(sessions)) %>%
mutate(dayOfWeek = forcats::fct_reorder(factor(dayOfWeek), totSessions, .desc = FALSE)) %>%
ggplot(aes(x = dayOfWeek, y = totSessions)) +
geom_bar(aes(fill = dayOfWeek), stat = "identity", show.legend = FALSE) +
coord_flip() +
scale_fill_manual(values = viridis::viridis(8)[-8], aesthetics = "fill") +
scale_y_continuous("Total sessions") +
theme_light() +
theme(panel.grid = element_blank())
# ---- Devices ----
# Plot sessions with deviceCategory
ga_byDevice_df %>%
ggplot(aes(x = deviceCategory, y = sessions, fill = deviceCategory)) +
geom_bar(stat = "identity", position = position_dodge(-.9), show.legend = TRUE) +
theme_light() +
scale_fill_manual(values = viridis::viridis(5), aesthetics = "fill") +
coord_flip() +
theme(panel.grid = element_blank())
# mostly mobile users
# Plot average session duration and device category
ga_byDevice_df %>%
ggplot(aes(x = deviceCategory, y = avgSessionDuration, fill = deviceCategory)) +
geom_bar(stat = "identity", position = position_dodge(-.9)) +
theme_light() +
scale_fill_manual(values = viridis::viridis(5), aesthetics = "fill") +
coord_flip() +
theme(panel.grid = element_blank())
# desktop users have a higher average session duration
# do they read more carefully or just forget the tab open?
# ---- Browser ----
# Plot ordered by browser count
ga_byBrowser_df %>%
count(browser) %>%
mutate(browser = forcats::fct_reorder(browser, n, .desc = TRUE)) %>%
ggplot(aes(x = browser, y = n)) +
geom_bar(aes(fill = browser), stat = "identity") +
theme_light() +
scale_fill_manual(values = viridis::viridis(7), aesthetics = "fill") +
scale_x_discrete(labels = scales::wrap_format(10)) +
ylab("Sessions by browser") +
theme(panel.grid = element_blank())
# ---- Channel ----
# Plot
ga_byChannel_df %>%
ggplot(aes(x = channelGrouping)) +
geom_bar(aes(fill = channelGrouping), show.legend = TRUE) +
facet_wrap(~ deviceCategory, ncol = 1) +
theme_light() +
scale_fill_manual(values = viridis::viridis(7), aesthetics = "fill") +
coord_flip() +
theme(panel.grid = element_blank())
# ---- Country ----
# Plot ordered by country count
ga_byCountry_df %>%
count(country) %>%
mutate(country = forcats::fct_reorder(country, n, .desc = FALSE)) %>%
ggplot(aes(x = country, y = n)) +
geom_bar(stat = "identity", show.legend = FALSE, fill = viridis::viridis(3)[2]) +
geom_text(aes(x = country, y = n, label = n),
size = 3.3,
fontface = "bold",
hjust = 0,
vjust = +.45) +
theme_light() +
coord_flip() +
theme(panel.grid = element_blank()) +
ylab("Sessions by country")
# ---- 3. Analyze Google analytics data ----
# Correspondence Analysis - browser & device category
CA(table(ga_data_df$browser, ga_data_df$deviceCategory), graph = TRUE)
# Correspondence Analysis - browser & channel
CA(table(ga_data_df$channelGrouping, ga_data_df$browser), graph = TRUE)
# Correspondence Analysis - channel & device
CA(table(ga_data_df$channelGrouping, ga_data_df$deviceCategory), graph = TRUE)
# Create categories of avg session duration
ga_data_df %<>%
mutate(avgSessionCategory = cut(avgSessionDuration,
breaks = c(-1, 2, 30, 420, 2000),
labels = c("Under 2s",
"2 - 30s",
"30s - 7min",
"Above 7 min")))
# When do people read more?
ga_data_df %>%
group_by(dayOfWeek) %>%
summarize(avgsession = mean(avgSessionDuration)) # Sunday, Friday
# CA: avg session vs day of week
ga_data_df %>%
group_by(dayOfWeek, avgSessionCategory) %>%
summarise(avgsession = mean(avgSessionDuration)) %>%
pivot_wider(names_from = avgSessionCategory,
values_from = avgsession) %>%
tibble::column_to_rownames(var = "dayOfWeek") %>%
CA(., graph = TRUE)
# CA: channel vs avg session duration
ga_data_df %>%
group_by(channelGrouping, avgSessionCategory) %>%
summarise(avgsession = mean(avgSessionDuration)) %>%
pivot_wider(names_from = avgSessionCategory,
values_from = avgsession) %>%
mutate(across(everything(), ~ replace_na(.x, 0))) %>%
tibble::column_to_rownames(var = "channelGrouping") %>%
CA(., graph = TRUE)
# CA: channel vs day of week
ga_data_df %>%
group_by(channelGrouping, dayOfWeek) %>%
summarise(avgsession = mean(avgSessionDuration)) %>%
pivot_wider(names_from = dayOfWeek,
values_from = avgsession) %>%
mutate(across(everything(), ~ replace_na(.x, 0))) %>%
tibble::column_to_rownames(var = "channelGrouping") %>%
CA(., graph = TRUE)
# Sessions by day of week - popular days
ga_data_df %>%
group_by(dayOfWeek) %>%
summarise(totSessions = sum(sessions)) %>%
arrange(desc(totSessions))
# CA: browser vs day of week
ga_data_df %>%
group_by(browser, dayOfWeek) %>%
summarise(avgsession = mean(avgSessionDuration)) %>%
pivot_wider(names_from = dayOfWeek,
values_from = avgsession) %>%
mutate(across(everything(), ~ replace_na(.x, 0))) %>%
tibble::column_to_rownames(var = "browser") %>%
CA(., graph = TRUE)