Ejemplo n.º 1
0
/**
 * EXPERIMENTAL: Retrieve Google's visits for the given page
 * More work needs to be done. Needs caching, needs to be less resource intensive, and
 * needs an automated way to determine the page.
 * Function may/will change in future releases. Only use if you know what you're doing.
 *
 * @param url - the page url, missing the domain information
 * @param days - the number of days to get
 * @return the number of visits
 **/
function get_analytics_visits_by_page($page, $days = 31)
{
    require_once 'class.analytics.stats.php';
    # Create a new API object
    $api = new GoogleAnalyticsStats();
    # Get the current accounts accounts
    $accounts = ga_get_analytics_accounts();
    # Verify accounts exist
    if (count($accounts) <= 0) {
        return 0;
    }
    # Loop throught the account and return the current account
    foreach ($accounts as $account) {
        # Check if the UID matches the selected UID
        if ($account['ga:webPropertyId'] == get_option('ga_uid')) {
            $api->setAccount($account['id']);
            break;
        }
    }
    # Encode the page url
    $page = urlencode($page);
    # Get the metric information from Google
    $before = date('Y-m-d', strtotime('-' . $days . ' days'));
    $yesterday = date('Y-m-d', strtotime('-1 day'));
    $stats = $api->getMetrics('ga:visits', $before, $yesterday, 'ga:pagePath', false, 'ga:pagePath%3D%3D' . $page, 1);
    # Check the size of the stats array
    if (count($stats) <= 0 || !is_array($stats)) {
        return 0;
    } else {
        # Loop through each stat for display
        foreach ($stats as $stat) {
            return $stat['ga:visits'];
        }
    }
}
 /**
  * Grabs the cached value of the unique visits for the previous day
  *
  * @param account - the account to get the unique visitors from
  * @param time - the amount of days to get
  * @return void
  **/
 function getUniqueVisitors($account, $time = 1)
 {
     // IF we have a cached version, return that, if not, continue on.
     if (get_transient('google_stats_uniques')) {
         return get_transient('google_stats_uniques');
     }
     # Get the class for interacting with the Google Analytics
     require_once 'class.analytics.stats.php';
     # Create a new Gdata call
     $stats = new GoogleAnalyticsStats();
     # Check if Google sucessfully logged in
     if (!$stats->checkLogin()) {
         return false;
     }
     $account = $stats->getSingleProfile();
     $account = $account[0]['id'];
     # Set the account to the one requested
     $stats->setAccount($account);
     # Get the latest stats
     $before = date('Y-m-d', strtotime('-' . $time . ' days'));
     $yesterday = date('Y-m-d', strtotime('-1 day'));
     try {
         $result = $stats->getMetrics('ga:visitors', $before, $yesterday);
     } catch (Exception $e) {
         print 'GA Widget - there was a service error ' . $e->getCode() . ':' . $e->getMessage();
     }
     $uniques = number_format($result->totalsForAllResults['ga:visitors']);
     # Store the array
     set_transient('google_stats_uniques', $uniques, 60 * 60 * 12);
     # Return the visits
     return $uniques;
 }