gglnltcs_process_ajax

The BestWebSoft Google Analytics gglnltcs process ajax function.

Description

gglnltcs_process_ajax(); 

Usage

  1. if ( !function_exists( 'gglnltcs_process_ajax' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bestwebsoft-google-analytics/bws-google-analytics.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = gglnltcs_process_ajax(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bws-google-analytics.php  
  1. function gglnltcs_process_ajax() { 
  2. global $gglnltcs_options, $gglnltcs_metrics_data, $gglnltcs_dimensions_data; 
  3. /** check ajax value */ 
  4. check_ajax_referer( 'gglnltcs_ajax_nonce_value', 'gglnltcs_nonce' ); 
  5. /** Get options from the database and set them to the global array */ 
  6. /** Get access token from the global array */ 
  7. $access_token = $gglnltcs_options['token']; 
  8. /** Google Client Stuff */ 
  9. require_once 'google-api-php-client/api-code/Google_Client.php'; 
  10. require_once 'google-api-php-client/api-code/contrib/Google_AnalyticsService.php'; 
  11. $client = new Google_Client(); 
  12. $client->setApplicationName( 'BestWebSoft Google Analytics' ); 
  13. $client->setClientId( '714548546682-ai821bsdfn2th170q8ofprgfmh5ch7cn.apps.googleusercontent.com' ); 
  14. $client->setClientSecret( 'pyBXulcOqPhQGzKiW4kehZZB' ); 
  15. $client->setRedirectUri( 'urn:ietf:wg:oauth:2.0:oob' ); 
  16. $client->setDeveloperKey( 'AIzaSyDA7L2CZgY4ud4vv6rw0Yu4GUDyfbRw0f0' ); 
  17. $client->setScopes( array( 'https://www.googleapis.com/auth/analytics.readonly' ) ); 
  18. $client->setUseObjects( true );  
  19. $client->setAccessToken( $access_token ); 
  20. /** Create Analytics Object */ 
  21. $analytics = new Google_AnalyticsService( $client ); 
  22. /** Parse form data that came from ajax */ 
  23. parse_str( $_POST['settings'], $settings ); 
  24. /** Line Chart Tab */ 
  25. if ( $_POST['tab'] == 'line_chart' ) { 
  26. /** Save up date range data for the Table Tab */ 
  27. if ( isset( $gglnltcs_options['settings']['gglnltcs_start_date'] ) ) { 
  28. $start_date = $gglnltcs_options['settings']['gglnltcs_start_date']; 
  29. $settings['gglnltcs_start_date'] = $start_date; 
  30. if ( isset( $gglnltcs_options['settings']['gglnltcs_end_date'] ) ) { 
  31. $end_date = $gglnltcs_options['settings']['gglnltcs_end_date']; 
  32. $settings['gglnltcs_end_date'] = $end_date; 
  33. /** Get analytics data that will be passed to the Google Chart */ 
  34. $dimensions = 'ga:year, ga:month, ga:day'; 
  35. $metrics = 'ga:visitors, ga:newVisits, ga:visits, ga:visitBounceRate, ga:avgTimeOnSite, ga:pageviews, ga:pageviewsPerVisit'; 
  36. $results = $analytics->data_ga->get( $_POST['viewProfileId'], '365daysAgo', 'today', $metrics, array( 'dimensions' => $dimensions ) ); 
  37. $monthes = array( 
  38. '01' => __( 'Jan', 'gglnltcs' ),  
  39. '02' => __( 'Feb', 'gglnltcs' ),  
  40. '03' => __( 'Mar', 'gglnltcs' ),  
  41. '04' => __( 'Apr', 'gglnltcs' ),  
  42. '05' => __( 'May', 'gglnltcs' ),  
  43. '06' => __( 'Jun', 'gglnltcs' ),  
  44. '07' => __( 'Jul', 'gglnltcs' ),  
  45. '08' => __( 'Aug', 'gglnltcs' ),  
  46. '09' => __( 'Sep', 'gglnltcs' ),  
  47. '10' => __( 'Oct', 'gglnltcs' ),  
  48. '11' => __( 'Nov', 'gglnltcs' ),  
  49. '12' => __( 'Dec', 'gglnltcs' ),  
  50. ); 
  51. $chart_data = $chart_date = $chart_visitors = $chart_new_visits = $chart_visits = $chart_bounce_rate = $chart_avg_time = $chart_pageviews = $chart_per_visit = array(); 
  52. foreach ( $results->getRows() as $row ) {  
  53. $chart_date[] = array( $row[0], $row[1], $row[2] ); 
  54. $chart_visitors[] = array( $row[3] ); 
  55. $chart_new_visits[] = array( $row[4] ); 
  56. $chart_visits[] = array( $row[5] ); 
  57. $chart_bounce_rate[] = array( $row[6] ); 
  58. $chart_avg_time[] = array( $row[7] ); 
  59. $chart_pageviews[] = array( $row[8] ); 
  60. $chart_per_visit[] = array( $row[9] ); 
  61. array_push( $chart_data,  
  62. $chart_date,  
  63. $chart_new_visits,  
  64. $chart_visitors,  
  65. $chart_visits,  
  66. $chart_bounce_rate,  
  67. $chart_avg_time,  
  68. $chart_pageviews,  
  69. $chart_per_visit 
  70. ); 
  71. $chart_data = json_encode( $chart_data ); 
  72. echo $chart_data; 
  73. /** Table Tab Chart */ 
  74. } else if ( $_POST['tab'] == 'table_chart' ) { 
  75. /** Load metrics and dimensions data */ 
  76. gglnltcs_get_statistic( $analytics, $settings, $gglnltcs_metrics_data, $gglnltcs_dimensions_data ); 
  77. /** Save updated settings to the database */ 
  78. /** prepare data for update_option - unset unwanted $_POST vars */ 
  79. unset( $settings['gglnltcs_nonce_name'], $settings['_wp_http_referer'] ); 
  80. $gglnltcs_options['settings'] = $settings; 
  81. update_option( 'gglnltcs_options', $gglnltcs_options ); 
  82. die();