= 0) { require 'vendor/autoload.php'; } else { set_include_path ( $GADASH_Config->plugin_path . '/tools/src/' . PATH_SEPARATOR . get_include_path () ); // Include GAPI client if (! class_exists ( 'Google_Client' )) { require_once 'Google/Client.php'; } // Include GAPI Analytics Service if (! class_exists ( 'Google_Service_Analytics' )) { require_once 'Google/Service/Analytics.php'; } } $this->client = new Google_Client (); $this->client->setScopes ( 'https://www.googleapis.com/auth/analytics.readonly' ); $this->client->setAccessType ( 'offline' ); $this->client->setApplicationName ( 'Google Analytics Dashboard' ); $this->client->setRedirectUri ( 'urn:ietf:wg:oauth:2.0:oob' ); if ($GADASH_Config->options ['ga_dash_userapi']) { $this->client->setClientId ( $GADASH_Config->options ['ga_dash_clientid'] ); $this->client->setClientSecret ( $GADASH_Config->options ['ga_dash_clientsecret'] ); $this->client->setDeveloperKey ( $GADASH_Config->options ['ga_dash_apikey'] ); } else { $this->client->setClientId ( '65556128781.apps.googleusercontent.com' ); $this->client->setClientSecret ( 'Kc7888wgbc_JbeCpbFjnYpwE' ); $this->client->setDeveloperKey ( 'AIzaSyBG7LlUoHc29ZeC_dsShVaBEX15SfRl_WY' ); } $this->service = new Google_Service_Analytics ( $this->client ); if ($GADASH_Config->options ['ga_dash_token']) { $token = $GADASH_Config->options ['ga_dash_token']; $token = $this->ga_dash_refresh_token (); if ($token) { $this->client->setAccessToken ( $token ); } } } function get_timeouts($daily) { $local_time = time () + $this->timeshift; if ($daily) { $nextday = explode ( '-', date ( 'n-j-Y', strtotime ( ' +1 day', $local_time ) ) ); $midnight = mktime ( 0, 0, 0, $nextday [0], $nextday [1], $nextday [2] ); return $midnight - $local_time; } else { $nexthour = explode ( '-', date ( 'H-n-j-Y', strtotime ( ' +1 hour', $local_time ) ) ); $newhour = mktime ( $nexthour [0], 0, 0, $nexthour [1], $nexthour [2], $nexthour [3] ); return $newhour - $local_time; } } function token_request() { $authUrl = $this->client->createAuthUrl (); ?>
service->management_profiles->listManagementProfiles ( '~all', '~all' ); $items = $profiles->getItems (); if (count ( $items ) != 0) { $ga_dash_profile_list = array (); foreach ( $items as $profile ) { $timetz = new DateTimeZone ( $profile->getTimezone () ); $localtime = new DateTime ( 'now', $timetz ); $timeshift = strtotime ( $localtime->format ( 'Y-m-d H:i:s' ) ) - time (); $ga_dash_profile_list [] = array ( $profile->getName (), $profile->getId (), $profile->getwebPropertyId (), $profile->getwebsiteUrl (), $timeshift, $profile->getTimezone () ); } update_option ( 'gadash_lasterror', 'N/A' ); return ($ga_dash_profile_list); } else { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': No properties were found in this account!' ); } } catch ( Google_IO_Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return false; } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); $this->ga_dash_reset_token ( true ); } } function ga_dash_refresh_token() { global $GADASH_Config; try { if (is_multisite () && $GADASH_Config->options ['ga_dash_network']) { $transient = get_site_transient ( "ga_dash_refresh_token" ); } else { $transient = get_transient ( "ga_dash_refresh_token" ); } if (empty ( $transient )) { if (! $GADASH_Config->options ['ga_dash_refresh_token']) { $google_token = json_decode ( $GADASH_Config->options ['ga_dash_token'] ); $GADASH_Config->options ['ga_dash_refresh_token'] = $google_token->refresh_token; $this->client->refreshToken ( $google_token->refresh_token ); } else { $this->client->refreshToken ( $GADASH_Config->options ['ga_dash_refresh_token'] ); } $token = $this->client->getAccessToken (); $google_token = json_decode ( $token ); $GADASH_Config->options ['ga_dash_token'] = $token; if (is_multisite () && $GADASH_Config->options ['ga_dash_network']) { set_site_transient ( "ga_dash_refresh_token", $token, $google_token->expires_in ); $GADASH_Config->set_plugin_options ( true ); } else { set_transient ( "ga_dash_refresh_token", $token, $google_token->expires_in ); $GADASH_Config->set_plugin_options (); } return $token; } else { return $transient; } } catch ( Google_IO_Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return false; } catch ( Exception $e ) { $this->ga_dash_reset_token ( false ); update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return false; } } function ga_dash_reset_token($all = true) { global $GADASH_Config; if (is_multisite () && $GADASH_Config->options ['ga_dash_network']) { delete_site_transient ( 'ga_dash_refresh_token' ); } else { delete_transient ( 'ga_dash_refresh_token' ); } $GADASH_Config->options ['ga_dash_token'] = ""; $GADASH_Config->options ['ga_dash_refresh_token'] = ""; if ($all) { $GADASH_Config->options ['ga_dash_tableid'] = ""; $GADASH_Config->options ['ga_dash_tableid_jail'] = ""; $GADASH_Config->options ['ga_dash_profile_list'] = ""; try { $this->client->revokeToken (); } catch ( Exception $e ) { if (is_multisite () && $GADASH_Config->options ['ga_dash_network']) { $GADASH_Config->set_plugin_options ( true ); } else { $GADASH_Config->set_plugin_options (); } } } if (is_multisite () && $GADASH_Config->options ['ga_dash_network']) { $GADASH_Config->set_plugin_options ( true ); } else { $GADASH_Config->set_plugin_options (); } } // Get Main Chart function ga_dash_main_charts($projectId, $period, $from, $to, $query) { global $GADASH_Config; $metrics = 'ga:' . $query; if ($period == "today") { $dimensions = 'ga:hour'; $timeouts = 0; } else if ($period == "yesterday") { $dimensions = 'ga:hour'; $timeouts = 1; } else { $dimensions = 'ga:year,ga:month,ga:day'; $timeouts = 1; } try { $serial = 'gadash_qr2' . str_replace ( array ( 'ga:', ',', '-' ), "", $projectId . $from . $metrics ); $transient = get_transient ( $serial ); if (empty ( $transient )) { $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions ) ); set_transient ( $serial, $data, $this->get_timeouts ( $timeouts ) ); } else { $data = $transient; } } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return 0; } $ga_dash_statsdata = ""; if ($period == "today" or $period == "yesterday") { for($i = 0; $i < $data ['totalResults']; $i ++) { $ga_dash_statsdata .= "['" . $data ['rows'] [$i] [0] . ":00'," . round ( $data ['rows'] [$i] [1], 2 ) . "],"; } } else { for($i = 0; $i < $data ['totalResults']; $i ++) { $ga_dash_statsdata .= "['" . $data ['rows'] [$i] [0] . "-" . $data ['rows'] [$i] [1] . "-" . $data ['rows'] [$i] [2] . "'," . round ( $data ['rows'] [$i] [3], 2 ) . "],"; } } return wp_kses ( rtrim ( $ga_dash_statsdata, ',' ), $GADASH_Config->allowed_html ); } // Get bottom Stats function ga_dash_bottom_stats($projectId, $period, $from, $to) { global $GADASH_Config; if ($period == "today") { $timeouts = 0; } else { $timeouts = 1; } $metrics = 'ga:visits,ga:visitors,ga:pageviews,ga:visitBounceRate,ga:organicSearches,ga:timeOnSite'; $dimensions = 'ga:year'; try { $serial = 'gadash_qr3' . $projectId . $from; $transient = get_transient ( $serial ); if (empty ( $transient )) { $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions ) ); set_transient ( $serial, $data, $this->get_timeouts ( $timeouts ) ); } else { $data = $transient; } } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return 0; } if (isset ( $data ['rows'] [1] [1] )) { for($i = 1; $i < 6; $i ++) { $data ['rows'] [0] [$i] += $data ['rows'] [1] [$i]; if ($i == 4) { $data ['rows'] [0] [$i] = $data ['rows'] [0] [$i] / 2; } } } return $data; } // Get Top Pages function ga_dash_top_pages($projectId, $from, $to) { global $GADASH_Config; $metrics = 'ga:pageviews'; $dimensions = 'ga:pageTitle,ga:hostname,ga:pagePath'; if ($from == "today") { $timeouts = 0; } else { $timeouts = 1; } try { $serial = 'gadash_qr4' . $projectId . $from; $transient = get_transient ( $serial ); if (empty ( $transient )) { $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions, 'sort' => '-ga:pageviews', 'max-results' => '24' ) ); // 'filters' => 'ga:pagePath!=/' set_transient ( $serial, $data, $this->get_timeouts ( $timeouts ) ); } else { $data = $transient; } } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return 0; } if (! isset ( $data ['rows'] )) { return 0; } $ga_dash_data = ""; $i = 0; // print_r($data ['rows'] ); while ( isset ( $data ['rows'] [$i] [0] ) ) { $ga_dash_data .= "['" . addslashes ( $data ['rows'] [$i] [0] ) . "'," . $data ['rows'] [$i] [3] . "],"; $i ++; } return wp_kses ( rtrim ( $ga_dash_data, ',' ), $GADASH_Config->allowed_html ); } // Get Top referrers function ga_dash_top_referrers($projectId, $from, $to) { global $GADASH_Config; $metrics = 'ga:visits'; $dimensions = 'ga:source,ga:fullReferrer,ga:medium'; if ($from == "today") { $timeouts = 0; } else { $timeouts = 1; } try { $serial = 'gadash_qr5' . $projectId . $from; $transient = get_transient ( $serial ); if (empty ( $transient )) { $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions, 'sort' => '-ga:visits', 'max-results' => '24', 'filters' => 'ga:medium==referral' ) ); set_transient ( $serial, $data, $this->get_timeouts ( $timeouts ) ); } else { $data = $transient; } } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return 0; } if (! isset ( $data ['rows'] )) { return 0; } $ga_dash_data = ""; $i = 0; while ( isset ( $data ['rows'] [$i] [0] ) ) { $ga_dash_data .= "['" . addslashes ( $data ['rows'] [$i] [0] ) . "'," . $data ['rows'] [$i] [3] . "],"; $i ++; } return wp_kses ( rtrim ( $ga_dash_data, ',' ), $GADASH_Config->allowed_html ); } // Get Top searches function ga_dash_top_searches($projectId, $from, $to) { global $GADASH_Config; $metrics = 'ga:visits'; $dimensions = 'ga:keyword'; if ($from == "today") { $timeouts = 0; } else { $timeouts = 1; } try { $serial = 'gadash_qr6' . $projectId . $from; $transient = get_transient ( $serial ); if (empty ( $transient )) { $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions, 'sort' => '-ga:visits', 'max-results' => '24' ) ); set_transient ( $serial, $data, $this->get_timeouts ( $timeouts ) ); } else { $data = $transient; } } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return 0; } if (! isset ( $data ['rows'] )) { return 0; } $ga_dash_data = ""; $i = 0; while ( isset ( $data ['rows'] [$i] [0] ) ) { if ($data ['rows'] [$i] [0] != "(not set)") { $ga_dash_data .= "['" . addslashes ( $data ['rows'] [$i] [0] ) . "'," . $data ['rows'] [$i] [1] . "],"; } $i ++; } return wp_kses ( rtrim ( $ga_dash_data, ',' ), $GADASH_Config->allowed_html ); } // Get Visits by Country function ga_dash_visits_country($projectId, $from, $to) { global $GADASH_Config; $metrics = 'ga:visits'; $options = ""; if ($from == "today") { $timeouts = 0; } else { $timeouts = 1; } if ($GADASH_Config->options ['ga_target_geomap']) { $dimensions = 'ga:city'; $this->getcountrycodes (); $filters = 'ga:country==' . ($this->country_codes [$GADASH_Config->options ['ga_target_geomap']]); } else { $dimensions = 'ga:country'; $filters = ""; } try { if ($GADASH_Config->options ['ga_target_geomap']) { $serial = 'gadash_qr7' . $projectId . $from . $GADASH_Config->options ['ga_target_geomap'] . $GADASH_Config->options ['ga_target_number']; } else { $serial = 'gadash_qr7' . $projectId . $from; } $transient = get_transient ( $serial ); if (empty ( $transient )) { if ($filters) $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions, 'filters' => $filters, 'sort' => '-ga:visits', 'max-results' => $GADASH_Config->options ['ga_target_number'] ) ); else $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions ) ); set_transient ( $serial, $data, $this->get_timeouts ( $timeouts ) ); } else { $data = $transient; } } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return 0; } if (! isset ( $data ['rows'] )) { return 0; } $ga_dash_data = ""; $i = 0; while ( isset ( $data ['rows'] [$i] [1] ) ) { $ga_dash_data .= "['" . addslashes ( $data ['rows'] [$i] [0] ) . "'," . $data ['rows'] [$i] [1] . "],"; $i ++; } return rtrim ( $ga_dash_data, ',' ); } // Get Traffic Sources function ga_dash_traffic_sources($projectId, $from, $to) { global $GADASH_Config; $metrics = 'ga:visits'; $dimensions = 'ga:medium'; if ($from == "today") { $timeouts = 0; } else { $timeouts = 1; } try { $serial = 'gadash_qr8' . $projectId . $from; $transient = get_transient ( $serial ); if (empty ( $transient )) { $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions ) ); set_transient ( $serial, $data, $this->get_timeouts ( $timeouts ) ); } else { $data = $transient; } } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return 0; } if (! isset ( $data ['rows'] )) { return 0; } $ga_dash_data = ""; for($i = 0; $i < $data ['totalResults']; $i ++) { $ga_dash_data .= "['" . str_replace ( "(none)", "direct", $data ['rows'] [$i] [0] ) . "'," . $data ['rows'] [$i] [1] . "],"; } return wp_kses ( rtrim ( $ga_dash_data, ',' ), $GADASH_Config->allowed_html ); } // Get New vs. Returning function ga_dash_new_return($projectId, $from, $to) { global $GADASH_Config; $metrics = 'ga:visits'; $dimensions = 'ga:visitorType'; if ($from == "today") { $timeouts = 0; } else { $timeouts = 1; } try { $serial = 'gadash_qr9' . $projectId . $from; $transient = get_transient ( $serial ); if (empty ( $transient )) { $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions ) ); set_transient ( $serial, $data, $this->get_timeouts ( $timeouts ) ); } else { $data = $transient; } } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return 0; } if (! isset ( $data ['rows'] )) { return 0; } $ga_dash_data = ""; for($i = 0; $i < $data ['totalResults']; $i ++) { $ga_dash_data .= "['" . addslashes ( $data ['rows'] [$i] [0] ) . "'," . $data ['rows'] [$i] [1] . "],"; } return wp_kses ( rtrim ( $ga_dash_data, ',' ), $GADASH_Config->allowed_html ); } // Frontend Widget Stats function frontend_widget_stats($projectId, $period, $anonim, $display) { global $GADASH_Config; $content = ''; $from = $period; $to = 'yesterday'; $metrics = 'ga:visits'; $dimensions = 'ga:year,ga:month,ga:day'; $title = __ ( "Visits", 'ga-dash' ) . ($anonim ? __ ( "\' trend", 'ga-dash' ) : ''); /* * Include Tools */ include_once ($GADASH_Config->plugin_path . '/tools/tools.php'); $tools = new GADASH_Tools (); if (isset ( $GADASH_Config->options ['ga_dash_style'] )) { $css = "colors:['" . $GADASH_Config->options ['ga_dash_style'] . "','" . $tools->colourVariator ( $GADASH_Config->options ['ga_dash_style'], - 20 ) . "'],"; $color = $GADASH_Config->options ['ga_dash_style']; } else { $css = ""; $color = "#3366CC"; } try { $serial = 'gadash_qr2' . str_replace ( array ( 'ga:', ',', '-' ), "", $projectId . $from . $metrics ); $transient = get_transient ( $serial ); if (empty ( $transient )) { $data = $this->service->data_ga->get ( 'ga:' . $projectId, $from, $to, $metrics, array ( 'dimensions' => $dimensions ) ); set_transient ( $serial, $data, $this->get_timeouts ( 1 ) ); } else { $data = $transient; } } catch ( Exception $e ) { update_option ( 'gadash_lasterror', date ( 'Y-m-d H:i:s' ) . ': ' . esc_html ( $e ) ); return ''; } if (! isset ( $data ['rows'] )) { return ''; } $ga_dash_statsdata = ""; $max_array = array (); foreach ( $data ['rows'] as $item ) { $max_array [] = $item [3]; } $max = max ( $max_array ) ? max ( $max_array ) : 1; for($i = 0; $i < $data ['totalResults']; $i ++) { $ga_dash_statsdata .= "['" . $data ['rows'] [$i] [0] . "-" . $data ['rows'] [$i] [1] . "-" . $data ['rows'] [$i] [2] . "'," . ($anonim ? str_replace ( ',', '.', round ( $data ['rows'] [$i] [3] * 100 / $max, 2 ) ) : $data ['rows'] [$i] [3]) . "],"; } $ga_dash_statsdata = wp_kses ( rtrim ( $ga_dash_statsdata, ',' ), $GADASH_Config->allowed_html ); if ($ga_dash_statsdata) { if ($display != 3) { if ($anonim) { $formater = "var formatter = new google.visualization.NumberFormat({ suffix: '%', fractionDigits: 2 }); formatter.format(data, 1); "; } else { $formater = ''; } $content = ' "; $content .= ''; } if ($display != 2 and isset ( $data ['totalsForAllResults'] ['ga:visits'] )) { switch ($period) { case '7daysAgo' : $periodtext = __ ( 'Last 7 Days', 'ga-dash' ); break; case '14daysAgo' : $periodtext = __ ( 'Last 14 Days', 'ga-dash' ); break; default : $periodtext = __ ( 'Last 30 Days', 'ga-dash' ); break; } $content .= '| ' . __ ( "Period:", 'ga-dash' ) . ' | ' . $periodtext . ' |
| ' . __ ( 'Total Visits:', 'ga-dash' ) . ' | ' . ($data ['totalsForAllResults'] ['ga:visits']) . ' |