Skip to content

Instantly share code, notes, and snippets.

@mwhitaker mwhitaker/bella.png
Last active Aug 29, 2015

Embed
What would you like to do?
Visualize users with multiple sessions from multiple cities in the US from Google Analytics
library(RGA) # get it at https://bitbucket.org/unikum/rga then set credentials and authorize, eg authorize(client.id,client.secret)
library(data.table)
# pull main cities
main_cities <- get_ga(profile.id = XXXXXXXX, start.date = "2014-12-01", end.date = "2014-12-31",
metrics = "ga:users", dimensions = "ga:city,ga:region,ga:latitude,ga:longitude",
filters = "ga:city!=(not set)",
sort = "-ga:users",
segment = "sessions::condition::ga:country==United States")
# use list of cities obtained to get users who had sessions from multiple cities
multi_city <- rbindlist(lapply(main_cities$city, function(x) {
seg <- paste0("sessions::condition::ga:country==United States,ga:region==",main_cities$region[x],";users::sequence::ga:city==",main_cities$city[x],";->ga:city!=",main_cities$city[x])
out <- get_ga(profile.id = XXXXXXXX, start.date = "2014-12-01", end.date = "2014-12-31",
metrics = "ga:users", dimensions = "ga:city,ga:region,ga:latitude,ga:longitude",
sort = "-ga:users",
filters = "ga:city!=(not set)",
segment = seg)
if(length(out)==0) {
cat("no result for city",multi_city$city[x])
return(NULL)}
return(cbind(origin_city=multi_city$city[x],origin_region=multi_city$region[x],origin_latitude=multi_city$latitude[x], origin_longitude=multi_city$longitude[x],out))
}))
# add new column so that we can then treat city pairs A > B same as B > A
multi_city[,pairs:=list(unlist(lapply(lapply(str_split(paste(origin_city,origin_region,city,region),' '), sort), function(x) paste(x,collapse="_"))))]
# remove city A > city A
multi_city <- multi_city[!(origin_city==city & origin_region==region)]
# pull out just lats and longs and sum up counts
draw <- multi_city[,list(lat1=origin_latitude,lon1=origin_longitude,lat2=latitude,lon2=longitude,count=sum(users)), by=pairs][order(count)]
draw <- draw[,.SD[1], by=pairs]
##### inspired by Flowing Data
library(maps)
library(geosphere)
library(magrittr)
# set colors and palette
pal <- colorRampPalette(c("#333333", "white", "#1292db"))
colors <- pal(100)
maxcnt <- max(draw$count)
# send to png file
png("cities.png", width=800, height=600)
map("state", col="#191919", fill=TRUE, bg="#000000", lwd=0.05)
for (i in 1:nrow(draw)) {
colindex <- round( (draw[i, count] / maxcnt) * length(colors) )
draw[i, gcIntermediate(c(lon1, lat1), c(lon2, lat2), n=100, addStartEnd=TRUE)] %>%
lines(col=colors[colindex], lwd=0.6)
}
dev.off() # close file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.