' . __('Loading', 'google-analyticator') . '...'; echo ''; } /** * AJAX data for the widget **/ function ajaxWidget() { # Check the ajax widget check_ajax_referer('google-analyticator-statsWidget_get'); # Attempt to login and get the current account $account = $this->getAnalyticsAccount(); $profile_id = get_option('ga_profileid'); if ( trim($profile_id) != '' ) { if ( substr($profile_id, 0, 3) == 'ga:' ) { $this->id = $profile_id; } else { $this->id = 'ga:' . $profile_id; } } else { $this->id = $account; } $this->api->setAccount($this->id); # Check that we can display the widget before continuing if ( $account == false || $this->id == false ) { # Output error message echo '

' . __('No Analytics account selected. Double check you are authenticated with Google on Google Analyticator\'s settings page and make sure an account is selected.', 'google-analyticator') . '

'; # Add Javascript variable to prevent breaking the Javascript echo ''; die(); } # Add the header information for the visits graph echo '

' . __('Visits Over the Past 30 Days', 'google-analyticator') . '

'; # Add the sparkline for the past 30 days $this->getVisitsGraph(); # Add the summary header echo '

' . __('Site Usage', 'google-analyticator') . '

'; # Add the visitor summary $this->getSiteUsage(); # Add the top 5 posts header echo '

' . __('Top Pages', 'google-analyticator') . '

'; # Add the top 5 posts $this->getTopPages(); # Add the tab information echo '
'; # Add the top 5 referrers header echo '

' . __('Top Referrers', 'google-analyticator') . '

'; # Add the referrer information $this->getTopReferrers(); # Add the second column echo '
'; # Add the top 5 search header echo '

' . __('Top Searches', 'google-analyticator') . '

'; # Add the search information $this->getTopSearches(); # Close the table echo '
'; die(); } /** * Get the current analytics account * * @return the analytics account **/ function getAnalyticsAccount() { $accounts = array(); # Get the class for interacting with the Google Analytics require_once('class.analytics.stats.php'); # Create a new Gdata call $this->api = new GoogleAnalyticsStats(); # Check if Google sucessfully logged in if ( ! $this->api->checkLogin() ) return false; # Get a list of accounts $accounts = $this->api->getAnalyticsAccounts(); # Check if we actually have accounts if ( !is_array($accounts) ) return false; # Check if we have a list of accounts if ( count($accounts) <= 0 ) return false; # 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') ) return $account['id']; } return false; } /** * Get the visitors graph **/ function getVisitsGraph() { # Get the value from the database $cached = maybe_unserialize(get_option('google_stats_visitsGraph_' . $this->id)); $updated = false; # Check if the call has been made before if ( is_array($cached) ) { # Check if the last time called was within two hours, if so, mark to not retrieve and grab the stats array if ( $cached['lastcalled'] > ( time() - 7200 ) ) { $updated = true; $stats = $cached['stats']; } } # If the stats need to be updated if ( ! $updated ) { # Get the metrics needed to build the visits graph $before = date('Y-m-d', strtotime('-31 days')); $yesterday = date('Y-m-d', strtotime('-1 day')); $stats = $this->api->getMetrics('ga:visits', $before, $yesterday, 'ga:date', 'ga:date'); # Store the serialized stats in the database update_option('google_stats_visitsGraph_' . $this->id, array('stats'=>$stats, 'lastcalled'=>time())); } # Create a list of the data points for graphing $data = ''; $max = 0; # Check the size of the stats array if ( !isset($stats) || !is_array($stats) || count($stats) <= 0 ) { $data = '0,0'; } else { foreach ( $stats AS $stat ) { # Verify the number is numeric if ( is_numeric($stat['ga:visits']) ) $data .= $stat['ga:visits'] . ','; else $data .= '0,'; # Update the max value if greater if ( $max < $stat['ga:visits'] ) $max = $stat['ga:visits']; } # Shorten the string to remove the last comma $data = substr($data, 0, -1); } # Add a fake stat if need be if ( !isset($stat['ga:visits']) ) $stat['ga:visits'] = 0; # Output the graph echo ''; echo ''; } /** * Get the site usage **/ function getSiteUsage() { # Get the value from the database $cached = maybe_unserialize(get_option('google_stats_siteUsage_' . $this->id)); $updated = false; # Check if the call has been made before if ( is_array($cached) ) { # Check if the last time called was within two hours, if so, mark to not retrieve and grab the stats array if ( $cached['lastcalled'] > ( time() - 7200 ) ) { $updated = true; $stats = $cached['stats']; } } # If the stats need to be updated if ( ! $updated ) { # Get the metrics needed to build the usage table $before = date('Y-m-d', strtotime('-31 days')); $yesterday = date('Y-m-d', strtotime('-1 day')); $stats = $this->api->getMetrics('ga:visits,ga:bounces,ga:entrances,ga:pageviews,ga:timeOnSite,ga:newVisits', $before, $yesterday); # Store the serialized stats in the database update_option('google_stats_siteUsage_' . $this->id, array('stats'=>$stats, 'lastcalled'=>time())); } # Create the site usage table if ( isset($stats[0]) ) { ?>
0.00% %
00:00:00 sec2Time($stats[0]['ga:timeOnSite']/$stats[0]['ga:visits']); ?>
0.00 0.00% %
id)); $updated = false; # Check if the call has been made before if ( is_array($cached) ) { # Check if the last time called was within two hours, if so, mark to not retrieve and grab the stats array if ( $cached['lastcalled'] > ( time() - 7200 ) ) { $updated = true; $stats = $cached['stats']; } } # If the stats need to be updated if ( ! $updated ) { # Get the metrics needed to build the top pages $before = date('Y-m-d', strtotime('-31 days')); $yesterday = date('Y-m-d', strtotime('-1 day')); $stats = $this->api->getMetrics('ga:pageviews', $before, $yesterday, 'ga:pageTitle,ga:pagePath', '-ga:pageviews', 'ga:pagePath!%3D%2F', '10'); # Store the serialized stats in the database update_option('google_stats_topPages_' . $this->id, array('stats'=>$stats, 'lastcalled'=>time())); } # Check the size of the stats array if ( count($stats) <= 0 || !is_array($stats) ) { echo '

' . __('There is no data for view.', 'google-analyticator') . '

'; } else { # Build the top pages list echo '
    '; # Set variables needed to correct (not set) bug $new_stats = array(); $notset_stats = array(); # Loop through each stat and create a new array foreach ( $stats AS $stat ) { # If the stat is not set if ( $stat['ga:pageTitle'] == '(not set)' ) { # Add it to separate array $notset_stats[] = $stat; } else { # Add it to new array with index set $new_stats[$stat['ga:pagePath']] = $stat; } } # Loop through all the (not set) stats and attempt to add them to their correct stat foreach ( $notset_stats AS $stat ) { # If the stat has a "partner" if ( $new_stats[$stat['ga:pagePath']] != NULL ) { # Add the pageviews to the stat $new_stats[$stat['ga:pagePath']]['ga:pageviews'] = $new_stats[$stat['ga:pagePath']]['ga:pageviews'] + $stat['ga:pageviews']; } else { # Stat goes to the ether since we couldn't find a partner (if anyone reads this and has a suggestion to improve, let me know) } } # Renew new_stats back to stats $stats = $new_stats; # Sort the stats array, since adding the (not set) items may have changed the order usort($stats, array($this, 'statSort')); # Since we can no longer rely on the API as a limiter, we need to keep track of this ourselves $stat_count = 0; # Loop through each stat for display foreach ( $stats AS $stat ) { echo '
  1. ' . esc_html($stat['ga:pageTitle']) . ' - ' . number_format($stat['ga:pageviews']) . ' ' . __('Views', 'google-analyticator') . '
  2. '; # Increase the stat counter $stat_count++; # Stop at 5 if ( $stat_count >= 5 ) break; } # Finish the list echo '
'; } } /** * Get the top referrers **/ function getTopReferrers() { # Get the value from the database $cached = maybe_unserialize(get_option('google_stats_topReferrers_' . $this->id)); $updated = false; # Check if the call has been made before if ( is_array($cached) ) { # Check if the last time called was within two hours, if so, mark to not retrieve and grab the stats array if ( $cached['lastcalled'] > ( time() - 7200 ) ) { $updated = true; $stats = $cached['stats']; } } # If the stats need to be updated if ( ! $updated ) { # Get the metrics needed to build the top referrers $before = date('Y-m-d', strtotime('-31 days')); $yesterday = date('Y-m-d', strtotime('-1 day')); $stats = $this->api->getMetrics('ga:visits', $before, $yesterday, 'ga:source,ga:medium', '-ga:visits', 'ga:medium%3D%3Dreferral', '5'); # Store the serialized stats in the database update_option('google_stats_topReferrers_' . $this->id, array('stats'=>$stats, 'lastcalled'=>time())); } # Check the size of the stats array if ( count($stats) <= 0 || !is_array($stats) ) { echo '

' . __('There is no data for view.', 'google-analyticator') . '

'; } else { # Build the top pages list echo '
    '; # Loop through each stat foreach ( $stats AS $stat ) { echo '
  1. ' . esc_html($stat['ga:source']) . ' - ' . number_format($stat['ga:visits']) . ' ' . __('Visits', 'google-analyticator') . '
  2. '; } # Finish the list echo '
'; } } /** * Get the top searches **/ function getTopSearches() { # Get the value from the database $cached = maybe_unserialize(get_option('google_stats_topSearches_' . $this->id)); $updated = false; # Check if the call has been made before if ( is_array($cached) ) { # Check if the last time called was within two hours, if so, mark to not retrieve and grab the stats array if ( $cached['lastcalled'] > ( time() - 7200 ) ) { $updated = true; $stats = $cached['stats']; } } # If the stats need to be updated if ( ! $updated ) { # Get the metrics needed to build the top searches $before = date('Y-m-d', strtotime('-31 days')); $yesterday = date('Y-m-d', strtotime('-1 day')); $stats = $this->api->getMetrics('ga:visits', $before, $yesterday, 'ga:keyword', '-ga:visits', 'ga:keyword!%3D(not%20set)', '5'); # Store the serialized stats in the database update_option('google_stats_topSearches_' . $this->id, array('stats'=>$stats, 'lastcalled'=>time())); } # Check the size of the stats array if ( count($stats) <= 0 || !is_array($stats) ) { echo '

' . __('There is no data for view.', 'google-analyticator') . '

'; } else { # Build the top pages list echo '
    '; # Loop through each stat foreach ( $stats AS $stat ) { echo '
  1. ' . esc_html($stat['ga:keyword']) . ' - ' . number_format($stat['ga:visits']) . ' ' . __('Visits', 'google-analyticator') . '
  2. '; } # Finish the list echo '
'; } } /** * Sort a set of stats in descending order * * @return how the stat should be sorted **/ function statSort($x, $y) { if ( $x['ga:pageviews'] == $y['ga:pageviews'] ) return 0; elseif ( $x['ga:pageviews'] < $y['ga:pageviews'] ) return 1; else return -1; } /** * Convert second to a time format **/ function sec2Time($time) { # Check if numeric if ( is_numeric($time) ) { $value = array( "years" => '00', "days" => '00', "hours" => '00', "minutes" => '00', "seconds" => '00' ); if ( $time >= 31556926 ) { $value["years"] = floor($time/31556926); $time = ($time%31556926); } if ( $time >= 86400 ) { $value["days"] = floor($time/86400); $time = ($time%86400); } if ( $time >= 3600 ) { $value["hours"] = str_pad(floor($time/3600), 2, 0, STR_PAD_LEFT); $time = ($time%3600); } if ( $time >= 60 ) { $value["minutes"] = str_pad(floor($time/60), 2, 0, STR_PAD_LEFT); $time = ($time%60); } $value["seconds"] = str_pad(floor($time), 2, 0, STR_PAD_LEFT); # Get the hour:minute:second version return $value['hours'] . ':' . $value['minutes'] . ':' . $value['seconds']; } else { return false; } } } // END class ?>