Skip to content

Instantly share code, notes, and snippets.

@itayw
Created October 28, 2014 08:06
Show Gist options
  • Save itayw/c5da9862d2bfccd72bd3 to your computer and use it in GitHub Desktop.
Save itayw/c5da9862d2bfccd72bd3 to your computer and use it in GitHub Desktop.
Joola wbepage heatmap
joola.init({
host: 'https://itay-playground.app.joo.la:443',
APIToken: 'DWyrb8NOmvdP2BwzaBU9gl1u4TVU4b1a'
}, function (err, ref) {
console.log('Joola initialized, version ', joola.VERSION);
//hook mouse moves
var pointindex = 0;
var movestack = [];
document.body.onmousemove = function (e) {
if (pointindex++ % 5 === 0) {
var point = {x: e.offsetX.toString(), y: e.offsetY.toString(), hovers: 1};
movestack.push(point);
}
};
setInterval(function () {
if (movestack.length > 0) {
var topush = movestack.splice(0, 1000);
joola.beacon.insert('heatmap_hover', topush, function (err) {
if (err)
console.log('err while sending becaon', err);
});
}
}, 1000);
setInterval(function () {
pointindex = 0;
}, 200);
//add the overlay that we draw the heatmap on
var canvas = d3.select('body').append('svg')
.attr('width', '100%')
.attr('height', Math.max(document.body.scrollHeight, document.body.offsetHeight,
document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight));
function plotmove(x, y) {
canvas.append('circle')
.attr('cx', x)
.attr('cy', y)
.attr('r', 5);
}
//run the query
joola.query.fetch({__: 'J9Mp40rSu1s2HKRqmaTMPtk1ats3sNYM'}, {
timeframe: 'last_1_day',
dimensions: ['x', 'y'],
metrics: ['hovers'],
collection: 'heatmap_hover',
realtime: true
}, function (err, result) {
if (err)
throw err;
result.documents.forEach(function (doc) {
var values = doc.values;
var x = values.x;
var y = values.y;
var hovers = values.hovers;
plotmove(x, y);
})
})
});
<!DOCTYPE HTML>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://joola-backend-stats.app.joo.la/joola.js"></script>
<script src="js/heatmap.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment