346 lines
11 KiB
PHP
346 lines
11 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 27/gen/2016
|
|
*/
|
|
|
|
class AnalyticsHelper {
|
|
private const COOKIE_UNIQUE_NAME = 'FDN_STAT_NUM';
|
|
|
|
private const COOKIE_TODAY_NAME = 'FDN_TODAY_VISIT';
|
|
|
|
private const COOKIE_MONTH_NAME = 'FDN_MONTH_VISIT';
|
|
|
|
private static function isNewAbsolute(): bool {
|
|
return !isset($_COOKIE[self::COOKIE_UNIQUE_NAME]);
|
|
}
|
|
private static function isNewToday(): bool {
|
|
return !isset($_COOKIE[self::COOKIE_TODAY_NAME]);
|
|
}
|
|
private static function isNewThisMonth(): bool {
|
|
return !isset($_COOKIE[self::COOKIE_MONTH_NAME]);
|
|
}
|
|
|
|
private static function updateCookies(){
|
|
setcookie(
|
|
self::COOKIE_UNIQUE_NAME,
|
|
uniqid("",true),
|
|
mktime(0, 0, 0, date('n'), date('j'), date('Y') + 1 )
|
|
);
|
|
setcookie(
|
|
self::COOKIE_TODAY_NAME,
|
|
time(),
|
|
mktime(0, 0, 0, date('n'), date('j') + 1)
|
|
);
|
|
setcookie(
|
|
self::COOKIE_MONTH_NAME,
|
|
time(),
|
|
mktime(0, 0, 0, date('n') + 1, 1)
|
|
);
|
|
}
|
|
|
|
public static function pageImpression($content, $d, $m, $y, $isNewToday, $isNewMonth, $isNewAbsolute) {
|
|
$dao = GlobalVariables::get("dao");
|
|
if ($dao instanceof GenericDao) {
|
|
$upd = [
|
|
'$inc' => [
|
|
'views' => 1
|
|
]
|
|
];
|
|
if ($isNewAbsolute || $isNewMonth || $isNewToday) {
|
|
$upd['$inc']['dayNewVisitors'] = 1;
|
|
}
|
|
if ($isNewAbsolute || $isNewMonth) {
|
|
$upd['$inc']['monthNewVisitors'] = 1;
|
|
}
|
|
if ($isNewAbsolute) {
|
|
$upd['$inc']['absNewVisitors'] = 1;
|
|
}
|
|
$dao->rawUpsert(
|
|
PageViewStatsModel::class,
|
|
PageViewStatsModel::getFilter($content, $d, $m, $y),
|
|
$upd
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
self::pageImpression(
|
|
$content,
|
|
intval(date('d')),
|
|
intval(date('m')),
|
|
intval(date('Y')),
|
|
self::isNewToday(),
|
|
self::isNewThisMonth(),
|
|
self::isNewAbsolute()
|
|
);
|
|
self::updateCookies();
|
|
}
|
|
|
|
/**
|
|
* La funzione si occupa di aggiungere una visualizzazione alla pagina corrente
|
|
*/
|
|
public static function logPageImpression(){
|
|
$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);
|
|
}
|
|
|
|
|
|
public static function notiziaImpression($content, $d, $m, $y) {
|
|
$dao = GlobalVariables::get("dao");
|
|
if ($dao instanceof GenericDao) {
|
|
$dao->rawUpsert(
|
|
ArticleStatsModel::class,
|
|
ArticleStatsModel::getFilter($content, $d, $m, $y),
|
|
[
|
|
'$inc' => [
|
|
'views' => 1
|
|
]
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
self::notiziaImpression(
|
|
$notiziaId,
|
|
intval(date('d')),
|
|
intval(date('m')),
|
|
intval(date('Y'))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* La funzione si occupa di aggiungere una visualizzazione alla notizia correlata a $id
|
|
* @param string $id
|
|
*/
|
|
public static function logNotiziaImpression($id){
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
$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){
|
|
$ret = GlobalVariables::get("dao")->aggregate(
|
|
ArticleStatsModel::class,
|
|
PipelineHelper::getNotiziaImpressions($id, $start, $end)
|
|
);
|
|
|
|
return sizeof($ret) == 0 ? 0 : $ret[0]['tot'];
|
|
}
|
|
|
|
|
|
public static function bannerImpression($bannerId, $d, $m, $y) {
|
|
$dao = GlobalVariables::get("dao");
|
|
if ($dao instanceof GenericDao) {
|
|
$dao->rawUpsert(
|
|
BannerStatsModel::class,
|
|
BannerStatsModel::getFilter($bannerId, $d, $m, $y),
|
|
[
|
|
'$inc' => [
|
|
'views' => 1
|
|
]
|
|
]
|
|
);
|
|
}
|
|
}
|
|
public static function bannerClick($bannerId, $d, $m, $y) {
|
|
$dao = GlobalVariables::get("dao");
|
|
if ($dao instanceof GenericDao) {
|
|
$dao->rawUpsert(
|
|
BannerStatsModel::class,
|
|
BannerStatsModel::getFilter($bannerId, $d, $m, $y),
|
|
[
|
|
'$inc' => [
|
|
'clicks' => 1
|
|
]
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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){
|
|
self::bannerImpression(
|
|
$bannerId,
|
|
intval(date('d')),
|
|
intval(date('m')),
|
|
intval(date('Y'))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* La funzione si occupa di aggiungere una visualizzazione all banner $id
|
|
* @param string $id
|
|
*/
|
|
public static function logBannerImpression($id){
|
|
$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 un banner
|
|
* @param string $id
|
|
*/
|
|
public static function countBannerImpressions($id, $start = null, $end = null){
|
|
$ret = GlobalVariables::get("dao")->aggregate(
|
|
BannerStatsModel::class,
|
|
PipelineHelper::getBannerImpressions($id, $start, $end)
|
|
);
|
|
|
|
return sizeof($ret) == 0 ? 0 : $ret[0]['tot'];
|
|
}
|
|
|
|
|
|
/**
|
|
* 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){
|
|
self::bannerClick(
|
|
$bannerId,
|
|
intval(date('d')),
|
|
intval(date('m')),
|
|
intval(date('Y'))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* La funzione si occupa di aggiungere una visualizzazione all banner $id
|
|
* @param string $id
|
|
*/
|
|
public static function getBannerClickLink($banner){
|
|
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) {
|
|
$ret = GlobalVariables::get("dao")->aggregate(
|
|
BannerStatsModel::class,
|
|
PipelineHelper::getBannerClicks($id, $start, $end)
|
|
);
|
|
|
|
return sizeof($ret) == 0 ? 0 : $ret[0]['tot'];
|
|
}
|
|
|
|
|
|
/**
|
|
* 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) {
|
|
// ?TODO: Ragionaci
|
|
// 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"))),"categories"=>$category);
|
|
$baseSearchArray = array("position"=>$position,"end"=>array('$gte'=>new MongoDB\BSON\UTCDateTime( strtotime("tomorrow CET") * 1000 ) ),"categories"=>$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 MongoDB\BSON\ObjectID($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;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
?>
|