Created
October 10, 2013 19:01
-
-
Save IronistM/6923644 to your computer and use it in GitHub Desktop.
Basic plotting of AOV, Conversion rate & Goal completion for a A/B Test across 3 domains. (#GoogleAnalytics and #ContentExperiments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load requirements to pull data from Google Analytics. | |
library(RGoogleAnalytics) | |
library(RColorBrewer) | |
library(scales) | |
library(lubridate) | |
library(ProjectTemplate) | |
library(ggplot2) | |
library(gridExtra) | |
library(plyr) | |
setwd("C:/Users/m.parzakonis/Google Drive/MyCodeRants/") | |
# CONNECT AND QUERY ----------------------------------------------------------- | |
# Let's initiate a query instance to do the job | |
query <- QueryBuilder() | |
# Open Google's OAuth Playground in the default browser. | |
# You will need to exchange your Authorization Code for an Access Token. | |
access_token <- query$authorize() | |
# if you are clever enough to check the AutoRefresh on OAuth Playgournd you can use the following | |
# access_token <- "ya29.AHES6ZQEeVsVTg7_RcLjsUY_1hj_QfC0ZMVuagfOCgL_mm68AA" | |
ga <- RGoogleAnalytics() | |
# Get list of profiles and echo to the console. You will use the left-most number for the | |
# profile you want to query in the next step. | |
( ga.profiles <- ga$GetProfileData(access_token) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Multiple plot function | |
# | |
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects) | |
# - cols: Number of columns in layout | |
# - layout: A matrix specifying the layout. If present, 'cols' is ignored. | |
# | |
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), | |
# then plot 1 will go in the upper left, 2 will go in the upper right, and | |
# 3 will go all the way across the bottom. | |
# | |
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { | |
require(grid) | |
# Make a list from the ... arguments and plotlist | |
plots <- c(list(...), plotlist) | |
numPlots = length(plots) | |
# If layout is NULL, then use 'cols' to determine layout | |
if (is.null(layout)) { | |
# Make the panel | |
# ncol: Number of columns of plots | |
# nrow: Number of rows needed, calculated from # of cols | |
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), | |
ncol = cols, nrow = ceiling(numPlots/cols)) | |
} | |
if (numPlots==1) { | |
print(plots[[1]]) | |
} else { | |
# Set up the page | |
grid.newpage() | |
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) | |
# Make each plot, in the correct location | |
for (i in 1:numPlots) { | |
# Get the i,j matrix positions of the regions that contain this subplot | |
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) | |
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, | |
layout.pos.col = matchidx$col)) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get list of profiles and echo to the console. You will use the left-most number for the | |
# profile you want to query in the next step. | |
( ga.profiles <- ga$GetProfileData(access_token) ) | |
ga.profiles<-ga.profiles[c(39,67,1),] | |
my_start_date <-"2013-09-28" # Query date range | |
my_end_date <-"2013-10-09" | |
# Create a filtering for getting specific data on the experiment across domains | |
filters<-c("ga:experimentId=~I1L9wLVESJen2wS_Cvt_KA","ga:experimentId=~3bo3cnAzTZux591CZabJDA","ga:experimentId=~_7ipfAJyRf2bMKCrXPfaaw") | |
for (i in 1:nrow(ga.profiles)) { | |
print(filters[i]) | |
# Initiate a Query | |
query <- QueryBuilder() | |
# Build the query string, use the profile by setting its index value | |
query$Init(start.date = my_start_date, | |
end.date = my_end_date, | |
dimensions = "ga:date,ga:experimentVariant,ga:browser", | |
metrics = "ga:goal1Completions,ga:transactions,ga:visits,ga:transactionRevenue", | |
sort = "ga:date", | |
filters = filters[i], | |
max.results = 1500, | |
table.id = paste("ga:",ga.profiles$id[i],sep="",collapse=","), | |
access_token=access_token) | |
# Make a request to get the data from the API | |
ga.data.1<- ga$GetReportData(query) | |
# Merge files, create metrics, limit dataset to just days when tags firing | |
ga.data.1$date <-ymd(ga.data.1$date) | |
# Report Header | |
print(paste("The following graphs and tables are about",ga.profiles$name[i],"experiment performance")) | |
# Build the graphs to use with multiplot() | |
# REMINDER : Use print() to get ggplot/qplot in loops! | |
a<-qplot(experimentVariant, transactionRevenue/transactions,data=ga.data.1, geom="boxplot" | |
, main=paste("AOV per Variant\n",ga.profiles$name[i]),xlab="Variant", ylab="AOV") | |
b<-qplot(experimentVariant, goal1Completions/visits,data=ga.data.1, geom="boxplot" | |
, main=paste("Select a flight % per Variant\n",ga.profiles$name[i]),xlab="Variant", ylab="Select a flight (%)") | |
c<-qplot(experimentVariant, transactions/visits,data=ga.data.1,geom="boxplot" | |
, main=paste("Conversion (%)\n",ga.profiles$name[i]),xlab="Variant", ylab="Conversion (%)") | |
print(multiplot(a,b,c, cols=3)) | |
# The next two are ggplot grpahs and will be use without the multiplot() | |
d<-ggplot(ga.data.1, aes(x=date, y=(transactions/visits)*100, colour=experimentVariant)) | |
e<-ggplot(ga.data.1, aes(x=date, y=(transactionRevenue/transactions), colour=experimentVariant)) | |
print(d + | |
geom_point(alpha=.3) + | |
geom_smooth(alpha=.2, size=1) + | |
theme(legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"))+ | |
labs(title = paste("Conversion (%) per Variant\n",ga.profiles$name[i]),x="Date",y="Conversion (%)")) | |
print(e + | |
geom_point(alpha=.3) + | |
geom_smooth(alpha=.2, size=1) + | |
theme(legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"))+ | |
labs(title = paste("AOV per Variant\n",ga.profiles$name[i]),x="Date",y="AOV")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment