Exemple #1
0
 /**
  * @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 ?? []);
 }
Exemple #2
0
 protected function _performance($date = null)
 {
     if (!$date) {
         $date = date('Y-m-d', strtotime('-1 day'));
     }
     $date = date('Y-m-d', strtotime('-1 day'));
     // find the publishers
     $publishers = \User::all(['type = ?' => 'publisher', 'live = ?' => true], ['_id', 'email', 'meta', 'org_id']);
     $dq = ['start' => $date, 'end' => $date];
     $dateQuery = Utils::dateQuery($dq);
     $start = $dateQuery['start'];
     $end = $dateQuery['end'];
     // store AD commission info
     $commInfo = [];
     $orgs = [];
     $advPerfs = [];
     $advertisers = [];
     $adsInfo = [];
     foreach ($publishers as $p) {
         $org = \Organization::find($orgs, $p->org_id);
         // find the clicks for the publisher
         $clicks = Db::query('Click', ['pid' => $p->_id, 'is_bot' => false, 'created' => Db::dateQuery($date, $date)], ['adid', 'country', 'device', 'os', 'referer']);
         $perf = Performance::exists($p, $date);
         // classify the clicks according to AD ID
         $classify = \Click::classify($clicks, 'adid');
         $countryWise = $deviceWise = $osWise = $refWise = [];
         foreach ($classify as $key => $value) {
             $ad = \Ad::find($adsInfo, $key, ['user_id', 'url']);
             $advert = Usr::find($advertisers, $ad->user_id, ['_id', 'meta', 'email', 'org_id']);
             $advertPerf = Usr::findPerf($advPerfs, $advert, $date);
             $countries = \Click::classify($value, 'country');
             foreach ($countries as $country => $records) {
                 $updateData = [];
                 $adClicks = count($records);
                 ArrayMethods::counter($countryWise, $country, $adClicks);
                 $pComm = \Commission::campaignRate($key, $commInfo, $country, array_merge(['type' => 'both', 'publisher' => $p], $dq));
                 $earning = \Ad::earning($pComm, $adClicks);
                 ArrayMethods::copy($earning, $updateData);
                 $updateData['profit'] = $updateData['revenue'] - $updateData['payout'];
                 $updateData['revenue'] = $updateData['payout'];
                 $perf->update($updateData);
                 $aComm = \Commission::campaignRate($key, $commInfo, $country, array_merge(['type' => 'advertiser'], $dq));
                 $updateData = [];
                 $earning = \Ad::earning($aComm, $adClicks);
                 ArrayMethods::copy($earning, $updateData);
                 $advertPerf->update($updateData);
             }
             $deviceWise = Click::classifyInfo(['clicks' => $value, 'type' => 'device', 'arr' => $deviceWise]);
             $osWise = Click::classifyInfo(['clicks' => $value, 'type' => 'os', 'arr' => $osWise]);
             $refWise = Click::classifyInfo(['clicks' => $value, 'type' => 'referer', 'arr' => $refWise]);
         }
         $msg = 'Performance saved for user: '******' with clicks: ' . $perf->clicks . ' impressions: ' . $perf->impressions;
         $this->log($msg);
         $splitCountry = ArrayMethods::topValues($countryWise);
         $splitCountry['rest'] = array_sum($countryWise) - array_sum($splitCountry);
         $meta = ['country' => $splitCountry, 'device' => ArrayMethods::topValues($deviceWise, count($deviceWise)), 'os' => ArrayMethods::topValues($osWise, count($osWise)), 'referer' => ArrayMethods::topValues($refWise, count($refWise))];
         $perf->meta = $meta;
         $perf->save();
     }
     foreach ($advPerfs as $key => $perf) {
         $msg = 'Saving performance for advertiser: ' . $key . ' with clicks: ' . $perf->clicks . ' earning: ' . $perf->revenue . ' impressions: ' . $perf->impressions;
         $this->log($msg);
         $perf->save();
     }
 }