// construct the class $oAnalytics = new analytics('tderouin', 'rem700sps'); // set it up to use caching $oAnalytics->useCache(); #$oAnalytics->setProfileByName('[Google analytics accountname]'); $oAnalytics->setProfileById('ga:16643416'); // set the date range $oAnalytics->setMonth(date('n'), date('Y')); // or $oAnalytics->setDateRange('YYYY-MM-DD', 'YYYY-MM-DD'); // print out visitors for given period #print_r($oAnalytics->getVisitors()); // print out pageviews for given period #print_r($oAnalytics->getPageviews()); // use dimensions and metrics for output // see: http://code.google.com/intl/nl/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html $results = $oAnalytics->getData(array('dimensions' => 'ga:keyword', 'metrics' => 'ga:visits', 'sort' => '-ga:visits')); $skip = array("(other)", "(not set)", "wikihow", "wiki how"); array_shift($results); foreach ($results as $r => $c) { $r = trim(preg_replace("@^how to@im", "", $r)); if (in_array($r, $skip)) { continue; } $l = new LSearch(); #$results = $l->googleSearchResultTitles($r); #$results = gSearch::query($r); $result = scrapeGoogle($r); if (empty($result)) { echo "{$r}\t(no matches)\n"; continue; }
<?php // session_start for caching session_start(); require 'analytics.class.php'; try { // construct the class $oAnalytics = new analytics('[username]', '[password]'); // set it up to use caching $oAnalytics->useCache(); $oAnalytics->setProfileByName('[Google analytics accountname]'); // or $oAnalytics->setProfileById('ga:123456'); // set the date range $oAnalytics->setMonth(date('n'), date('Y')); // or $oAnalytics->setDateRange('YYYY-MM-DD', 'YYYY-MM-DD'); echo '<pre>'; // print out visitors for given period print_r($oAnalytics->getVisitors()); // print out pageviews for given period print_r($oAnalytics->getPageviews()); // use dimensions and metrics for output // see: http://code.google.com/intl/nl/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html print_r($oAnalytics->getData(array('dimensions' => 'ga:keyword', 'metrics' => 'ga:visits', 'sort' => 'ga:keyword'))); } catch (Exception $e) { echo 'Caught exception: ' . $e->getMessage(); }
$message .= "</ul>"; print array2json(array("status" => 403, "message" => $message)); exit; } $yesterday = date("Y-m-d", time() - 1 * 24 * 60 * 60); // set the date range between yesterday and before 1 mmonth $oAnalytics->setDateRange(date("Y-m-d", strtotime("-1 month", time() - 1 * 24 * 60 * 60)), $yesterday); // Select metrics foreach ($_POST['data'] as $items) { $metricsArray[] = $items['metrics']; } // get the datas by metrics foreach ($metricsArray as $metrics) { $dateStore = null; $valueStore = null; // from google $response = $oAnalytics->getData(array('dimensions' => 'ga:date', 'metrics' => $metrics, 'sort' => 'ga:date')); $dateFormat = $_POST['dateFormat']; // set the date format foreach ($response as $date => $value) { $dateStore[] = date($dateFormat, strtotime($date)); $valueStore[] = $value; } $values[] = $valueStore; } // return a json with datas print array2json(array("status" => 200, "data" => array("dates" => $dateStore, "values" => $values))); } catch (Exception $e) { // return errors print array2json(array("status" => 400, "message" => $e->getMessage())); }