Skip to content

Instantly share code, notes, and snippets.

@madilk
Created June 21, 2021 20:51
Show Gist options
  • Save madilk/654af67708c9051fc356f16eedb5442e to your computer and use it in GitHub Desktop.
Save madilk/654af67708c9051fc356f16eedb5442e to your computer and use it in GitHub Desktop.
Pyramid chart in R using Google Analytics API plus demographic data [age and gender]
#source R script
#https://thomas-neitmann.github.io/ggcharts/reference/pyramid_chart.html
library(googleAnalyticsR)
#pull data from GA API
library(googleAnalyticsR)
#change location of json service key
ga_auth(json_file = "C:\\secret-folder\\service-key.json")
account_list$viewId
#change view id
viewID <- 12345678
data <- google_analytics(
viewID,
date_range = c("2020-01-01","2021-06-09"),
dimensions = c("ga:userAgeBracket",
"ga:userGender"),
metrics = c("sessions"),
anti_sample = TRUE
)
#SO thread on age + gender in GA API
#https://stackoverflow.com/questions/37488470/obtain-demographic-data-with-google-analytics-api
summary(data)
str(data)
View(data)
library(ggplot2)
pyramid_chart(data, userAgeBracket,
sessions, userGender, bar_colors = c("pink","lightblue"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment