This is the one metric we have on the usage dashboard. Let’s summarize new users by month.
Code
df_monthly_new_users = df_daily %>%select(date, new_users ) %>%mutate(year =year(date),month =month(date),year_month_str = glue::glue("{year}-{month}")) %>%group_by(year_month_str) %>%summarise(new_users =sum(new_users)) %>%ungroup() %>%mutate(date =ymd(glue::glue("{year_month_str}-01"))) %>%## format date as MM dd, YYYYmutate(Name =format(date, "%B %d, %Y")) %>%arrange(desc(date))%>%select(Name, `New Users`= new_users, Date = date)df_monthly_new_users %>%write_csv("metadata_cache/df_monthly_new_users.csv")df_monthly_new_users %>% jsonlite::write_json("metadata_cache/df_monthly_new_users.json", auto_unbox =TRUE, pretty = T)
Let’s preview this
Code
df_monthly_new_users %>%reactable()
Source Code
---title: "SALURBAL Google Analytics Ingestions"author: - name: Ran Li (Maintainer) orcid: 0000-0002-4699-4755 date: last-modifiedformat: html: page-layout: full toc: true self-contained: true code-fold: true df-print: kable code-tools: true comments: hypothesis: theme: cleaneditor: sourceexecute: warning: false message: false eval: trueeditor_options: chunk_output_type: console---```{r}#| code-summary: "Setup Code"#| echo: falselibrary(pacman)p_load(tidyverse, arrow, googleAnalyticsR, lubridate, reactable, janitor)## local parametrsstart_date ="2022-10-01"end_date ='today'```This notebook ingests google analytics data for our datawarehouse we will leverage the `googleAnalyticsR` package. # Properties```{r}#| code-summary: "Connect to Google Analytics"#| echo: falsega_auth(email="ranli2011@gmail.com")# ga_auth()# account_df <- ga_accounts()# account_df$id``````{r}#| code-summary: "Authenticate if needd"#| echo: false#| eval: falsega_auth()account_df <-ga_accounts()account_df$id```Lets get a list of all of our google analytics accounts - focusing on SALURBAL```{r}#| code-summary: "Get SALURBAL GA properties"#| eval: false## All SALURBAL Propertiessalurbal_ga =ga_account_list("ga4") %>%filter(str_detect(property_name, "salurbal"))## Portal Propertyga_portal = salurbal_ga %>%filter(property_name =='salurbal-data-portal')property_id = ga_portal$propertyIdsalurbal_ga```Here is a table of available dimensions and metrics available for us to analyze.```{r}#| code-summary: "Google Analytics codebook"#| eval: falsedf_metrics_available =ga_meta(version =c("universal", "data"),propertyId = property_id,cached =TRUE,no_api =FALSE) %>%as_tibble()%>%filter(status !="DEPRECATED")df_metrics_available %>%select(type, name = uiName, description) %>%arrange(desc(type), name) %>%reactable(searchable =TRUE)```# PortalLets get some basic monthly information about SALURABL portal. Lets see what is available for this property.## Trends### DownloadLet's just get basic metrics.```{r}#| code-summary: "Get basic metrics"if (!file.exists('metadata_cache/df_daily.parquet')){ df_daily =ga_data( property_id,metrics =c("sessions", "newUsers","activeUsers", "screenPageViews","bounceRate","averageSessionDuration","eventsPerSession" ),dimensions =c('date'),date_range =c(start_date, end_date),limit =-1 ) %>%arrange(desc(date)) df_daily %>%clean_names()%>%write_parquet("metadata_cache/df_daily.parquet")} else { df_daily =read_parquet("metadata_cache/df_daily.parquet")}df_daily %>%reactable()```### Metrics#### New users by MonthThis is the one metric we have on the usage dashboard. Let's summarize new users by month. ```{r}df_monthly_new_users = df_daily %>%select(date, new_users ) %>%mutate(year =year(date),month =month(date),year_month_str = glue::glue("{year}-{month}")) %>%group_by(year_month_str) %>%summarise(new_users =sum(new_users)) %>%ungroup() %>%mutate(date =ymd(glue::glue("{year_month_str}-01"))) %>%## format date as MM dd, YYYYmutate(Name =format(date, "%B %d, %Y")) %>%arrange(desc(date))%>%select(Name, `New Users`= new_users, Date = date)df_monthly_new_users %>%write_csv("metadata_cache/df_monthly_new_users.csv")df_monthly_new_users %>% jsonlite::write_json("metadata_cache/df_monthly_new_users.json", auto_unbox =TRUE, pretty = T)```Let's preview this```{r}df_monthly_new_users %>%reactable()```