344 lines
11 KiB
PHP
344 lines
11 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 27/gen/2016
|
|
*/
|
|
|
|
class AnalyticsHelper {
|
|
private static $COOKIE_NAME = "FDN_STAT_NUM";
|
|
private static $COOKIE_TIME = "+1 year";
|
|
|
|
/**
|
|
* La funzione si occupa del salvataggio vero e proprio della impression della pagina.
|
|
* UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/pageImpression.php). Nelle pagine base utilizzare invece la funzione di log
|
|
* @param string $ip
|
|
* @param string $content
|
|
* @param string $source
|
|
*/
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* La funzione si occupa di aggiungere una visualizzazione alla pagina corrente
|
|
*/
|
|
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) );
|
|
}
|
|
|
|
|
|
/**
|
|
* La funzione si occupa del salvataggio vero e proprio della impression della notizia.
|
|
* UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/notiziaImpression.php). Nelle pagine base utilizzare invece la funzione di log
|
|
* @param string $ip
|
|
* @param string $content
|
|
* @param string $source
|
|
*/
|
|
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);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* La funzione si occupa di aggiungere una visualizzazione alla notizia correlata a $id
|
|
* @param string $id
|
|
*/
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* Restitusce il numero di impressions per una notizia
|
|
* @param string $id
|
|
*/
|
|
public static function countNotiziaImpressions($id, $start = null, $end = null){
|
|
$searchArray = array("statType"=>StatisticModel::STAT_TYPE_NOTIZIA,"content.notizia"=>$id);
|
|
if (!is_null($start)){
|
|
$searchArray["date"]['$gte'] = new MongoDate($start);
|
|
}
|
|
if (!is_null($end)){
|
|
$searchArray["date"]['$lte'] = new MongoDate($end);
|
|
}
|
|
return GlobalVariables::get("dao")->count("StatisticModel",$searchArray);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* La funzione si occupa del salvataggio vero e proprio della impression del banner.
|
|
* UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/bannerImpression.php). Nelle pagine base utilizzare invece la funzione di log
|
|
* @param string $ip
|
|
* @param string $content
|
|
* @param string $source
|
|
*/
|
|
public static function saveBannerImpression($ip, $bannerId, $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();
|
|
$banner = GlobalVariables::get("dao")->getFirst("BannerModel",array("id"=>$bannerId));
|
|
if (!is_null($banner)){
|
|
$tmp->content->banner = $bannerId;
|
|
$tmp->content->position = $banner->position;
|
|
|
|
$tmp->source = $source;
|
|
$tmp->ip = $ip;
|
|
$tmp->statType = StatisticModel::STAT_TYPE_BANNER_IMPRESSION;
|
|
$stat = new StatisticModel($tmp);
|
|
GlobalVariables::get("dao")->save($stat);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* La funzione si occupa di aggiungere una visualizzazione all banner $id
|
|
* @param string $id
|
|
*/
|
|
public static function logBannerImpression($id){
|
|
if (!isset($_COOKIE[self::$COOKIE_NAME])){
|
|
self::prepareStatCookie();
|
|
}
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
$source = null;
|
|
if (array_key_exists("HTTP_REFERER",$_SERVER)){
|
|
$source = $_SERVER['HTTP_REFERER'];
|
|
}
|
|
|
|
$function = '$.ajax({url: "stats/bannerImpression.php", method: "POST", data: { "ip": "'.$ip.'", "banner": "'.$id.'", "source": "'.$source.'" }, dataType: "html" });';
|
|
echo '<script type="text/javascript">'.$function.'</script>';
|
|
|
|
// OLD VERSION
|
|
// $function = '$.ajax({url: "stats/bannerImpression.php", method: "POST", data: { "ip": "'.$ip.'", "banner": "'.$id.'", "source": "'.$source.'" }, dataType: "html" });';
|
|
// GUIHandler::addOnLoadJsEvent($function);
|
|
}
|
|
|
|
/**
|
|
* Restitusce il numero di impressions per una notizia
|
|
* @param string $id
|
|
*/
|
|
public static function countBannerImpressions($id, $start = null, $end = null){
|
|
$searchArray = array("statType"=>StatisticModel::STAT_TYPE_BANNER_IMPRESSION,"content.banner"=>$id);
|
|
if (!is_null($start)){
|
|
$searchArray["date"]['$gte'] = new MongoDate($start);
|
|
}
|
|
if (!is_null($end)){
|
|
$searchArray["date"]['$lte'] = new MongoDate($end);
|
|
}
|
|
return GlobalVariables::get("dao")->count("StatisticModel",$searchArray);
|
|
}
|
|
|
|
|
|
/**
|
|
* La funzione si occupa del salvataggio vero e proprio della impression dell banner.
|
|
* UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/bannerImpression.php). Nelle pagine base utilizzare invece la funzione di log
|
|
* @param string $ip
|
|
* @param string $content
|
|
* @param string $source
|
|
*/
|
|
public static function saveBannerClick($bannerId, $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();
|
|
$banner = GlobalVariables::get("dao")->getFirst("BannerModel",array("id"=>$bannerId));
|
|
if (!is_null($banner)){
|
|
$tmp->content->banner = $bannerId;
|
|
$tmp->content->position = $banner->position;
|
|
|
|
$tmp->source = $source;
|
|
$tmp->ip = $_SERVER['REMOTE_ADDR'];
|
|
$tmp->statType = StatisticModel::STAT_TYPE_BANNER_CLICK;
|
|
$stat = new StatisticModel($tmp);
|
|
GlobalVariables::get("dao")->save($stat);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* La funzione si occupa di aggiungere una visualizzazione all banner $id
|
|
* @param string $id
|
|
*/
|
|
public static function getBannerClickLink($banner){
|
|
if (!isset($_COOKIE[self::$COOKIE_NAME])){
|
|
self::prepareStatCookie();
|
|
}
|
|
return "bannerClick('".$banner->id."','".$banner->link."');";
|
|
}
|
|
|
|
/**
|
|
* Restitusce il numero di impressions per una notizia
|
|
* @param string $id
|
|
*/
|
|
public static function countBannerClicks($id, $start = null, $end = null){
|
|
$searchArray = array("statType"=>StatisticModel::STAT_TYPE_BANNER_CLICK,"content.banner"=>$id);
|
|
if (!is_null($start)){
|
|
$searchArray["date"]['$gte'] = new MongoDate($start);
|
|
}
|
|
if (!is_null($end)){
|
|
$searchArray["date"]['$lte'] = new MongoDate($end);
|
|
}
|
|
return GlobalVariables::get("dao")->count("StatisticModel",$searchArray);
|
|
}
|
|
|
|
|
|
/**
|
|
* Restituisce il banner da mostrare tenendo in considerazione quelli già visualizzati e le visualizzazioni totali
|
|
* @param int $position
|
|
* @param string $replaced id del banner rimpiazzato
|
|
*/
|
|
public static function getBannerToShow($position, $replaced = null, $category = BannerModel::GENERAL_CATEGORY){
|
|
// Rimuovi il replaced dalla lista
|
|
$shown = array();
|
|
$rval = null;
|
|
if (ASDSessionHandler::sessionValueExists("bannerActive")){
|
|
$shown = ASDSessionHandler::getSessionValue("bannerActive");
|
|
|
|
if (!is_null($replaced) && in_array($replaced, $shown)){
|
|
$key = array_search($replaced, $shown);
|
|
unset($shown[$key]);
|
|
ASDSessionHandler::setSessionValue("bannerActive",$shown);
|
|
}
|
|
}
|
|
|
|
// idlist contiene gli id dei banner in ordine di visualizzazione crescente (esclusi quelli non visualizzati)
|
|
$pipeline = PipelineHelper::getTodaysBannerVisualizationPipeline($position);
|
|
$stats = GlobalVariables::get("dao")->aggregate("StatisticModel",$pipeline);
|
|
// var_dump($stats);
|
|
$idList = array();
|
|
if (sizeof($stats)>0){
|
|
foreach ($stats as $stat){
|
|
$idList[] = $stat["_id"]["banner"];
|
|
}
|
|
}
|
|
|
|
$baseSearchArray = array("position"=>$position,"end"=>array('$gte'=>new MongoDate(strtotime("tomorrow"))),"category"=>$category);
|
|
// Prendi il primo dei "mai mostrati oggi" tra quelli non presenti nella pagina
|
|
$todayPlusShown = array_merge($idList,$shown);
|
|
if (sizeof($todayPlusShown)>0){
|
|
$tmp = array_map(function ($id) {return new MongoId($id);}, $todayPlusShown);
|
|
$tmp = array('_id'=>array('$nin'=>$tmp));
|
|
$rval = GlobalVariables::get("dao")->getFirst("BannerModel", array_merge($baseSearchArray, $tmp) );
|
|
}
|
|
else {
|
|
$rval = GlobalVariables::get("dao")->getFirst("BannerModel", $baseSearchArray);
|
|
}
|
|
|
|
if (is_null($rval)){
|
|
$todayLessShown = array_values(array_diff($idList, $shown));
|
|
if (sizeof($todayLessShown)>0){
|
|
$i = 0;
|
|
while (is_null($rval) && $i<sizeof($todayLessShown)){
|
|
$condition = array_merge($baseSearchArray, array("id"=>$todayLessShown[$i]) );
|
|
$rval = GlobalVariables::get("dao")->getFirst("BannerModel", $condition );
|
|
$i++;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_null($rval)){
|
|
$shown[] = $rval->id;
|
|
ASDSessionHandler::setSessionValue("bannerActive",$shown);
|
|
}
|
|
return $rval;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
?>
|