Skip to content

Instantly share code, notes, and snippets.

@dpapathanasiou
Created June 19, 2013 21:48
Show Gist options
  • Save dpapathanasiou/5818427 to your computer and use it in GitHub Desktop.
Save dpapathanasiou/5818427 to your computer and use it in GitHub Desktop.
This gist contains functions to plot the data logged by the buckabuckaboo plugin (https://github.com/dpapathanasiou/buckabuckaboo) as an animated sequence of lines per distinct IP address (visitor) against a grayed-out background image which represents the page on the site being monitored using R (http://www.r-project.org/).
#
# mouseTrackPlot.R
#
# Functions to plot the data logged by the buckabuckaboo plugin (https://github.com/dpapathanasiou/buckabuckaboo)
# as an animated sequence of lines per distinct IP address (visitor) against a grayed-out background image
# which represents the page on the site being monitored using R (http://www.r-project.org/).
#
# To run this inside an R repl, use these commands:
#
# > source("mouseTrackPlot.R")
# > plotMousetracks("mousetracks.csv", "page_snapshot_background.png", [normalized width], [normalized height])
#
# The mousetracks.csv file must contain the ip address, date, time, and normalized x,y screen coordinate of the
# visitor mouse movement.
#
library(png)
plotBackground <- function (backgroundImage, w, h, xLabel, yLabel) {
# create an empty plot with a background image,
# using plot information so the image will fill the plot box, and draw it
# source: http://stackoverflow.com/a/12918368
plot(c(0, w), c(0, h), axes=FALSE, xlab=xLabel, ylab=yLabel, type="n")
lim <- par()
rasterImage(backgroundImage, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
}
plotMousetracks <- function (csvFile, backgroundImage, w, h) {
# load the background image for the plot
ima <- readPNG(backgroundImage)
# get the tracking data from the csv file
mousetracks <- read.csv(file=csvFile,header=TRUE,sep=",");
am <- length(mousetracks$ip)
# set the information text: sanitized ip address and date/time
ipPrefix = strsplit(as.character(mousetracks[1,]$ip), '\\.')[[1]][1]
maskedIp = paste(ipPrefix, 'xxx', 'xxx', 'xxx', sep=".") # for privacy, hide the ip except for the first tuple
infoLabel <- paste(maskedIp, mousetracks[1,]$date, mousetracks[1,]$time, sep=" ")
# set the line color and font type
par(fg="#FF007F") # bright pink
infoFont <- c('sans serif', 'bold')
infoPos <- (w / 2) - (w / 3) # set the infoLabel text in the bottom left corner of the plot
# create the first empty plot and infoLabel
plotBackground (ima, w, h, "", "")
text(infoPos, -3, infoLabel, vfont=infoFont)
# draw the lines use the mousetrack data frame
for(i in 0:am) {
r1 <- i+1
r2 <- i+2 # iterate 2 lines at a time through the data frame
if( mousetracks[r1,]$ip != mousetracks[r2,]$ip ) {
# the next data set belongs to a new ip address so clear the plot, after a 2-second pause
Sys.sleep(2)
plotBackground (ima, w, h, "", "")
# get the next sanitized ip address
ipPrefix = strsplit(as.character(mousetracks[r2,]$ip), '\\.')[[1]][1]
maskedIp = paste(ipPrefix, 'xxx', 'xxx', 'xxx', sep=".")
}
# update the information text
text(infoPos, -3, infoLabel, vfont=infoFont, col="white") # clear the current line
infoLabel <- paste(maskedIp, mousetracks[r1,]$date, mousetracks[r1,]$time, sep=" ")
text(infoPos, -3, infoLabel, vfont=infoFont)
# draw the lines between two steps in the data frame
lines(c(mousetracks[r1,]$x, mousetracks[r2,]$x), c(mousetracks[r1,]$y, mousetracks[r2,]$y), lwd=2)
Sys.sleep(0.05) # slight pause between drawing lines, so that the animation is not overwhelmingly fast
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment