/** * Authenticate and return a seeded gapi instance * * @param String $email * @param String $password * @return gapi */ public static function authenticate($email, $password) { $auth_method = new gapiClientLogin(); $auth_method->fetchToken($email, $password); return new gapi($auth_method); }
/** Analytics page, uses the analytics class to grab information from Google analytics API */ function analytics() { if (isAdmin() == false) { redirect('', 'location'); } // list table fields $this->template->set_template("urika_admin"); $out = "<p><strong>Analyics for urika-app.com:</strong> An general overview of recent activity on the site for the last<em> 30 days</em>.<br/><br/></p>"; /* Start analytics data grab */ $this->load->helper("analytics_api_helper"); $login = "******"; $password = "******"; //$profile_id = "4301220"; $profile_id = "41180304"; $start_date = time() - 60 * 60 * 24 * 30; $end_date = time() - 60 * 60 * 24 * 1; // authenticate $ga = gapiClientLogin::authenticate($login, $password); // general data $ga->requestReportData($profile_id, null, array('pageviews', 'visits', 'visitors', 'timeOnSite'), null, null, $start_date, $end_date); $data = $ga->getResults(); // visits $ga->requestReportData($profile_id, array('date'), array('visits'), array('date'), null, $start_date, $end_date, 1, 30); $visits_data = $ga->getResults(); // traffic sources $ga->requestReportData($profile_id, array('medium'), array('visits'), null, null, $start_date, $end_date); $traffic_data = $ga->getResults(); // pages $ga->requestReportData($profile_id, array('pagePath'), array('visits'), array('-visits'), null, $start_date, $end_date, 1, 10); $page_data = $ga->getResults(); /* Start Google charts bit and sort output */ // 1) General info $general = ' <table style="font-size: 12px;"> <tr><th style="text-align:left; font-size: 12px">Pageviews:</th><td> ' . $data[0]->getPageviews() . '</td></tr> <tr><th style="text-align:left; font-size: 12px">Visits:</th><td> ' . $data[0]->getVisits() . '</td></tr> <tr><th style="text-align:left; font-size: 12px">Pages per Visit:</th><td> ' . round($data[0]->getPageViews() / $data[0]->getVisits()) . '</td></tr> <tr><th style="text-align:left; font-size: 12px">Visitors:</th><td> ' . $data[0]->getVisitors() . '</td></tr> <tr><th style="text-align:left; font-size: 12px">Average time on site:</th><td> ' . round($data[0]->getTimeonsite() / $data[0]->getVisits()) . 's</td></tr> </table> '; // 2) get visits data $visits_chd = ""; $visits_chl = ""; $total = 0; $min = 100000; $max = 0; $k = 0; foreach ($visits_data as $result) { $visits_chd .= $result->getVisits() . ','; if ($k == 0) { $visits_chl .= date("jS M", strtotime($result->getDate())) . '|'; $k++; } else { $k++; if ($k == 4) { $k = 0; } } // numeric values $total += $result->getVisits(); if ($result->getVisits() < $min) { $min = $result->getVisits(); } if ($result->getVisits() > $max) { $max = $result->getVisits(); } } $visits_chd = substr($visits_chd, 0, -1); $visits_chl = substr($visits_chl, 0, -1); $avg = round($total / 30); $visits_chart_src = 'https://chart.googleapis.com/chart?cht=lc&chs=500x300&chxt=x,y,r&chxr=1,0,' . $max . '|2,0,' . $max . '&chds=0,' . $max . '&chxl=2:|min|average|max&chxs=2,0000dd,13,-1,t,FF0000&chxp=2,' . $min . ',' . $avg . ',' . $max . '&chxtc=2,-500&chd=t:' . $visits_chd . '&chl=' . $visits_chl; // 3) traffic sources $traffic_chd = ""; $traffic_chl = ""; foreach ($traffic_data as $result) { $traffic_chd .= $result->getVisits() . ','; $traffic_chl .= $result->getMedium() . ' - ' . $result->getVisits() . '|'; } $traffic_chd = substr($traffic_chd, 0, -1); $traffic_chl = substr($traffic_chl, 0, -1); $traffic_chart_src = 'https://chart.googleapis.com/chart?cht=p&chs=400x250&chd=t:' . $traffic_chd . '&chl=' . $traffic_chl; // 4) Page views $pages = '<table style="font-size: 12px;">'; foreach ($page_data as $result) { $pages .= ' <tr> <th style="text-align: left; font-size:12px;">' . $result->getPagepath() . '</th> <td>' . $result->getVisits() . '</td> </tr> '; } $pages .= '</table>'; /* End all API stuff */ $chart_html = ' <div class="left col-8"> <h3 style="margin-bottom: 10px;">General Data</h3> ' . $general . ' </div> <div class="left col-8" style="margin-left: 20px;"> <h3 style="margin-bottom: 10px;">Visits</h3> <img src="' . $visits_chart_src . '" /> </div> <div class="clear"> </div> <div class="left col-8"> <h3 style="margin-bottom: 10px;">Traffic Sources</h3> <img src="' . $traffic_chart_src . '" /> </div> <div class="left col-8" style="margin-left: 20px;"> <h3 style="margin-bottom: 10px;">Pages</h3> ' . $pages . ' </div> '; $out .= $chart_html; $this->template->add_css("assets/css/admin.css"); $this->template->write("menu_lis", $this->_tableMenuLinksHTML()); $this->template->write("feature", "<h2>Admin Dashboard</h2>"); $this->template->write("content", $out); $this->template->render(); // set back to defaut $this->template->set_template("urika"); }