protected function __construct($config) { if (!$config) { throw new \exception('`$config` cant be null when constructing StarterKit'); } if ($config['strict']) { error_reporting(E_ALL | E_NOTICE | E_STRICT); } $this->public_html = $config['public']; $this->debug = $config['debug']; date_default_timezone_set($config['timezone']); $this->slim = new \Slim\Slim($config['slim_args']); $this->twig_config = $config['twig_args']; $this->smtp_config = $config['smtp_args']; $this->files =& $_FILES; $this->get = $this->slim->request()->get(); $this->post = $this->slim->request()->post(); if ($config['session_args']['type'] === 'redis') { ini_set('session.save_handler', 'redis'); ini_set('session.timeout', 18000); // each client should remember their session id for EXACTLY 1 hour session_set_cookie_params(18000); ini_set('session.save_path', 'tcp://' . $config['session_args']['host'] . ':' . $config['session_args']['port']); } session_name($config['session_args']['name']); session_start(); $this->session =& $_SESSION; $this->remote_addr = $this->slim->request->getIp(); if (!isset($this->session['notifications'])) { $this->session['notifications'] = []; } //sometimes you might need to set additional args. you could allow middleware to add args here, or virtually anything else you want $base_url = $config['scheme'] . $_SERVER['SERVER_NAME']; $extra_template_args = ['csrf' => $this->csrf(), 'base_url' => $base_url . '/', 'year' => date('Y'), 'styles' => '', 'scripts' => '', 'styles_external' => '', 'scripts_external' => '', 'user' => $this->is_user() ? $this->session['user'] : false, 'admin' => $this->is_admin() ? $this->session['admin'] : false, 'url' => $this->getUrl($base_url), 'canonical' => $base_url . $this->slim->request()->getResourceUri()]; $this->args = array_merge($config['template_args'], $extra_template_args); //todo make these all lazy load for performance. $this->cache = \StarterKit\Cache::getInstance($config['cache_args']); $this->db = \StarterKit\DB::getInstance($config['db_args']); $this->filter = \RedBeanFVM\RedBeanFVM::getInstance(); $this->smtp = \StarterKit\Email::getInstance($config['smtp_args']); self::$instance =& $this; }
<?php //provides a database connection for cron jobs. require 'config.php'; require 'vendor/autoload.php'; require 'StarterKit/App.php'; \StarterKit\App::registerAutoloader(); $db = \StarterKit\DB::getInstance($config['db_args']); $cache = \StarterKit\Cache::getInstance($config['cache_args']);
public function getAnalytics($k = false) { require LIB_PATH . 'analytics/GAPI.php'; $cache = \StarterKit\Cache::getInstance(); $key = 'admin:analytics'; $data = $cache->get($key); if ($data === -1) { $ga = new \GoogleAnalyticsAPI('service'); $ga->auth->setClientId('444232255351-jp55p2lohhfu4oao0bpm0lggo00sotuc.apps.googleusercontent.com'); // From the APIs console $ga->auth->setEmail('*****@*****.**'); // From the APIs console $ga->auth->setPrivateKey(LIB_PATH . 'analytics/secret.p12'); $auth = $ga->auth->getAccessToken(); if ($auth['http_code'] != 200) { throw new \exception('Fail to connect'); } $token = $auth['access_token']; $expires = $auth['expires_in']; $created = time(); $ga->setAccessToken($token); $ga->setAccountId('ga:109638630'); //set some common dates $now = date('Y-m-d', strtotime(date('Y-m-d') . '-1 day')); // $dates represents the start date parameter for each timespan. no need to specify end date, we already have it in $now $dates = ['year' => date("Y-m-d", strtotime('first day of January ' . date('Y'))), 'month' => date('Y-m-01', strtotime('this month')), 'day' => date('Y-m-d', strtotime($now . ' -1 day'))]; $data = []; //device $data['devices'] = $ga->getVisitsBySystemOs(['max-results' => 100])['rows']; //visits by location $data['locations'] = $ga->query(['metrics' => 'ga:visits', 'dimensions' => 'ga:country', 'sort' => '-ga:visits', 'max-results' => 50, 'start-date' => $dates['year']])['rows']; //new vs returning $data['new_vs_returning'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:userType'])['rows']; //visits by city $data['visits_by_city'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:city', 'max-results' => 10])['rows']; //map view $data['map_view'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:city,ga:Latitude,ga:Longitude'])['rows']; //visitors by browser $data['browser'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:browser'])['rows']; //visitors by screensize $data['screen_sizes'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:screenResolution'])['rows']; //visits by service provider $data['isp'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:networkDomain', 'max-results' => 50])['rows']; //most popular pages $data['pages'] = $ga->query(['metrics' => 'ga:pageViews', 'dimensions' => 'ga:pagePath', 'max-results' => 10])['rows']; $data['hits_by_day'] = $ga->query(['metrics' => 'ga:pageviews,ga:visitors', 'dimensions' => 'ga:date', 'start-date' => $dates['month'], 'end-date' => $now])['rows']; $data['hits_by_country'] = $ga->query(['metrics' => 'ga:visitors', 'dimensions' => 'ga:country', 'start-date' => $dates['month'], 'end-date' => $now])['rows']; $data['hits_by_city'] = $ga->query(['metrics' => 'ga:visitors', 'dimensions' => 'ga:city', 'start-date' => $dates['month'], 'end-date' => $now])['rows']; $data['this_month'] = ['name' => date('M'), 'data' => $ga->query(['metrics' => 'ga:visits', 'dimensions' => 'ga:date', 'start-date' => $dates['month'], 'end-date' => $now])['rows']]; $data['this_year'] = ['name' => date('Y'), 'data' => $ga->query(['metrics' => 'ga:visits', 'dimensions' => 'ga:date', 'start-date' => $dates['year'], 'end-date' => $now])['rows']]; $data['months_in_year'] = $ga->query(['metrics' => 'ga:pageviews', 'dimensions' => 'ga:month', 'sort' => 'ga:month', 'start-date' => $dates['year'], 'end-date' => $now])['rows']; $data['social'] = $ga->query(['metrics' => 'ga:socialActivities', 'dimensions' => 'ga:week', 'sort' => 'ga:week', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 25])['rows']; $data['referrals'] = $ga->query(['metrics' => 'ga:users', 'dimensions' => 'ga:referralPath', 'sort' => 'ga:referralPath', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 20])['rows']; $data['device_type'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:deviceCategory', 'sort' => 'ga:deviceCategory', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 20])['rows']; $data['mobile_devices'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:mobileDeviceInfo', 'sort' => 'ga:mobileDeviceInfo', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 20])['rows']; try { $data['search_terms'] = $ga->query(['metrics' => 'ga:searchResultViews', 'dimensions' => 'ga:searchKeyword', 'sort' => 'ga:searchKeyword', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 50])['rows']; } catch (\exception $e) { $data['search_terms'] = [['None Yet', 0]]; } $data['last_seven'] = $ga->query(['metrics' => 'ga:pageviews', 'dimensions' => 'ga:dayOfWeek', 'sort' => 'ga:dayOfWeek', 'start-date' => '8daysAgo', 'end-date' => 'yesterday', 'max-results' => 50])['rows']; if (!empty($data) && $data !== false && $data !== 0 && !is_null($data)) { $cache->set($key, $data, 60 * 60); //never set empty arrays to cache cache! } } if ($k) { if (isset($data[$k])) { return $data[$k]; } } return $data; }