function ga_dash_new_return($service, $projectId, $from, $to) { $metrics = 'ga:visits'; $dimensions = 'ga:visitorType'; try { $serial = 'gadash_qr9' . str_replace(array('ga:', ',', '-', date('Y')), "", $projectId . $from . $to); $transient = get_transient($serial); if (empty($transient)) { $data = $service->data_ga->get('ga:' . $projectId, $from, $to, $metrics, array('dimensions' => $dimensions)); set_transient($serial, $data, get_option('ga_dash_cachetime')); } else { $data = $transient; } } catch (Google_ServiceException $e) { echo ga_dash_pretty_error($e); return; } if (!isset($data['rows'])) { return 0; } $ga_dash_data = ""; for ($i = 0; $i < $data['totalResults']; $i++) { $ga_dash_data .= "['" . str_replace(array("'", "\\"), " ", $data['rows'][$i][0]) . "'," . $data['rows'][$i][1] . "],"; } return rtrim($ga_dash_data, ','); }
function ga_dash_content() { require_once 'functions.php'; if (!get_option('ga_dash_cachetime') or get_option('ga_dash_cachetime') == 10) { update_option('ga_dash_cachetime', "900"); } if (!class_exists('Google_Exception')) { require_once 'src/Google_Client.php'; } require_once 'src/contrib/Google_AnalyticsService.php'; //$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF']; $client = new Google_Client(); $client->setAccessType('offline'); $client->setApplicationName('Google Analytics Dashboard'); $client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob'); if (get_option('ga_dash_userapi')) { $client->setClientId(get_option('ga_dash_clientid')); $client->setClientSecret(get_option('ga_dash_clientsecret')); $client->setDeveloperKey(get_option('ga_dash_apikey')); } else { $client->setClientId('65556128781.apps.googleusercontent.com'); $client->setClientSecret('Kc7888wgbc_JbeCpbFjnYpwE'); $client->setDeveloperKey('AIzaSyBG7LlUoHc29ZeC_dsShVaBEX15SfRl_WY'); } $service = new Google_AnalyticsService($client); if (ga_dash_get_token()) { $token = ga_dash_get_token(); $client->setAccessToken($token); } if (!$client->getAccessToken()) { $authUrl = $client->createAuthUrl(); if (!isset($_REQUEST['ga_dash_authorize'])) { if (!current_user_can('manage_options')) { _e("Ask an admin to authorize this Application", 'ga-dash'); return; } echo '<div style="padding:20px;">' . __("Use this link to get your access code:", 'ga-dash') . ' <a href="' . $authUrl . '" target="_blank">' . __("Get Access Code", 'ga-dash') . '</a>'; echo '<form name="input" action="#" method="get"> <p><b>' . __("Access Code:", 'ga-dash') . ' </b><input type="text" name="ga_dash_code" value="" size="61"></p> <input type="submit" class="button button-primary" name="ga_dash_authorize" value="' . __("Save Access Code", 'ga-dash') . '"/> </form> </div>'; return; } else { if ($_REQUEST['ga_dash_code']) { $client->authenticate($_REQUEST['ga_dash_code']); ga_dash_store_token($client->getAccessToken()); } else { $adminurl = admin_url("#ga-dash-widget"); echo '<script> window.location="' . $adminurl . '"; </script> '; } } } if (current_user_can('manage_options')) { if (isset($_REQUEST['ga_dash_profiles'])) { update_option('ga_dash_tableid', $_REQUEST['ga_dash_profiles']); } try { $client->setUseObjects(true); $profile_switch = ""; $serial = 'gadash_qr1'; $transient = get_transient($serial); if (empty($transient)) { $profiles = $service->management_profiles->listManagementProfiles('~all', '~all'); set_transient($serial, $profiles, 60 * 60 * 24); } else { $profiles = $transient; } //print_r($profiles); $items = $profiles->getItems(); $profile_switch .= '<form><select id="ga_dash_profiles" name="ga_dash_profiles" onchange="this.form.submit()">'; if (count($items) != 0) { $ga_dash_profile_list = ""; foreach ($items as &$profile) { if (!get_option('ga_dash_tableid')) { update_option('ga_dash_tableid', $profile->getId()); } $profile_switch .= '<option value="' . $profile->getId() . '"'; if (get_option('ga_dash_tableid') == $profile->getId()) { $profile_switch .= "selected='yes'"; } $profile_switch .= '>' . ga_dash_get_profile_domain($profile->getwebsiteUrl()) . '</option>'; $ga_dash_profile_list[] = array($profile->getName(), $profile->getId(), $profile->getwebPropertyId(), $profile->getwebsiteUrl()); } update_option('ga_dash_profile_list', $ga_dash_profile_list); } $profile_switch .= "</select></form><br />"; $client->setUseObjects(false); } catch (Google_ServiceException $e) { echo ga_dash_pretty_error($e); return; } } if (current_user_can('manage_options')) { if (get_option('ga_dash_jailadmins')) { if (get_option('ga_dash_tableid_jail')) { $projectId = get_option('ga_dash_tableid_jail'); } else { _e("Ask an admin to asign a Google Analytics Profile", 'ga-dash'); return; } } else { echo $profile_switch; $projectId = get_option('ga_dash_tableid'); } } else { if (get_option('ga_dash_tableid_jail')) { $projectId = get_option('ga_dash_tableid_jail'); } else { _e("Ask an admin to asign a Google Analytics Profile", 'ga-dash'); return; } } if (isset($_REQUEST['query'])) { $query = $_REQUEST['query']; } else { $query = "visits"; } if (isset($_REQUEST['period'])) { $period = $_REQUEST['period']; } else { $period = "last30days"; } switch ($period) { case 'today': $from = date('Y-m-d'); $to = date('Y-m-d'); break; case 'yesterday': $from = date('Y-m-d', time() - 24 * 60 * 60); $to = date('Y-m-d', time() - 24 * 60 * 60); break; case 'last7days': $from = date('Y-m-d', time() - 7 * 24 * 60 * 60); $to = date('Y-m-d'); break; case 'last14days': $from = date('Y-m-d', time() - 14 * 24 * 60 * 60); $to = date('Y-m-d'); break; default: $from = date('Y-m-d', time() - 30 * 24 * 60 * 60); $to = date('Y-m-d'); break; } switch ($query) { case 'visitors': $title = __("Visitors", 'ga-dash'); break; case 'pageviews': $title = __("Page Views", 'ga-dash'); break; case 'visitBounceRate': $title = __("Bounce Rate", 'ga-dash'); break; case 'organicSearches': $title = __("Organic Searches", 'ga-dash'); break; default: $title = __("Visits", 'ga-dash'); } $metrics = 'ga:' . $query; $dimensions = 'ga:year,ga:month,ga:day'; try { $serial = 'gadash_qr2' . str_replace(array('ga:', ',', '-', date('Y')), "", $projectId . $from . $to . $metrics); $transient = get_transient($serial); if (empty($transient)) { $data = $service->data_ga->get('ga:' . $projectId, $from, $to, $metrics, array('dimensions' => $dimensions)); set_transient($serial, $data, get_option('ga_dash_cachetime')); } else { $data = $transient; } } catch (Google_ServiceException $e) { echo ga_dash_pretty_error($e); return; } $ga_dash_statsdata = ""; for ($i = 0; $i < $data['totalResults']; $i++) { $ga_dash_statsdata .= "['" . $data['rows'][$i][0] . "-" . $data['rows'][$i][1] . "-" . $data['rows'][$i][2] . "'," . round($data['rows'][$i][3], 2) . "],"; } $ga_dash_statsdata = rtrim($ga_dash_statsdata, ','); $metrics = 'ga:visits,ga:visitors,ga:pageviews,ga:visitBounceRate,ga:organicSearches,ga:timeOnSite'; $dimensions = 'ga:year'; try { $serial = 'gadash_qr3' . str_replace(array('ga:', ',', '-', date('Y')), "", $projectId . $from . $to); $transient = get_transient($serial); if (empty($transient)) { $data = $service->data_ga->get('ga:' . $projectId, $from, $to, $metrics, array('dimensions' => $dimensions)); set_transient($serial, $data, get_option('ga_dash_cachetime')); } else { $data = $transient; } } catch (Google_ServiceException $e) { echo ga_dash_pretty_error($e); return; } if (get_option('ga_dash_style') == "light") { $css = "colors:['gray','darkgray'],"; $colors = "black"; } else { $css = ""; $colors = "blue"; } $code = '<script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("visualization", "1", {packages:["corechart"]}); google.setOnLoadCallback(ga_dash_callback); function ga_dash_callback(){ ga_dash_drawstats(); if(typeof ga_dash_drawmap == "function"){ ga_dash_drawmap(); } if(typeof ga_dash_drawpgd == "function"){ ga_dash_drawpgd(); } if(typeof ga_dash_drawrd == "function"){ ga_dash_drawrd(); } if(typeof ga_dash_drawsd == "function"){ ga_dash_drawsd(); } if(typeof ga_dash_drawtraffic == "function"){ ga_dash_drawtraffic(); } } function ga_dash_drawstats() { var data = google.visualization.arrayToDataTable([' . "\n ['" . __("Date", 'ga-dash') . "', '" . $title . "']," . $ga_dash_statsdata . " \n ]);\n\n var options = {\n\t\t legend: {position: 'none'},\t\n\t\t pointSize: 3," . $css . "\n title: '" . $title . "',\n\t\t chartArea: {width: '85%'},\n hAxis: { title: '" . __("Date", 'ga-dash') . "', titleTextStyle: {color: '" . $colors . "'}, showTextEvery: 5}\n\t\t};\n\n var chart = new google.visualization.AreaChart(document.getElementById('ga_dash_statsdata'));\n\t\tchart.draw(data, options);\n\t\t\n }"; if (get_option('ga_dash_map')) { $ga_dash_visits_country = ga_dash_visits_country($service, $projectId, $from, $to); if ($ga_dash_visits_country) { $code .= ' google.load("visualization", "1", {packages:["geochart"]}) function ga_dash_drawmap() { var data = google.visualization.arrayToDataTable([' . "\n\t\t\t ['" . __("Country", 'ga-dash') . "', '" . __("Visits", 'ga-dash') . "']," . $ga_dash_visits_country . " \n\t\t\t]);\n\t\t\t\n\t\t\tvar options = {\n\t\t\t\tcolors: ['white', '" . $colors . "']\n\t\t\t};\n\t\t\t\n\t\t\tvar chart = new google.visualization.GeoChart(document.getElementById('ga_dash_mapdata'));\n\t\t\tchart.draw(data, options);\n\t\t\t\n\t\t }"; } } if (get_option('ga_dash_traffic')) { $ga_dash_traffic_sources = ga_dash_traffic_sources($service, $projectId, $from, $to); $ga_dash_new_return = ga_dash_new_return($service, $projectId, $from, $to); if ($ga_dash_traffic_sources and $ga_dash_new_return) { $code .= ' google.load("visualization", "1", {packages:["corechart"]}) function ga_dash_drawtraffic() { var data = google.visualization.arrayToDataTable([' . "\n\t\t\t ['" . __("Source", 'ga-dash') . "', '" . __("Visits", 'ga-dash') . "']," . $ga_dash_traffic_sources . ' ]); var datanvr = google.visualization.arrayToDataTable([' . "\n\t\t\t ['" . __("Type", 'ga-dash') . "', '" . __("Visits", 'ga-dash') . "']," . $ga_dash_new_return . " \n\t\t\t]);\n\t\t\t\n\t\t\tvar chart = new google.visualization.PieChart(document.getElementById('ga_dash_trafficdata'));\n\t\t\tchart.draw(data, {\n\t\t\t\tis3D: true,\n\t\t\t\ttooltipText: 'percentage',\n\t\t\t\tlegend: 'none',\n\t\t\t\ttitle: '" . __("Traffic Sources", 'ga-dash') . "'\n\t\t\t});\n\t\t\t\n\t\t\tvar chart1 = new google.visualization.PieChart(document.getElementById('ga_dash_nvrdata'));\n\t\t\tchart1.draw(datanvr, {\n\t\t\t\tis3D: true,\n\t\t\t\ttooltipText: 'percentage',\n\t\t\t\tlegend: 'none',\n\t\t\t\ttitle: '" . __("New vs. Returning", 'ga-dash') . "'\n\t\t\t});\n\t\t\t\n\t\t }"; } } if (get_option('ga_dash_pgd')) { $ga_dash_top_pages = ga_dash_top_pages($service, $projectId, $from, $to); if ($ga_dash_top_pages) { $code .= ' google.load("visualization", "1", {packages:["table"]}) function ga_dash_drawpgd() { var data = google.visualization.arrayToDataTable([' . "\n\t\t\t ['" . __("Top Pages", 'ga-dash') . "', '" . __("Visits", 'ga-dash') . "']," . $ga_dash_top_pages . " \n\t\t\t]);\n\t\t\t\n\t\t\tvar options = {\n\t\t\t\tpage: 'enable',\n\t\t\t\tpageSize: 6,\n\t\t\t\twidth: '100%'\n\t\t\t}; \n\t\t\t\n\t\t\tvar chart = new google.visualization.Table(document.getElementById('ga_dash_pgddata'));\n\t\t\tchart.draw(data, options);\n\t\t\t\n\t\t }"; } } if (get_option('ga_dash_rd')) { $ga_dash_top_referrers = ga_dash_top_referrers($service, $projectId, $from, $to); if ($ga_dash_top_referrers) { $code .= ' google.load("visualization", "1", {packages:["table"]}) function ga_dash_drawrd() { var datar = google.visualization.arrayToDataTable([' . "\n\t\t\t ['" . __("Top Referrers", 'ga-dash') . "', '" . __("Visits", 'ga-dash') . "']," . $ga_dash_top_referrers . " \n\t\t\t]);\n\t\t\t\n\t\t\tvar options = {\n\t\t\t\tpage: 'enable',\n\t\t\t\tpageSize: 6,\n\t\t\t\twidth: '100%'\n\t\t\t}; \n\t\t\t\n\t\t\tvar chart = new google.visualization.Table(document.getElementById('ga_dash_rdata'));\n\t\t\tchart.draw(datar, options);\n\t\t\t\n\t\t }"; } } if (get_option('ga_dash_sd')) { $ga_dash_top_searches = ga_dash_top_searches($service, $projectId, $from, $to); if ($ga_dash_top_searches) { $code .= ' google.load("visualization", "1", {packages:["table"]}) function ga_dash_drawsd() { var datas = google.visualization.arrayToDataTable([' . "\n\t\t\t ['" . __("Top Searches", 'ga-dash') . "', '" . __("Visits", 'ga-dash') . "']," . $ga_dash_top_searches . " \n\t\t\t]);\n\t\t\t\n\t\t\tvar options = {\n\t\t\t\tpage: 'enable',\n\t\t\t\tpageSize: 6,\n\t\t\t\twidth: '100%'\n\t\t\t}; \n\t\t\t\n\t\t\tvar chart = new google.visualization.Table(document.getElementById('ga_dash_sdata'));\n\t\t\tchart.draw(datas, options);\n\t\t\t\n\t\t }"; } } $code .= "</script>"; $code .= "</script>"; $ga_button_style = get_option('ga_dash_style') == 'light' ? 'button' : 'gabutton'; $code .= '<div id="ga-dash"> <center> <div id="buttons_div"> <input class="' . $ga_button_style . '" type="button" value="' . __("Today", 'ga-dash') . '" onClick="window.location=\'?period=today&query=' . $query . '\'" /> <input class="' . $ga_button_style . '" type="button" value="' . __("Yesterday", 'ga-dash') . '" onClick="window.location=\'?period=yesterday&query=' . $query . '\'" /> <input class="' . $ga_button_style . '" type="button" value="' . __("Last 7 days", 'ga-dash') . '" onClick="window.location=\'?period=last7days&query=' . $query . '\'" /> <input class="' . $ga_button_style . '" type="button" value="' . __("Last 14 days", 'ga-dash') . '" onClick="window.location=\'?period=last14days&query=' . $query . '\'" /> <input class="' . $ga_button_style . '" type="button" value="' . __("Last 30 days", 'ga-dash') . '" onClick="window.location=\'?period=last30days&query=' . $query . '\'" /> </div> <div id="ga_dash_statsdata"></div> <div id="details_div"> <table class="gatable" cellpadding="4"> <tr> <td width="24%">' . __("Visits:", 'ga-dash') . '</td> <td width="12%" class="gavalue"><a href="?query=visits&period=' . $period . '" class="gatable">' . $data['rows'][0][1] . '</td> <td width="24%">' . __("Visitors:", 'ga-dash') . '</td> <td width="12%" class="gavalue"><a href="?query=visitors&period=' . $period . '" class="gatable">' . $data['rows'][0][2] . '</a></td> <td width="24%">' . __("Page Views:", 'ga-dash') . '</td> <td width="12%" class="gavalue"><a href="?query=pageviews&period=' . $period . '" class="gatable">' . $data['rows'][0][3] . '</a></td> </tr> <tr> <td>' . __("Bounce Rate:", 'ga-dash') . '</td> <td class="gavalue"><a href="?query=visitBounceRate&period=' . $period . '" class="gatable">' . round($data['rows'][0][4], 2) . '%</a></td> <td>' . __("Organic Search:", 'ga-dash') . '</td> <td class="gavalue"><a href="?query=organicSearches&period=' . $period . '" class="gatable">' . $data['rows'][0][5] . '</a></td> <td>' . __("Pages per Visit:", 'ga-dash') . '</td> <td class="gavalue"><a href="#" class="gatable">' . ($data['rows'][0][1] ? round($data['rows'][0][3] / $data['rows'][0][1], 2) : '0') . '</a></td> </tr> </table> </div>'; if (get_option('ga_dash_map')) { $code .= '<br /><h3>' . __("Visits by Country", 'ga-dash') . '</h3> <div id="ga_dash_mapdata"></div>'; } if (get_option('ga_dash_traffic')) { $code .= '<br /><h3>' . __("Traffic Overview", 'ga-dash') . '</h3> <table width="100%"><tr><td width="50%"><div id="ga_dash_trafficdata"></div></td><td width="50%"><div id="ga_dash_nvrdata"></div></td></tr></table>'; } $code .= '</center> </div>'; if (get_option('ga_dash_pgd')) { $code .= '<div id="ga_dash_pgddata"></div>'; } if (get_option('ga_dash_rd')) { $code .= '<div id="ga_dash_rdata"></div>'; } if (get_option('ga_dash_sd')) { $code .= '<div id="ga_dash_sdata"></div>'; } echo $code; }