telmap-analytics.R

Uploaded by:fwild

              
library(rgoogleanalytics)

source("~/google-key.R")

ga <- RGoogleAnalytics()
ga$SetCredentials(username, password, key)
profiles <- ga$GetProfileData()

startdate = as.Date("2011-06-01")
enddate = as.Date("2012-02-29")

wprfl = 1
query <- QueryBuilder()
query$Init(
  start.date = as.character(startdate),
  end.date = as.character(enddate),
  dimensions = "ga:date",
  metrics = "ga:visitors,ga:pageviews,ga:timeOnSite,ga:visits,ga:pageviewsPerVisit",
  sort = "ga:date",
  table.id = "ga:27244629" # as.character(profiles$profile[wprfl,3])
)
#statsname = profiles$profile[wprfl,2]
stats = ga$GetReportData(query)

# nice colours
cs = c( "#FF9900", "#8AD71B", "#FFCC00", "#6ABFF5", "#8E4CE8", "#D02D2D", "#2AB8BD", "#BDA32A", "#1B2B8B", "#6A8B1B", "#1B728B", "#5A1B8B", "#828282", "#75A982" )

# -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  
# aggregate data

ndays = difftime(enddate,startdate)
visitors = matrix(0, nrow=1, ncol=0)
visits = matrix(0, nrow=1, ncol=0)
pages = matrix(0, nrow=1, ncol=0)
rownames(visitors) = c("telmap")
rownames(visits) = c("telmap")
rownames(pages) = c("telmap")
lastmonth = ""
mydate = startdate
nmonth = 0

for (i in 1:(ndays+1)) {
  
	month = strftime(mydate, "%m")
	year = strftime(mydate, "%Y")
	
	if (lastmonth!=month) {
		nmonth = nmonth + 1
		visitors = cbind(visitors, vector(length=nrow(visitors), mode="integer") )
		colnames(visitors)[nmonth] = paste(year,month,sep="-")
		visits = cbind(visits, vector(length=nrow(visits), mode="integer") )
		colnames(visits)[nmonth] = paste(year,month,sep="-")
		pages = cbind(pages, vector(length=nrow(pages), mode="integer") )
		colnames(pages)[nmonth] = paste(year,month,sep="-")
	}
	
	visitors[1,nmonth] = visitors[1,nmonth] + stats$data[i, 'ga:visitors']
	visits[1,nmonth] = visits[1,nmonth] + stats$data[i, 'ga:visits']
	pages[1,nmonth] = pages[1,nmonth] + stats$data[i, 'ga:pageviews']
	
	mydate = mydate + 1
	lastmonth = month
	
}

sum(visitors)
sum(visits)
sum(pages)


# -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  
# visualise

pdf(file=paste("gallery/stats",statsname,".pdf", sep=""), width=20/2.54, height=20/2.54)

op=par(mar = c(5, 11, 8, 8)+0.1)

# choose fixed value instead if you want to compare 
# against other domains e.g.: maxp = 13000
maxp = max(pages)+100 

plot(visitors["telmap",], type="l", col=cs[1], xaxt="n", cex=2, cex.axis=1, cex.sub=2, cex.lab=2, xlab=toupper(statsname), ylab="visitors/visits/pages", ylim=c(0,maxp), frame.plot=F, cex.main=2, lwd=3 )
lines(visits["telmap",], col=cs[2], lwd=3)
lines(pages["telmap",], col=cs[3], lwd=3)
axis(1,at=1:ncol(visitors),labels=colnames(visitors))

# labels
text(ncol(visitors), round(sum(visitors[1,(ncol(visitors)-2):ncol(visitors)])/3), "visitors", col=cs[1], pos=4, cex=1.5, srt=0, adj=1, xpd = TRUE)
text(ncol(visits), round(sum(visits[1,(ncol(visits)-2):ncol(visits)])/3), "visits", col=cs[2], pos=4, cex=1.5, srt=0, adj=1, xpd = TRUE)
text(ncol(pages), round(sum(pages[1,(ncol(pages)-2):ncol(pages)])/3), "pages", col=cs[3], pos=4, cex=1.5, srt=0, adj=1, xpd = TRUE)

dev.off()