Code
# Libraries
library(googleAnalyticsR)
library(gargle)
library(tidyverse)
library(plotly)
library(lubridate)
Tony Duan
# A tibble: 4 × 4
account_name accountId property_name propertyId
<chr> <chr> <chr> <chr>
1 verykoala 30370450 verykoala - GA4 431072878
2 verykoala 30370450 drinking 431975107
3 Demo Account 54516992 GA4 - Flood-It! 153293282
4 Demo Account 54516992 GA4 - Google Merch Shop 213025502
# A tibble: 18 × 5
country activeUsers newUsers sessions screenPageViews
<chr> <dbl> <dbl> <dbl> <dbl>
1 Hong Kong 32 23 136 631
2 China 28 31 279 1814
3 United States 15 12 20 95
4 Thailand 6 2 14 87
5 Ireland 5 5 5 5
6 Japan 5 4 5 13
7 Macao 4 4 20 58
8 Sweden 4 4 4 4
9 United Kingdom 3 3 3 3
10 (not set) 2 2 2 2
11 Malaysia 2 2 4 9
12 Singapore 2 0 4 11
13 South Korea 2 2 3 2
14 Argentina 1 1 1 1
15 Australia 1 1 1 1
16 Canada 1 1 1 3
17 Iran 1 1 1 3
18 Taiwan 1 1 1 1
fig <- plot_ly(data=country,x = ~sessions,y=~country,text=~sessions, type = "bar",orientation = 'h')%>%
layout(title = 'Vistors',yaxis = list(title='Regions',categoryorder = "total ascending"),
images = list(
list(
source = base64enc::dataURI(file = "images/google_analytics-ar21.webp")
,xref = "paper"
,yref = "paper"
,x = 0.9
,y = 0.3
,sizex = 0.3
,sizey = 0.3
,xanchor="right"
,layer = "below"
)
)
)
fig
# A tibble: 100 × 7
date dayOfWeek activeUsers newUsers sessions screenPageViews
<date> <chr> <dbl> <dbl> <dbl> <dbl>
1 2024-12-31 2 2 2 2 2
2 2024-12-29 0 2 0 4 11
3 2024-12-27 5 2 0 5 215
4 2024-12-26 4 2 1 2 23
5 2024-12-25 3 3 1 10 37
6 2024-12-22 0 2 0 3 39
7 2024-12-21 6 2 0 5 20
8 2024-12-20 5 4 2 7 63
9 2024-12-19 4 2 0 11 39
10 2024-12-18 3 2 0 7 13
# ℹ 90 more rows
# ℹ 1 more variable: weekend_date <date>
fig <- plot_ly(data = sample002, x = ~weekend_date, y = ~sessions,mode = 'lines')%>% layout(title = 'Vistors',images = list(
list(
source = base64enc::dataURI(file = "images/google_analytics-ar21.webp")
,xref = "paper"
,yref = "paper"
,x = 0.4
,y = 0.9
,sizex = 0.3
,sizey = 0.3
,xanchor="left"
)
)
)
fig
https://www.listendata.com/2023/09/how-to-use-google-analytics-in-r.html
---
title: "google analytic in R"
author: "Tony Duan"
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: right
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
---
# package
```{r}
# Libraries
library(googleAnalyticsR)
library(gargle)
library(tidyverse)
library(plotly)
library(lubridate)
```
# API
```{r}
# Access
ga_auth(email="verykoala@gmail.com")
ga_account_list("ga4")
```
# download data
```{r}
my_property_id <- 431975107
from_date <- "2023-01-01"
to_date <- "2024-12-31"
overall <- ga_data(
my_property_id,
metrics = c("activeUsers", "newUsers","sessions", "screenPageViews"),
date_range = c(from_date, to_date)
)
```
```{r}
overall
```
# image at the back into plotly
```{r}
# By Country
country <- ga_data(
my_property_id,
dimensions = c("country"),
metrics = c("activeUsers", "newUsers","sessions", "screenPageViews"),
date_range = c(from_date, to_date)
)
country
```
# add image at the back into plotly
```{r}
fig <- plot_ly(data=country,x = ~sessions,y=~country,text=~sessions, type = "bar",orientation = 'h')%>%
layout(title = 'Vistors',yaxis = list(title='Regions',categoryorder = "total ascending"),
images = list(
list(
source = base64enc::dataURI(file = "images/google_analytics-ar21.webp")
,xref = "paper"
,yref = "paper"
,x = 0.9
,y = 0.3
,sizex = 0.3
,sizey = 0.3
,xanchor="right"
,layer = "below"
)
)
)
fig
```
```{r}
sample <- ga_data(
my_property_id,
dimensions = c("date","dayOfWeek"),
metrics = c("activeUsers", "newUsers","sessions", "screenPageViews"),
date_range = c(from_date, to_date)
) %>%
arrange(desc(date)) %>% mutate(weekend_date=floor_date(date, "week")+7)
sample
sample002=sample %>% group_by(weekend_date) %>% summarise(activeUsers=sum(activeUsers)
,sessions=sum(sessions))
```
```{r}
fig <- plot_ly(data = sample002, x = ~weekend_date, y = ~sessions,mode = 'lines')%>% layout(title = 'Vistors',images = list(
list(
source = base64enc::dataURI(file = "images/google_analytics-ar21.webp")
,xref = "paper"
,yref = "paper"
,x = 0.4
,y = 0.9
,sizex = 0.3
,sizey = 0.3
,xanchor="left"
)
)
)
fig
```
# reference:
https://www.listendata.com/2023/09/how-to-use-google-analytics-in-r.html