Skip to content

Instantly share code, notes, and snippets.

@SergProduction
Last active January 14, 2017 18:51
Show Gist options
  • Save SergProduction/4c602f88b5893d322ad410a700089520 to your computer and use it in GitHub Desktop.
Save SergProduction/4c602f88b5893d322ad410a700089520 to your computer and use it in GitHub Desktop.
'use strict';
var tools = (function(){ //инструменты
var setCookie = function(name, value, expires, path, domain, secure) {
var expdate = new Date(); //Создаем объект даты
var monthFromNow = expdate.getTime() + (60 * 24 * 60 * 60 * 1000);
expdate.setTime(monthFromNow); //Устанавливаем значение даты
document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires : "; expires=" + expdate.toGMTString()) + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
};
var getCookie = function (c_name) {
if (document.cookie.length > 0) {
var c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
var c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
};
var time = function( format ){ // _format: Y M Dw D h m s
let date = new Date();
let tmp = {
"~Y~": Y = date.getFullYear(), //год в формате (****)
"~M~": M = date.getMonth()+1, //месяц, от 0 до 11
"~D~": D = date.getDate(), // число месяца, от 1 до 31
"~Dw~": Dw = date.getDay()+1, // номер дня в неделе 0-воскресенье, 6-суббота
"~h~": h = date.getHours(),
"~m~": m = date.getMinutes(),
"~s~": s = date.getSeconds(),
};
Object.keys(tmp).forEach( key => {
format = format.replace( key, tmp[key] )
})
return format
};
var allChildren = function(elem, callback){//перебирает все дочернии элементы elem и передает каждый в ф-цию callback
let ch = elem.childNodes;
for( let i=0; i<ch.length; i++ ){
if( ch[i].nodeType != 1 ) continue
if( ch[i].childNodes.length == 0 ){
callback( ch[i] )
}
else{
callback( ch[i] )
allChildren(ch[i], callback)
}
}
};
return {
setCookie,
getCookie,
time,
allChildren
}
}());
var spy = (function(){
let {input, output} = getDataOfCookie(); //input = промежуточные, output = конечные данные
function getDataOfCookie(){
let input, output;
let temp = { //output = промежуточные данные; в случае если нет в куках
click: [],
};
let data = { //output = конечные данные; в случае если нет в куках
form: {
search: '',
form: {},
click: 0,
submit: 0,
},
click: 0,
mousemove: {
x: 0,
y: 0,
all: 0
},
time: {
start: null,
end: null
},
scroll: {
bottom: 0,
all: 0
}
}
const loyaltyInput = tools.getCookie("loyaltyInput");
const loyaltyOutput = tools.getCookie("loyaltyOutput");
if( loyaltyInput && loyaltyOutput){
input = JSON.parse( loyaltyInput );
output = JSON.parse( loyaltyOutput );
}
else if( loyaltyInput && !loyaltyOutput){
input = JSON.parse( loyaltyInput );
output = data;
}
else if( !loyaltyInput && loyaltyOutput ){
input = temp
output = JSON.parse( loyaltyOutput );
}
else {
input = temp;
output = data;
}
return {input, output}
}
//--------------------------------------------------
document.addEventListener( 'click', changeValueInput, true );
document.addEventListener( 'click', monitoringMouseClick );
document.addEventListener( 'mousemove', monitoringMouseMove );
window.addEventListener( 'scroll', monitoringScroll )
document.addEventListener( 'mouseleave', submitData );
//---------------------------------------------------
function checkInputConut( elem ){
let par = elem;
let conunt = 0;
let noSearch = function(el){
if(el.tagName == 'INPUT') conunt +=1;
}
for(let i=0; i<2; i++){
if( par.parentElement != null )
par = par.parentElement;
}
tools.allChildren(par, noSearch);
if( conunt == 1){
return false
}else{
return true
}
}
function changeValueInput(e) {
console.log('changeValueInput', e.target.tagName)
if (e.target.tagName == 'INPUT' || e.target.tagName == 'TEXTAREA'){
e.target.oninput = inChange; // INPUT || TEXTAREA
e.target.onchange = inChange; // INPUT || TEXTAREA
output.form.click += 1;
if( e.target.type == 'submit' ){
output.form.submit += 1
}
function inChange(e){ //сработает при потере фокуса
if (!e.target.value)
return
if( checkInputConut( e.target ) ){
output.form.form[ e.target.name ] = e.target.value;
} else {
output.form.search = e.target.value;
}
}
}
}
function monitoringMouseClick(e){
let my = e.clientY, mx = e.clientX;
//input.click.push({x:mx,y:my});
output.click += 1
submitData()
}
function monitoringMouseMove(e){
let my = e.clientY, mx = e.clientX;
//output.mousemove.x =
//output.mousemove.y =
output.mousemove.all += 1;
}
function monitoringScroll(e){
const html = document.documentElement;
const body = document.body;
let scrollTop = html.scrollTop || body && body.scrollTop || 0;
scrollTop -= html.clientTop;
output.scroll.all +=1;
if( document.documentElement.clientHeight + scrollTop == document.documentElement.scrollHeight )
output.scroll.bottom += 1
//console.log('scrollTop', scrollTop);
}
function submitData(e){
output.time.end = +new Date()
tools.setCookie( "loyaltyInput", JSON.stringify(output) )
tools.setCookie( "loyaltyOutput", JSON.stringify(output) )
var data = new FormData();
data.append( "'keyid", om_keyid );
data.append( "'url", output.url);
data.append( "set", 1 );
data.append( "param", JSON.stringify( output ) );
console.log('output',output)
fetch('http://gibrid24.ru/assets/app/hot/index.php', {
mode: 'no-cors',
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: data
})
.then(
function(response){
//console.log('response', response )
},
function(reject){
console.log('error', reject)
})
}
output.time.start = +new Date()
output.url = location.href
/*
setInterval( () => {
submitData()
},10*1000)
*/
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment