105 lines
2.9 KiB
PHP
105 lines
2.9 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 27/gen/2016
|
|
*/
|
|
|
|
class AnalyticsHelper {
|
|
private static $COOKIE_NAME = "FDN_STAT_NUM";
|
|
// Switch this value to +1 year in production environment
|
|
private static $COOKIE_TIME = "+5 minutes";
|
|
|
|
public static function savePageImpression($ip, $content, $source){
|
|
$tmp = new stdClass();
|
|
|
|
$tmp->user = null;
|
|
if (isset($_COOKIE[self::$COOKIE_NAME])){
|
|
$id = $_COOKIE[self::$COOKIE_NAME];
|
|
|
|
$tmp->user = new stdClass();
|
|
$tmp->user->id = $id;
|
|
$tmp->user->isNew = false;
|
|
if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){
|
|
$tmp->user->isNew = true;
|
|
}
|
|
}
|
|
|
|
$tmp->content = $content;
|
|
$tmp->source = $source;
|
|
$tmp->ip = $ip;
|
|
$tmp->statType = StatisticModel::STAT_TYPE_PAGE;
|
|
$stat = new StatisticModel($tmp);
|
|
GlobalVariables::get("dao")->save($stat);
|
|
}
|
|
|
|
public static function logPageImpression(){
|
|
if (!isset($_COOKIE[self::$COOKIE_NAME])){
|
|
self::prepareStatCookie();
|
|
}
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
$content = $_SERVER['PHP_SELF'];
|
|
$source = null;
|
|
if (array_key_exists("HTTP_REFERER",$_SERVER)){
|
|
$source = $_SERVER['HTTP_REFERER'];
|
|
}
|
|
|
|
$function = '$.ajax({url: "stats/pageImpression.php", method: "POST", data: { "ip": "'.$ip.'", "content": "'.$content.'", "source": "'.$source.'" }, dataType: "html" });';
|
|
GUIHandler::addOnLoadJsEvent($function);
|
|
}
|
|
|
|
|
|
private static function prepareStatCookie(){
|
|
setcookie(self::$COOKIE_NAME, uniqid("",true), strtotime(self::$COOKIE_TIME) );
|
|
}
|
|
|
|
|
|
public static function saveNotiziaImpression($ip, $notiziaId, $source){
|
|
$tmp = new stdClass();
|
|
|
|
$tmp->user = null;
|
|
if (isset($_COOKIE[self::$COOKIE_NAME])){
|
|
$id = $_COOKIE[self::$COOKIE_NAME];
|
|
|
|
$tmp->user = new stdClass();
|
|
$tmp->user->id = $id;
|
|
$tmp->user->isNew = false;
|
|
if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){
|
|
$tmp->user->isNew = true;
|
|
}
|
|
}
|
|
|
|
$tmp->content = new stdClass();
|
|
$notizia = GlobalVariables::get("dao")->getFirst("NotiziaModel",array("id"=>$notiziaId));
|
|
if (!is_null($notizia)){
|
|
$tmp->content->notizia = $notiziaId;
|
|
$tmp->content->category = $notizia->category;
|
|
|
|
$tmp->source = $source;
|
|
$tmp->ip = $ip;
|
|
$tmp->statType = StatisticModel::STAT_TYPE_NOTIZIA;
|
|
$stat = new StatisticModel($tmp);
|
|
GlobalVariables::get("dao")->save($stat);
|
|
}
|
|
|
|
}
|
|
|
|
public static function logNotiziaImpression($id){
|
|
if (!isset($_COOKIE[self::$COOKIE_NAME])){
|
|
self::prepareStatCookie();
|
|
}
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
$notizia = $id;
|
|
$source = null;
|
|
if (array_key_exists("HTTP_REFERER",$_SERVER)){
|
|
$source = $_SERVER['HTTP_REFERER'];
|
|
}
|
|
|
|
$function = '$.ajax({url: "stats/notiziaImpression.php", method: "POST", data: { "ip": "'.$ip.'", "notizia": "'.$id.'", "source": "'.$source.'" }, dataType: "html" });';
|
|
GUIHandler::addOnLoadJsEvent($function);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|