Skip to content

Instantly share code, notes, and snippets.

@artemklevtsov
Last active September 14, 2015 18:41
Show Gist options
  • Save artemklevtsov/079b73220f61a08de3cf to your computer and use it in GitHub Desktop.
Save artemklevtsov/079b73220f61a08de3cf to your computer and use it in GitHub Desktop.
RGA demos
## Load pakcages
library(RGA)
library(stringi)
library(ggplot2)
## Authorisation
authorize()
## Get Profile ID
# Profiles (profiles) list
profiles <- list_profiles()
# Site URL
site_url <- "example.com"
# Get Profile ID
id <- profiles[profiles$website.url == site_url, "id"]
## Setup dates range
# Set first date
start.date <- "2014-01-01"
# Set the last date
end.date <- "2014-12-31"
## Get data
ga_data <- get_ga(id, start.date = start.date, end.date = end.date,
metrics = "ga:sessions", dimensions = "ga:date",
filters = "ga:sessions > 0")
## Prepare data
cal_data <- cbind(ga_data, stri_datetime_fields(ga_data$date))
cal_data <- transform(cal_data,
Month = factor(Month, levels = 1:12, labels = stri_datetime_symbols(width = "abbreviated")$Month),
DayOfWeek = factor(DayOfWeek, levels = 1:7, labels = stri_datetime_symbols(width = "abbreviated")$Weekday))
# If Monday is first day of week
#cal_data$DayOfWeek <- factor(cal_data$DayOfWeek, levels(cal_data$DayOfWeek)[c(2:7, 1)])
# If Sunday is first day of week
cal_data[as.numeric(cal_data$DayOfWeek) == 1, "WeekOfMonth"] <- cal_data[as.numeric(cal_data$DayOfWeek) == 1, "WeekOfMonth"] + 1
## Draw plot
ggplot(data = cal_data, aes(x = DayOfWeek, y = WeekOfMonth, fill = sessions)) +
geom_tile(colour = "white") +
geom_text(aes(label = Day)) +
facet_wrap(~ Year + Month, ncol = 3, scales = "free") +
scale_fill_gradient(name = "Sessions", low = "steelblue4", high = "red") +
labs(title = "Time-Series Calendar Heatmap", x = NULL, y = NULL) +
scale_y_reverse(breaks = NULL) -> p
## Save plot
png(filename = "calendar.png", res = 300, width = par("din")[1], height = par("din")[2], units = "in")
print(p)
dev.off()
## Load pakcages
library(RGA)
library(ggplot2)
library(stringi)
## Authorisation
authorize()
## Get Profile ID
# Profiles (profiles) list
profiles <- list_profiles()
# Site URL
site_url <- "example.com"
# Get Profile ID
id <- profiles[profiles$website.url == site_url, "id"]
## Setup dates range
# Get first date
start.date <- firstdate(id)
# Get the next monday
start.date <- as.Date(cut(start.date, "weeks")) + 7
# Get the last sunday
end.date <- as.Date(cut(Sys.Date(), "weeks")) - 1
## Get data
ga_data <- get_ga(id, start.date = start.date, end.date = end.date,
metrics = "ga:pageviews", dimensions = "ga:dayOfWeek,ga:hour")
## Prepare data
ga_data$day.of.week <- ga_data$day.of.week + 1
ga_data$day.of.week <- factor(ga_data$day.of.week, levels = 1:7, labels = stri_datetime_symbols(width = "abbreviated")$Weekday)
# If Monday is first day of week
ga_data$day.of.week <- factor(ga_data$day.of.week, levels(cal_data$DayOfWeek))
ga_data$hour <- sprintf("%02d:00", ga_data$hour)
## Draw plot
ggplot(ga_data, aes(x = day.of.week, y = hour)) +
geom_tile(aes(fill = pageviews), colour = "white") +
scale_x_discrete(limits = levels(ga_data$day.of.week)) +
scale_y_discrete(limits = rev(unique(ga_data$hour))) +
scale_fill_gradient(name = "Page views", low = "steelblue4", high = "red") +
labs(title = "Heatmap daily users activity", x = NULL, y = NULL) +
theme(axis.ticks = element_blank()) -> p
## Save plot
png(filename = "heatmap.png", res = 300, width = par("din")[1], height = par("din")[2], units = "in")
print(p)
dev.off()
## Load pakcages
library(RGA)
library(ggplot2)
library(scales)
## Authorisation
authorize()
## Get Profile ID
# Profiles (profiles) list
profiles <- list_profiles()
# Site URL
site_url <- "example.com"
# Get Profile ID
id <- profiles[profiles$website.url == site_url, "id"]
## Setup dates range
# Set first date
start.date <- "2012-01-01"
# Set the last date
end.date <- "2014-12-31"
## Get data
ga_data <- get_ga(id, start.date = start.date, end.date = end.date,
metrics = "ga:sessions", dimensions = "ga:yearMonth,ga:year",
filters = "ga:sessions > 0")
## Prepare data
ga_data$date <- as.Date(paste0(ga_data$year.month, "01"), "%Y%m%d",ga:year)
## Draw plot
ggplot(data = ga_data, aes(x = as.Date(date), y = sessions)) +
geom_bar(stat = "identity") +
facet_wrap(~ year, ncol = 1, scales = "free_x") +
scale_x_date(labels = date_format("%b"), breaks = date_breaks("month")) +
labs(title = "Time-Series years comparison", x = "") -> p
## Save plot
png(filename = "sessions.png", res = 300, width = par("din")[1], height = par("din")[2], units = "in")
print(p)
dev.off()
## Load pakcages
library(RGA)
library(ggplot2)
library(scales)
## Authorisation
authorize()
## Get Profile ID
# Profiles (profiles) list
profiles <- list_profiles()
# Site URL
site_url <- "example.com"
# Get Profile ID
id <- profiles[profiles$website.url == site_url, "id"]
## Setup dates range
# Set first date
start.date <- "2012-01-01"
# Set the last date
end.date <- "2014-12-31"
## Get data
ga_data <- get_ga(id, start.date = start.date, end.date = end.date,
metrics = "ga:sessions", dimensions = "ga:year,ga:month,ga:medium",
filters = "ga:sessions > 1 && ga:medium != (not set)")
## Prepare data
ga_data$date <- as.Date(paste(ga_data$year, ga_data$month, 1), "%Y%m%d")
ga_data[ga_data$medium == "(none)", "medium"] <- "direct"
## Draw plot
ggplot(data = ga_data, aes(x = date, y = sessions, fill = medium)) +
geom_area(position = "stack") +
facet_wrap(~ year, ncol = 1, scales = "free_x") +
scale_x_date(labels = date_format("%b"), breaks = date_breaks("month")) +
labs(title = "Time-Series years comparison", x = "") +
theme(legend.position = "bottom") -> p
## Save plot
png(filename = "traffic-sources.png", res = 300, width = par("din")[1], height = par("din")[2], units = "in")
print(p)
dev.off()
## Load pakcages
library(RGA)
library(wordcloud)
library(RColorBrewer)
## Authorisation
authorize()
## Get Profile ID
# Profiles (profiles) list
profiles <- list_profiles()
# Site URL
site_url <- "example.com"
# Get Profile ID
id <- profiles[profiles$website.url == site_url, "id"]
## Setup dates range
# Set first date
start.date <- "2014-01-01"
# Set the last date
end.date <- "2014-12-31"
## Get data
ga_data <- get_ga(id, start.date = start.date, end.date = end.date,
metrics = "ga:sessions", dimensions = "ga:keyword",
filters = "ga:medium==organic && ga:keyword!=(not provided) && ga:keyword!=(not set) && ga:keyword!=(other)",
sort = "-ga:sessions", max.results = 100)
## Prepare data
ga_data$keyword <- tolower(ga_data$keyword)
## Draw plot
pal <- brewer.pal(8, "Dark2")
## Save plot
png(filename = "wordcloud.png", res = 300, width = par("din")[1], height = par("din")[2], units = "in")
wordcloud(ga_data$keyword, ga_data$sessions, random.order = FALSE, colors = pal)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment