protected static function _livePerfQuery($match = []) { $records = Db::collection('Click')->aggregate([['$match' => $match], ['$project' => ['adid' => 1, 'country' => 1, '_id' => 0]], ['$group' => ['_id' => ['adid' => '$adid', 'country' => '$country'], 'count' => ['$sum' => 1]]], ['$group' => ['_id' => '$_id.adid', 'countries' => ['$push' => ['country' => '$_id.country', 'count' => '$count']], 'count' => ['$sum' => '$count']]], ['$sort' => ['count' => -1]]]); return $records; }
/** * @before _secure * @after _displayData * @todo fetch realtime data for today (if start == end) But if start != today then fetch data from * performance table and add it with realtime */ public function publishers() { $this->seo(["title" => "Publisher Rankings"]); $view = $this->getActionView(); $match = ['pid' => ['$in' => $this->org->users('publisher')], 'is_bot' => false, 'created' => Db::dateQuery($this->start, $this->end)]; $records = Db::collection('Click')->aggregate([['$match' => $match], ['$project' => ['pid' => 1, 'device' => 1, '_id' => 0]], ['$group' => ['_id' => ['pid' => '$pid', 'device' => '$device'], 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]], ['$limit' => (int) $this->limit]]); $stats = $deviceStats = []; foreach ($records as $r) { $obj = Utils::toArray($r); $_id = $obj['_id']; $pid = Utils::getMongoID($_id['pid']); ArrayMethods::counter($stats, $pid, $obj['count']); if (!isset($deviceStats[$pid])) { $deviceStats[$pid] = []; } ArrayMethods::counter($deviceStats[$pid], $_id['device'], $obj['count']); /*$adClicks = Click::classify($pubClicks, 'adid'); $br = 0; $total = count($adClicks); foreach ($adClicks as $adid => $records) { $pageViews = Db::query('PageView', ['pid' => $pid, 'adid' => $adid], ['cookie']); // Create a counter based on cookie and select only the values whose // counter is less than 2 $multiPageSessions = 0; $totalClicks = count($records); $pageViews = Click::classify($pageViews, 'cookie'); foreach ($pageViews as $ckid => $rows) { if (count($rows) >= 2) { $multiPageSessions++; } } $bounce = 1 - ($multiPageSessions / $totalClicks); $br += $bounce; } $bounceRate[$pid] = (int) (round($br / $total, 2) * 100);*/ } $view->set('stats', $stats)->set('deviceStats', $deviceStats)->set('bounceRate', $bounceRate ?? []); }
public static function total($dq = [], $user = null, $fields = []) { $name = __CLASS__; if (count($fields) === 0) { $fields = ['clicks', 'conversions', 'impressions', 'revenue']; } $project = ['user_id' => 1, '_id' => 0]; $group = ['_id' => '$user_id']; foreach ($fields as $f) { $project[$f] = 1; $group[$f] = ['$sum' => '$' . $f]; } $match = ['created' => Db::dateQuery($dq['start'], $dq['end'])]; if (is_array($user)) { $keys = ArrayMethods::arrayKeys($user, '_id'); $match['user_id'] = ['$in' => Db::convertType($keys)]; } else { $match['user_id'] = Db::convertType($user->_id); } $records = Db::collection($name)->aggregate([['$match' => $match], ['$project' => $project], ['$group' => $group]]); $result = []; foreach ($records as $r) { $obj = Utils::toArray($r); $add = []; foreach ($fields as $f) { $add[$f] = $obj[$f]; } ArrayMethods::add($add, $result); } return $result; }
protected static function _perfQuery($match, $extra) { $meta = $extra['meta'] ?? true; $clickCol = Db::collection('Click'); $group = ['country' => 1, 'device' => 1, 'os' => 1, 'referer' => 1, '_id' => 0]; $_id = ['country' => '$country', 'os' => '$os', 'device' => '$device', 'referer' => '$referer']; if (!$meta) { // if meta is not required $group = ['country' => 1, '_id' => 0]; $_id = ['country' => '$country']; } $records = $clickCol->aggregate([['$match' => $match], ['$project' => $group], ['$group' => ['_id' => $_id, 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]]]); return $records; }
/** * @before _secure */ public function manage() { $this->seo(['title' => 'Campaign: List', 'description' => 'Manage campaigns']); $view = $this->getActionView(); $campaigns = []; $page = RM::get("page", 1); $limit = RM::get("limit", 10); $property = RM::get("property"); $value = RM::get("value"); switch (RM::get("sort")) { case 'trending': $start = RM::get("start", date("Y-m-d", strtotime('now'))); $end = RM::get("end", date("Y-m-d", strtotime('now'))); $q = ['start' => $start, 'end' => $end]; $view->set($q); // Only find the ads for this organizations $allAds = \Ad::all(['org_id' => $this->org->_id], ['_id']); $in = Db::convertType(array_keys($allAds)); $clickCol = Db::collection('Click'); $match = ['created' => Db::dateQuery($start, $end), 'is_bot' => false, 'adid' => ['$in' => $in]]; $records = $clickCol->aggregate([['$match' => $match], ['$project' => ['adid' => 1, '_id' => 0]], ['$group' => ['_id' => '$adid', 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]], ['$limit' => (int) $limit]]); foreach ($records as $r) { $arr = Utils::toArray($r); $id = Utils::getMongoID($arr['_id']); $campaigns[] = Ad::first(["id = ?" => $id]); } $count = $limit; break; default: $query = ["org_id = ?" => $this->org->id]; if (in_array($property, ["user_id", "live", "id"])) { $query[$property] = $value; } else { if (in_array($property, ["url", "title"])) { $query[$property] = Utils::mongoRegex(preg_quote($value)); } } $campaigns = \Ad::all($query, [], 'created', 'desc', $limit, $page); $count = \Ad::count($query); break; } $categories = \Category::all(['org_id' => $this->org->_id], ['_id', 'name']); $active = \Ad::count(["org_id = ?" => $this->org->id, "live = ?" => 1]); $inactive = \Ad::count(["org_id = ?" => $this->org->id, "live = ?" => 0]); $view->set("campaigns", $campaigns)->set("count", $count)->set("active", $active)->set("inactive", $inactive)->set("limit", $limit)->set("page", $page)->set("property", $property)->set("value", $value)->set("categories", $categories); }
/** * @before _admin */ public function manage() { $this->seo(["title" => "List Publisher"]); $view = $this->getActionView(); $page = RM::get("page", 1); $limit = RM::get("limit", 10); $publishers = []; $start = RM::get("start", date('Y-m-d')); $end = RM::get("end", date('Y-m-d')); $view->set(['start' => $start, 'end' => $end]); switch (RM::get("sort")) { case 'trending': $match = ['pid' => ['$in' => $this->org->users('publisher')], 'is_bot' => false, 'created' => Db::dateQuery($start, $end)]; $clickCol = Db::collection('Click'); $records = $clickCol->aggregate([['$match' => $match], ['$project' => ['pid' => 1, '_id' => 0]], ['$group' => ['_id' => '$pid', 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]], ['$limit' => (int) $limit]]); foreach ($records as $r) { $arr = Utils::toArray($r); $id = Utils::getMongoID($arr['_id']); $publishers[] = User::first(["id = ?" => $id]); } $count = $limit; break; default: $query = ["type = ?" => "publisher", "org_id = ?" => $this->org->_id]; $property = RM::get("property"); $value = RM::get("value"); if (in_array($property, ["live", "id"])) { $query["{$property} = ?"] = $value; } else { if (in_array($property, ["email", "name", "phone"])) { $query["{$property} = ?"] = Db::convertType($value, 'regex'); } } $publishers = \User::all($query, [], 'created', 'desc', $limit, $page); $count = \User::count($query); break; } $active = \User::count(["type = ?" => "publisher", "org_id = ?" => $this->org->_id, "live = ?" => 1]); $inactive = \User::count(["type = ?" => "publisher", "org_id = ?" => $this->org->_id, "live = ?" => 0]); $view->set("publishers", $publishers)->set("property", $property)->set("value", $value)->set("active", $active)->set("inactive", $inactive)->set("count", $count)->set("limit", $limit)->set("page", $page); }