-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGA_analysis.Rmd
537 lines (453 loc) · 18.5 KB
/
GA_analysis.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
---
title: "GA_analysis"
author: "Michael Breshock"
date: "6/25/2021"
output: html_document
editor_options:
chunk_output_type: console
---
remember to update
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(googleAnalyticsR)
library(ggplot2)
library(tidyverse)
library(magrittr)
library(ggmap)
library(leaflet)
library(ggpubr)
```
```{r}
theme_set(theme_linedraw() +
theme(axis.text = element_text(size = 11,
color = "black"),
axis.title = element_text(size = 12,
color = "black"),
plot.title = element_text(size = 14,
color = "black",
hjust = .5))
)
```
```{r sessionsdaily}
ocs_property_id = 256923228
dates = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metric = c("sessions"),
dimensions = c("date"), limit = 400)
dates
```
```{r devicecategory}
ocs_property_id = 256923228
ses_wk_ctg = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("deviceCategory"))
ses_wk_ctg %<>% mutate(percent = round(100*sessions/sum(sessions), 2),
total = sum(sessions))
ggplot(ses_wk_ctg, aes(x = as.factor(deviceCategory), y = sessions)) +
geom_bar(stat = "identity")
```
[Paramaters:](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema)
sessions: The number of sessions that began on your site or app (event triggered: session_start).
deviceCategory: The type of device: Desktop, Tablet, or Mobile.
```{r sources}
user_sources = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("newUsers"),
dimensions = c("firstUserSource")) %>%
subset(grepl("127",firstUserSource) == FALSE) %>%
mutate(firstUserSource = as_factor(firstUserSource),
firstUserSource = fct_infreq(firstUserSource))
ggplot(user_sources, aes(x = firstUserSource, y = newUsers)) +
geom_bar(stat = "identity") + theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
```
(t.co = twitter)
firstUserSource = The source that first acquired the user to your website or app.
newUsers: The number of users who interacted with your site or launched your app for the first time (event triggered: first_open or first_visit).
```{r usersweek}
users_wk = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("totalUsers", "newUsers", "eventCount",
"active7DayUsers", "userEngagementDuration"),
dimensions = c("week")) %>%
mutate(week = as.numeric(week),
avgEngagementDuration = # avg engagement in minutes
round(userEngagementDuration/(60*totalUsers)))
pivot_longer(users_wk, c(totalUsers, newUsers, active7DayUsers),
names_to = "metric", values_to = "count") %>%
ggplot(aes(x = week, y = count)) +
geom_line(aes(color = metric))
```
```{r usersday}
ocs_property_id = 256923228
users_date = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("totalUsers", "newUsers",
"active1DayUsers"),
dimensions = c("date"), limit = 1000) %>%
pivot_longer(c(newUsers, active1DayUsers),
names_to = "metric", values_to = "count")
ggplot(users_date, aes(x = date, y = count, color = metric)) +
geom_line() + #scale_color_viridis_d() +
labs(x = "Day", y = "User Count", title = "Open Case Studies Daily New and Active Users")
```
```{r usertotals}
#users totals
ocs_property_id = 256923228
total_users = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("totalUsers", "newUsers",
"active1DayUsers"))
avg_newusers = total_users$newUsers / nrow(dates)
avg_totusers = total_users$active1DayUsers / nrow(dates)
```
```{r avgengage}
ggplot(users_date, aes(x = as.factor(avgEngagementDuration),
fill = as.factor(avgEngagementDuration))) +
geom_bar() + scale_fill_viridis_d() + guides(fill = "none") +
labs(x = "Average Engagement Duration (Minutes)", y = "Number of Days",
title = "Count of Daily Average Engagement")
```
week: The week of the event, a two-digit number from 01 to 53. Each week starts on Sunday. January 1st is always in week 01. The first and last week of the year have fewer than 7 days in most years. Weeks other than the first and the last week of the year always have 7 days. For years where January 1st is a Sunday, the first week of that year and the last week of the prior year have 7 days.
totalUsers: The number of distinct users who have logged at least one event, regardless of whether the site or app was in use when that event was logged.
eventCount: The count of events.
active7DayUsers: The number of distinct active users on your site or app within a 7 day period. The 7 day period includes the last day in the report's date range.
userEngagementDuration: The total amount of time (in seconds) your website or app was in the foreground of users' devices.
```{r cities}
cities = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("country", "city"), limit = 1000)
cities %<>% mutate(
latitude = geocode(paste(city, country, sep = ", "))$lat,
longitude = geocode(paste(city, country, sep = ", "))$lon
)
cities
```
city: The city from which the user activity originated.
```{r}
worldmap = map_data("world")
cities = drop_na(cities)
save(cities, file = "city_coordinates.rda")
```
```{r bubblemap}
# Create a color palette with handmade bins.
mybins <- c(1, 5, 10, 50, 100, 1000, 2000)
mypalette <- colorBin(palette="Reds", domain = cities$sessions,
na.color="transparent", bins=mybins)
# Final Map
m <- leaflet(cities) %>%
addTiles() %>%
setView(lat = 0, lng = 0, zoom = 1) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(~longitude, ~latitude,
fillColor = ~mypalette(sessions), fillOpacity = 0.7, radius= ~sqrt(sessions), stroke=FALSE
) %>%
addLegend(pal=mypalette, values=~sessions, opacity=0.9, title = "Sessions", position = "topright" )
m
```
```{r topcities}
cities10 = drop_na(cities) %>% filter(country != "Peru") %>% head(10) %>%
mutate(city = case_when(
city == "(not set)" ~ " ",
TRUE ~ city)) %>%
unite(location, c(city, country), sep = "\n")
ggplot(cities10, aes(x = as_factor(location), y = sessions, fill = as_factor(location))) +
geom_col() + scale_fill_viridis_d() +
labs(x = "City, Country", y = "No. of Sessions",
title = "Top Ten Cities With Most Open Case Studies Sessions") +
guides(fill = "none") +
theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
scale_y_continuous() +
geom_text(aes(label = paste0(sessions)), vjust = -.5) + ylim(c(0,1150))
```
```{r, browser}
ocs_property_id = 256923228
browsers = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("browser"))
head(browsers)
```
```{r, dhm}
ocs_property_id = 256923228
dhm = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("hour"), limit = 1000)
dhm
```
```{r, mediums}
ocs_property_id = 256923228
mediums = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("medium"))
mediums
```
```{r url}
ocs_property_id = 256923228
urls = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("fullPageUrl"))
case_studies = c("ocs-bp-rural-and-urban-obesity",
"ocs-bp-air-pollution",
"ocs-bp-vaping-case-study",
"ocs-bp-opioid-rural-urban",
"ocs-bp-RTC-wrangling",
"ocs-bp-RTC-analysis",
"ocs-bp-youth-disconnection",
"ocs-bp-youth-mental-health",
"ocs-bp-school-shootings-dashboard",
"ocs-bp-co2-emissions",
"ocs-bp-diet")
pageUrl = urls %>% mutate(
interactive = str_detect(fullPageUrl, "rsconnect")) %>%
filter(str_detect(fullPageUrl, "opencasestudies.org") |
str_detect(fullPageUrl, "jhsph.edu/ocs")) %>%
mutate(
code = case_when(
str_detect(fullPageUrl, "ocs-bp-school-shootings-dashboard") == TRUE ~ "School Shootings",
str_detect(fullPageUrl, "ocs-bp-youth-disconnection") == TRUE ~ "Youth Disconnection",
str_detect(fullPageUrl, "ocs-bp-opioid-rural-urban") == TRUE ~ "Opiods",
str_detect(fullPageUrl, "ocs-bp-vaping-case-study") == TRUE ~ "Vaping",
str_detect(fullPageUrl, "ocs-bp-youth-mental-health") == TRUE ~ "Mental Health",
str_detect(fullPageUrl, "ocs-bp-rural-and-urban-obesity") == TRUE ~ "Obesity",
str_detect(fullPageUrl, "ocs-bp-RTC-analysis") == TRUE ~ "RTC Analysis",
str_detect(fullPageUrl, "ocs-bp-co2-emissions") == TRUE ~ "CO2 Emissions",
str_detect(fullPageUrl, "ocs-bp-RTC-wrangling") == TRUE ~ "RTC Wrangling",
str_detect(fullPageUrl, "ocs-bp-diet") == TRUE ~ "Diet",
str_detect(fullPageUrl, "ocs-bp-air-pollution") == TRUE ~ "Air Pollution"
)
)
pageTotals = pageUrl %>% group_by(code) %>%
mutate(ocs_sum = sum(sessions)) %>% select(code, ocs_sum) %>% distinct()
interactiveTotals = pageUrl %>% filter(interactive == TRUE) %>% group_by(code) %>%
mutate(ocs_sum = sum(sessions)) %>% select(code, ocs_sum, interactive) %>% distinct()
staticTotals = pageUrl %>% filter(interactive == FALSE) %>% group_by(code) %>%
mutate(ocs_sum = sum(sessions)) %>% select(code, ocs_sum, interactive) %>% distinct()
cstotalsplot = ggplot(drop_na(pageTotals), aes(x = reorder(code, -ocs_sum), y = ocs_sum, fill = code)) +
geom_bar(position = "dodge", stat = "identity") + scale_fill_viridis_d() +
labs(x = "Case Study", y = "Session Count",
title = "Number of Total Sessions by Case Study") +
theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
guides(fill = "none") + geom_text(aes(label = paste0(ocs_sum)), vjust = -.5)
```
```{r staticeplot}
staticplot = ggplot(drop_na(staticTotals),
aes(x = reorder(code, -ocs_sum), y = ocs_sum, fill = code)) +
geom_bar(position = "dodge", stat = "identity") + scale_fill_viridis_d() +
labs(x = "Case Study", y = "Session Count",
title = "Number of Total Sessions by Static Case Study") +
theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
guides(fill = "none") + geom_text(aes(label = paste0(ocs_sum)), vjust = -.5) +
ylim(0,1150)
staticplot
```
```{r interactiveplot}
interactivesplot = ggplot(drop_na(interactiveTotals),
aes(x = reorder(code, -ocs_sum), y = ocs_sum, fill = code)) +
geom_bar(position = "dodge", stat = "identity") + scale_fill_viridis_d() +
labs(x = "Case Study", y = "Session Count",
title = "Number of Total Sessions by Interactive Case Study") +
theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
guides(fill = "none") + geom_text(aes(label = paste0(ocs_sum)), vjust = -.5) +
ylim(0,500)
interactivesplot
```
```{r case_study_sessionsplot}
ggarrange(staticplot, interactivesplot,
labels = c("A.", "B."),
ncol = 1, nrow = 2)
```
```{r langauge}
ocs_property_id = 256923228
language = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("newUsers"),
dimensions = c("language"))
language
```
```{r, linkUrl}
ocs_property_id = 256923228
linkUrl = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("linkUrl"))
linkUrl
```
```{r, OSversion}
ocs_property_id = 256923228
OSversion = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("operatingSystem"))
OSversion
```
```{r, pageReferrer}
ocs_property_id = 256923228
pageReferrer = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("pageReferrer"))
pageReferrer
```
look into collapsing duplicates functions / group_by!
```{r pagetitle}
ocs_property_id = 256923228
# used fullPageUrl instead
# pagetitle = ga_data(ocs_property_id,
# date_range = c("2021-01-01", "yesterday"),
# metrics = c("sessions"),
# dimensions = c("pageTitle"))
# case_studies = c("School Shootings in the United States",
# "Disparities in Youth Disconnection",
# "Opioids in United States",
# "Vaping Behaviors in American Youth",
# "Mental Health of American Youth",
# "Exploring global patterns of obesity across rural and urban regions", "Influence of Multicollinearity on Measured Impact of Right-to-Carry Gun Laws",
# "Exploring CO2 emissions across time",
# "Influence of Multicollinearity on Measured Impact of Right-to-Carry Gun Laws Part 1",
# "Exploring global patterns of dietary behaviors associated with health risk",
# "Predicting Annual Air Pollution")
# pages = pagetitle %>% mutate(
# ocs = str_remove(pageTitle, pattern = "Open Case Studies: ")
# ) %>%
# filter(ocs %in% case_studies) %>%
# mutate(
# static = str_count(pageTitle, "Open Case Studies: ")) %>%
# mutate(
# code = case_when(
# ocs == "School Shootings in the United States" ~ "School Shootings",
# ocs == "Disparities in Youth Disconnection" ~ "Youth Disconnection",
# ocs == "Opioids in United States" ~ "Opiods",
# ocs == "Vaping Behaviors in American Youth" ~ "Vaping",
# ocs == "Mental Health of American Youth" ~ "Mental Health",
# ocs == "Exploring global patterns of obesity across rural and urban regions" ~ "Obesity",
# ocs == "Influence of Multicollinearity on Measured Impact of Right-to-Carry Gun Laws" ~ "RTC Analysis",
# ocs == "Exploring CO2 emissions across time" ~ "CO2 Emissions",
# ocs == "Influence of Multicollinearity on Measured Impact of Right-to-Carry Gun Laws Part 1" ~ "RTC Wrangling",
# ocs == "Exploring global patterns of dietary behaviors associated with health risk" ~ "Diet",
# ocs == "Predicting Annual Air Pollution" ~ "Air Pollution"
# )
# ) %>% group_by(code) %>%
# mutate(ocs_sum = sum(sessions)) %>% select(code, ocs_sum) %>% distinct()
# pages
```
```{r percentScrolled}
ocs_property_id = 256923228
percentScrolled = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("percentScrolled"))
percentScrolled
```
```{r searchTerm}
ocs_property_id = 256923228
searchTerm = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("searchTerm"))
searchTerm
```
```{r sources}
ocs_property_id = 256923228
sources = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessions"),
dimensions = c("source"))
sources
```
```{r sessions}
ocs_property_id = 256923228
engagedSessions = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("engagedSessions", "engagementRate", "sessions"),
dimensions = c("week"), limit = 1000)
ses_long = engagedSessions %>%
pivot_longer(c(sessions, engagedSessions), names_to = "metric", values_to = "count")
sesplot = ggplot(ses_long, aes(x = as.numeric(week), y = count, color = metric)) +
geom_line() +
labs(x = "Week Number", y = "Count", title = "Open Case Studies Weekly Sessions and Engaged Sessions")
```
```{r engagedSessions}
engageplot = ggplot(engagedSessions, aes(x = as.numeric(week), y = engagementRate,
color = engagementRate)) +
geom_line() +
labs(x = "Week Number", y = "Engagement Rate",
title = "Open Case Studies Weekly Engagement Rate") + guides(color = "none")
#scale_color_manual(labels = "engagmentRate", values = "#8C4799")
```
```{r engagementplot}
ggarrange(sesplot, engageplot,
labels = c("A.", "B."),
ncol = 1, nrow = 2)
```
```{r sessiontotals}
#totals
totalSessions = ga_data(ocs_property_id,
date_range = c("2021-01-01", "today"),
metrics = c("engagedSessions", "engagementRate", "sessions"),
limit = 1000)
avgsessions = totalSessions$sessions / nrow(dates)
avgengagedsessions = totalSessions$engagedSessions / nrow(dates)
```
```{r, screenPageViews}
ocs_property_id = 256923228
screenPageViews = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("screenPageViews"),
dimensions = c("pageTitle"), limit = 1000)
screenPageViews
```
```{r, sessionsPerUser}
ocs_property_id = 256923228
sessionsPerUser = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("sessionsPerUser"),
dimensions = c("pageTitle"), limit = 1000)
sessionsPerUser
```
```{r, conversions}
ocs_property_id = 256923228
conversions = ga_data(ocs_property_id,
date_range = c("2021-01-01", "yesterday"),
metrics = c("conversions"),
dimensions = c("eventName"), limit = 1000)
conversions
```
dimensions of interest:
- browser
- cohort
- country/countryID
- dateHourMinute
- firstSessionDate
- fileName
- firstUserMedium
- firstUserTrafficOrigin
- fullPageUrl
- hour, minute
- language, languageCode
- linkClasses, linkDomain, linkUrl
- medium, method
- operatingSystem, operatingSystemWithVersion
- pageLocation, pageReferrer
- pageTitle
- percentScrolled
- searchTerm
- streamName
- sessionSource, source, sessionMedium
metrics of interest:
- active1DayUsers
- conversions
- engagedSessions, engagementRate
- screenPageViews
- sessions, sessionsPerUser
for users data: use active1Dayusers and newUsers
cool figure to make: # of events (or users) per day (events y axis, days x axis)
have a timeline of project development along the x-axis to view if website traffic was changed at all by project developments.
helpful links:
https://www.dase-analytics.com/blog/en/how-to-get-data-from-google-analytics-using-r-programming/
https://ga-dev-tools.web.app/dimensions-metrics-explorer/
https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema