/** * 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) { # Get the value from the database $visits = maybe_unserialize(get_option('google_stats_visits_' . $account)); # Check to make sure the timeframe is an int and greater than one $time = (int) $time; if ($time < 1) { $time = 1; } # Check if the call has been made before if (is_array($visits)) { # Check if the last time called was within two hours, if so, return that if ($visits['lastcalled'] > time() - 7200) { return $visits['unique']; } } # If here, the call has not been made or it is expired # Get the current memory limit $current_mem_limit = substr(ini_get('memory_limit'), 0, -1); # Check if this limit is less than 96M, if so, increase it if ($current_mem_limit < 96 || $current_mem_limit == '') { if (function_exists('memory_get_usage')) { @ini_set('memory_limit', '96M'); } } # Get the class for interacting with the Google Analytics require_once 'class.analytics.stats.php'; # Create a new Gdata call $stats = new GoogleAnalyticsStats(); # 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')); $uniques = number_format($stats->getMetric('ga:visitors', $before, $yesterday)); # Store the array update_option('google_stats_visits_' . $account, array('unique' => $uniques, 'lastcalled' => time())); # Return the visits return $uniques; }
/** * 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; }