Row

users

856,595

pageviews

1,436,771

Row

Page views

About

Custom Google Analytics dashboard

This custom Google Analytics dashboard provides an overview of the evolution of page views and the most popular posts for the blog statsandr.com. This dashboard is built with R using the R Markdown framework.

Code

The code behind this dashboard is available on GitHub. Feel free to adapt the code to your needs.

Data

The input data for this dashboard is the Google Analytics data of statsandr.com, for the period from Dec 16, 2019 to Dec 15, 2021.

Information & contact

More information on how to track the performance of your blog or website can be found in these articles:

For any question or feedback, you can either open an issue or contact me.

Go back to statsandr.com (blog) or antoinesoetewey.com (personal website).

---
title: "Google Analytics dashboard"
author: "statsandr.com"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    source_code: embed
    social: ["twitter", "facebook", "linkedin"]
---

```{r setup, include=FALSE}
library(flexdashboard)
library(readr)
library(ggplot2)
library(dplyr)
library(reactable)
library(lubridate)
library(stringr)
library(googleAnalyticsR)

googleAuthR::gar_set_client('/Users/antoinesoetewey/Google Drive/statsandr.com/client_secret_1041935229803-bif7gdbj7edts4c5e9r5oedtcpqdcr5k.apps.googleusercontent.com.json')

# specify an email you have already authenticated with once outside of Rmd
ga_auth(email = "ant.soetewey@gmail.com")

accounts <- ga_account_list()
# accounts
accounts$webPropertyName

# select the view ID by property name
view_id <- accounts$viewId[which(accounts$webPropertyName == "statsandr.com")]

# set date range
start_date <- as.Date("2019-12-16")
end_date <- Sys.Date() - 1

# get Google Analytics (GA) data
gadata0 <- google_analytics(view_id,
  date_range = c(start_date, end_date),
  metrics = c("users", "pageviews"),
  anti_sample = TRUE # slows down the request but ensures data isn't sampled
)

gadata1 <- google_analytics(view_id,
  date_range = c(start_date, end_date),
  metrics = c("pageviews"),
  dimensions = c("date"),
  anti_sample = TRUE # slows down the request but ensures data isn't sampled
)

gadata2 <- google_analytics(view_id,
  date_range = c(start_date, end_date),
  metrics = c("pageviews"),
  dimensions = c("pageTitle"),
  anti_sample = TRUE # slows down the request but ensures data isn't sampled
)
gadata2 <- subset(gadata2, pageviews > 500)

theme_set(theme_minimal())

users_color <- "purple"
pageviews_color <- "forestgreen"
```

Row
-----------------------------------------------------------------------

### users {.value-box}

```{r}
valueBox(
  value = paste(format(sum(gadata0$users), big.mark = ","), "", sep = " "),
  caption = "Users",
  icon = "far fa-user",
  color = users_color
)
```

### pageviews {.value-box}

```{r}
valueBox(
  value = paste(format(sum(gadata0$pageviews), big.mark = ","), "", sep = " "),
  caption = "Page views",
  icon = "far fa-file-alt",
  color = pageviews_color
)
```

Row {.tabset}
-----------------------------------------------------------------------

### Page views

```{r}
gadata_by_day <- gadata1 %>% 
  group_by(date) %>% 
  summarize(pagesums = sum(pageviews))

# scatter plot with a trend line
gadata1 %>%
  ggplot(aes(x = date, y = pageviews)) +
  geom_point(size = 1L, color = "steelblue") + # change size and color of points
  geom_smooth(color = "steelblue", alpha = 0.25) + # change color of smoothed line and transparency of confidence interval
  labs(
    y = "Page views",
    x = ""
    # title = "Daily evolution of page views,
    # subtitle = paste0(format(start_date, "%b %d, %Y"), " to ", format(end_date, "%b %d, %Y")),
    # caption = "Data: Google Analytics data of statsandr.com"
  ) +
  theme(plot.margin = unit(c(5.5, 15.5, 5.5, 5.5), "pt")) + # to avoid the plot being cut on the right edge
  scale_y_continuous(labels = scales::comma) # better y labels
```

### Most popular posts

```{r}
# gadata_most_popular <- gadata2 %>% 
#   count(pageTitle, wt = pageviews, sort=TRUE)

# reactable(gadata_most_popular,
#            columns = list(pageTitle     = colDef(name = "Title",
#                                             align = "left"),
#                           n             = colDef(name = "Page views")),
#             pagination = TRUE,
#             searchable = TRUE,
#             striped = TRUE)

most_viewed_posts <- gadata2 %>%
  mutate(Title = str_trunc(pageTitle, width = 40)) %>% # keep maximum 40 characters
  count(Title, wt = pageviews, sort = TRUE)

# plot
top_n(most_viewed_posts, n = 15, n) %>% # edit n for more or less pages to display
  ggplot(., aes(x = reorder(Title, n), y = n)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  coord_flip() +
  labs(
    y = "Page views",
    x = ""
    # title = "Top performing pages in terms of page views",
    # subtitle = paste0(format(start_date, "%b %d, %Y"), " to ", format(end_date, "%b %d, %Y")),
    # caption = "Data: Google Analytics data of statsandr.com"
  ) +
  scale_y_continuous(labels = scales::comma) # better y labels
```

### About

**Custom Google Analytics dashboard**

This [custom Google Analytics dashboard](https://antoinesoetewey.com/files/google-analytics-dashboard) provides an overview of the evolution of page views and the most popular posts for the blog [statsandr.com](https://statsandr.com/). This dashboard is built with R using the R Markdown framework.

**Code**

The code behind this dashboard is available on [GitHub](https://github.com/AntoineSoetewey/Google-Analytics-dashboard){target="_blank"}. Feel free to adapt the code to your needs.

**Data**

The input data for this dashboard is the Google Analytics data of [statsandr.com](https://statsandr.com/), for the period from `r paste0(format(start_date, "%b %d, %Y"), " to ", format(end_date, "%b %d, %Y"))`.

**Information & contact**

More information on how to track the performance of your blog or website can be found in these articles:

- [How to track the performance of your blog in R?](https://statsandr.com/blog/track-blog-performance-in-r/)
- [Stats and R is 2 years old!](https://statsandr.com/blog/statsandr-is-2-years-old/)

For any question or feedback, you can either open an [issue](https://github.com/AntoineSoetewey/Google-Analytics-dashboard/issues){target="_blank"} or [contact me](https://statsandr.com/contact/).


*Go back to [statsandr.com](https://statsandr.com/) (blog) or [antoinesoetewey.com](https://antoinesoetewey.com/) (personal website).*