Beispiel #1
0
/**
 * Checks if the WordPress API is a valid method for selecting an account
 *
 * @return a list of accounts if available, false if none available
 **/
function ga_get_analytics_accounts()
{
    $accounts = array();
    # Get the class for interacting with the Google Analytics
    require_once 'class.analytics.stats.php';
    # Create a new Gdata call
    if (isset($_POST['token']) && $_POST['token'] != '') {
        $stats = new GoogleAnalyticsStats($_POST['token']);
    } elseif (trim(get_option('ga_google_token')) != '') {
        $stats = new GoogleAnalyticsStats();
    } else {
        return false;
    }
    # Check if Google sucessfully logged in
    if (!$stats->checkLogin()) {
        return false;
    }
    # Get a list of accounts
    $accounts = $stats->getAnalyticsAccounts();
    # Return the account array if there are accounts
    if (count($accounts) > 0) {
        return $accounts;
    } else {
        return false;
    }
}
 function form($instance)
 {
     //Defaults
     $instance = nxt_parse_args((array) $instance, array('title' => '', 'account' => '', 'timeFrame' => '1', 'pageBg' => 'fff', 'widgetBg' => '999', 'innerBg' => 'fff', 'font' => '333', 'line1' => 'Unique', 'line2' => 'Visitors'));
     $title = htmlspecialchars($instance['title']);
     $acnt = htmlspecialchars($instance['account']);
     $timeFrame = htmlspecialchars($instance['timeFrame']);
     $pageBg = htmlspecialchars($instance['pageBg']);
     $widgetBg = htmlspecialchars($instance['widgetBg']);
     $innerBg = htmlspecialchars($instance['innerBg']);
     $font = htmlspecialchars($instance['font']);
     $line1 = htmlspecialchars($instance['line1']);
     $line2 = htmlspecialchars($instance['line2']);
     $accounts = array();
     # 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();
     # Check if Google sucessfully logged in
     $login = $stats->checkLogin();
     # Get a list of accounts
     $accounts = $stats->getAnalyticsAccounts();
     # Output the options
     echo '<p style="text-align:right;"><label for="' . $this->get_field_name('title') . '">' . __('Title', 'google-analyticator') . ': <input style="width: 250px;" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></label></p>';
     # The list of accounts
     echo '<p style="text-align:right;"><label for="' . $this->get_field_name('account') . '">' . __('Analytics account', 'google-analyticator') . ': ';
     echo '<select name="' . $this->get_field_name('account') . '" id="' . $this->get_field_id('account') . '" style="margin-top: -3px; margin-bottom: 10px;">';
     if (count($accounts) > 0) {
         foreach ($accounts as $account) {
             $select = $acnt == $account['id'] ? ' selected="selected"' : '';
             echo '<option value="' . $account['id'] . '"' . $select . '>' . $account['title'] . '</option>';
         }
     } elseif ($login == false) {
         echo '<option value="">' . __('Authenticate on settings page.', 'google-analyticator') . '</option>';
     } else {
         echo '<option value="">' . __('No Analytics accounts available.', 'google-analyticator') . '</option>';
     }
     echo '</select></label></p>';
     # Time frame
     echo '<p style="text-align:right;"><label for="' . $this->get_field_name('timeFrame') . '">' . __('Days of data to get', 'google-analyticator') . ': <input style="width: 150px;" id="' . $this->get_field_id('timeFrame') . '" name="' . $this->get_field_name('timeFrame') . '" type="text" value="' . $timeFrame . '" /></label></p>';
     # Page background
     echo '<p style="text-align:right;"><label for="' . $this->get_field_name('pageBg') . '">' . __('Page background', 'google-analyticator') . ': <input style="width: 150px;" id="' . $this->get_field_id('pageBg') . '" name="' . $this->get_field_name('pageBg') . '" type="text" value="' . $pageBg . '" /></label></p>';
     # Widget background
     echo '<p style="text-align:right;"><label for="' . $this->get_field_name('widgetBg') . '">' . __('Widget background', 'google-analyticator') . ': <input style="width: 150px;" id="' . $this->get_field_id('widgetBg') . '" name="' . $this->get_field_name('widgetBg') . '" type="text" value="' . $widgetBg . '" /></label></p>';
     # Inner background
     echo '<p style="text-align:right;"><label for="' . $this->get_field_name('innerBg') . '">' . __('Inner background', 'google-analyticator') . ': <input style="width: 150px;" id="' . $this->get_field_id('innerBg') . '" name="' . $this->get_field_name('innerBg') . '" type="text" value="' . $innerBg . '" /></label></p>';
     # Font color
     echo '<p style="text-align:right;"><label for="' . $this->get_field_name('font') . '">' . __('Font color', 'google-analyticator') . ': <input style="width: 150px;" id="' . $this->get_field_id('font') . '" name="' . $this->get_field_name('font') . '" type="text" value="' . $font . '" /></label></p>';
     # Text line 1
     echo '<p style="text-align:right;"><label for="' . $this->get_field_name('line1') . '">' . __('Line 1 text', 'google-analyticator') . ': <input style="width: 200px;" id="' . $this->get_field_id('line1') . '" name="' . $this->get_field_name('line1') . '" type="text" value="' . $line1 . '" /></label></p>';
     # Text line 2
     echo '<p style="text-align:right;"><label for="' . $this->get_field_name('line2') . '">' . __('Line 2 text', 'google-analyticator') . ': <input style="width: 200px;" id="' . $this->get_field_id('line2') . '" name="' . $this->get_field_name('line2') . '" type="text" value="' . $line2 . '" /></label></p>';
 }
Beispiel #3
0
/**
 * Checks if the WordPress API is a valid method for selecting an account
 *
 * @return a list of accounts if available, false if none available
 **/
function ga_get_analytics_accounts()
{
    $accounts = array();
    # Create a new Gdata call
    if (isset($_POST['token']) && $_POST['token'] != '') {
        $stats = new GoogleAnalyticsStats($_POST['token']);
    } elseif (trim(get_option('ga_google_token')) != '') {
        $stats = new GoogleAnalyticsStats();
    } else {
        return false;
    }
    # Check if Google sucessfully logged in
    if (!$stats->checkLogin()) {
        return false;
    }
    # Get a list of accounts
    $accounts = $stats->getAllProfiles();
    natcasesort($accounts);
    # Return the account array if there are accounts
    if (count($accounts) > 0) {
        return $accounts;
    } else {
        return false;
    }
}
 /**
  * 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;
 }