Skip to content

Instantly share code, notes, and snippets.

@rossmari
Last active August 29, 2015 14:05
Show Gist options
  • Save rossmari/f715d84d4d40d8f8babf to your computer and use it in GitHub Desktop.
Save rossmari/f715d84d4d40d8f8babf to your computer and use it in GitHub Desktop.
crazy way to inspect page visits count and site visits count
$(function()
{
//============ Subscribe on events example ==
// $(document).on('pvc_changed', function(event, pvc_count){
// alert(pvc_count);
// });
//
// $(document).on('svc_changed', function(event, svc_count){
// alert(svc_count);
// });
//===========================================
var d = new Date()
var id = d.getMinutes() + '.' + d.getSeconds() + '.' + d.getMilliseconds();
add_site_viewer(id);
var pvc = update_cookie_count('page_visits_count', 1);
$(document).trigger('pvc_changed', pvc);
if (compareVisitAndLeaveTime(new Date()))
{
var svc = update_cookie_count('site_visits_count', 1);
$(document).trigger('svc_changed', svc);
}
$(window).bind('beforeunload', function() {
if (!window.btn_clicked) {
var length = remove_site_viewer(id);
if (length == 0) {
setLeaveTime();
}
}
});
function update_cookie_count(name, update_on)
{
if ($.cookie(name) == null)
{
$.cookie(name, 1);
return 1;
}
else
{
var count = parseInt($.cookie(name));
count = count + update_on;
if(count < 0) {
count = 0;
}
$.cookie(name, count);
return count;
}
}
function remove_site_viewer(id)
{
var array = [];
if ($.cookie('site_viewers') == null)
{
array = []
}
else {
var viewers_row = $.cookie('site_viewers');
array = JSON.parse(viewers_row);
}
var index = array.indexOf(id);
array.splice(index, 1);
$.cookie('site_viewers', JSON.stringify(array));
return array.length;
}
function add_site_viewer(id)
{
var array = [];
if ($.cookie('site_viewers') == null)
{
array = []
}
else {
var viewers_row = $.cookie('site_viewers');
array = JSON.parse(viewers_row);
}
array.push(id);
$.cookie('site_viewers', JSON.stringify(array));
}
function setLeaveTime()
{
var leave_time = new Date();
$.cookie('leave_time', leave_time);
}
function compareVisitAndLeaveTime(visit)
{
var leave = Date.parse($.cookie('leave_time'))
var delta = (visit - leave) / 1000.0 / 60.0;
//wait 5 minutes before decide that it's a new site visit
return delta >= 5.0
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment