Created
September 27, 2012 12:17
-
-
Save thefish/3793702 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
По адресу trackDomain должен находиться скрипт response.php или аналог. | |
Куки можно читать оттуда же. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Скрипт для проставления куки. | |
//Attention! обращения к этому скриту ддолжны писаться в логи, по ним и надо вести оосновной анализ | |
//Можно делать это на встроенном языке конфига nginx, целый ПХП тут не нужен | |
DEFINE('SEP', '||'); | |
error_reporting(0); | |
$arr = explode(SEP, $_COOKIE[$_REQUEST['cookieName']]); | |
array_unshift($arr, $_REQUEST['arr']); | |
$arr = implode(SEP, array_filter(array_unique($arr))); | |
setcookie( | |
$_REQUEST['cookieName'], | |
$arr, | |
(time() + 946080000), | |
"/"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @author [email protected] | |
* Сохраняет в куки 10 последних URL. | |
* Осторожно - максимальный размер куки 4096 байт | |
* При ОЧЕНЬ длинных url часть старых данных может легко потеряться или стать невалидной | |
*/ | |
var id = 1024; //Это id который мы получаем от nginx | |
var maxStored = 10; //Сколько url хранить | |
var splitSymbol = '||'; | |
var cookieName = "tracker-" + id; | |
var trackDomain = "tracker.dev"; | |
function trackVisitor() { | |
//console.log(cookieName); //debug | |
var current = document.URL; | |
var arr = readCookie(cookieName).split(splitSymbol); | |
//console.log(arr); //debug | |
if (arr.indexOf(current) == -1) { //такого нет? | |
arr.unshift(current); //Вставляем в начало | |
if (arr.length >maxStored) arr.length = maxStored; //Оставляем 10 первых элементов | |
var elt = document.createElement('div'); | |
elt.innerHTML = '<img src="http://'+trackDomain+'/pixel.png?cookieName='+cookieName+'&arr=' + arr.join(splitSymbol) + '" width="1" height="1" style="margin-left:-999px;" />'; | |
document.body.appendChild(elt); | |
//document.cookie = cookieName + "=" + arr.join(splitSymbol) + "; Domain="+trackDomain+";expires=Thu, 31 Dec 2037 00:00:00 UTC; path=/"; //Ставим КИСА КУКУ | |
console.log('yay'); | |
} | |
} | |
function readCookie(name) {var nameEQ = name + "="; | |
var ca = document.cookie.split(';'); | |
for(var i=0;i < ca.length;i++) { | |
var c = ca[i]; | |
while (c.charAt(0)==' ') c = c.substring(1,c.length); | |
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); | |
} | |
return ''; | |
} | |
//IE6 hack - там у массива нет indexOf, допиливаем ручками | |
if (!Array.prototype.indexOf) {Array.prototype.indexOf = function (obj, fromIndex) { | |
if (fromIndex == null) { | |
fromIndex = 0; | |
} else if (fromIndex < 0) { | |
fromIndex = Math.max(0, this.length + fromIndex); | |
} | |
for (var i = fromIndex, j = this.length; i < j; i++) { | |
if (this[i] === obj) | |
return i; | |
} | |
return -1; | |
}; | |
} | |
//Эмуляция $(document).ready(); | |
if(document.addEventListener) { // FF, Опера, вебкит | |
document.addEventListener("DOMContentLoaded", function() { | |
document.removeEventListener( "DOMContentLoaded", arguments.callee, false); | |
trackVisitor(); | |
}, false); | |
} | |
else if(document.attachEvent) { // Ослег | |
document.attachEvent("onreadystatechange", function() { | |
if(document.readyState === "complete") { | |
document.detachEvent("onreadystatechange", arguments.callee); | |
trackVisitor(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment