's Dashboard
Enter your Google Analytics Property ID
This is required to fetch data from your Analytics account.
GOOGLE_CLIENT_ID, 'client_secret' => GOOGLE_CLIENT_SECRET, 'refresh_token' => $refresh_token, 'grant_type' => 'refresh_token' ]; $ch = curl_init($token_url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($token_params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http_code == 200) { $new_token_data = json_decode($response, true); if (isset($new_token_data['access_token'])) { $tokens['access_token'] = $new_token_data['access_token']; file_put_contents('db/google_tokens.json', json_encode($tokens)); $access_token = $new_token_data['access_token']; // --- 2. Fetch Data from Google Analytics Data API --- $analytics_api_url = "https://analyticsdata.googleapis.com/v1beta/properties/{$ga_property_id}:runReport"; $api_request_body = [ 'dateRanges' => [['startDate' => '28daysAgo', 'endDate' => 'today']], 'metrics' => [ ['name' => 'sessions'], ['name' => 'totalUsers'], ['name' => 'screenPageViews'], ['name' => 'bounceRate'] ] ]; $ch_analytics = curl_init($analytics_api_url); curl_setopt($ch_analytics, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $access_token, 'Content-Type: application/json' ]); curl_setopt($ch_analytics, CURLOPT_POST, true); curl_setopt($ch_analytics, CURLOPT_POSTFIELDS, json_encode($api_request_body)); curl_setopt($ch_analytics, CURLOPT_RETURNTRANSFER, true); $analytics_response = curl_exec($ch_analytics); $analytics_http_code = curl_getinfo($ch_analytics, CURLINFO_HTTP_CODE); curl_close($ch_analytics); if ($analytics_http_code == 200) { $report = json_decode($analytics_response, true); if (isset($report['rows'][0]['metricValues'])) { $metricValues = $report['rows'][0]['metricValues']; $_SESSION['ga_data'] = [ 'sessions' => $metricValues[0]['value'] ?? '0', 'users' => $metricValues[1]['value'] ?? '0', 'page_views' => $metricValues[2]['value'] ?? '0', 'bounce_rate' => isset($metricValues[3]['value']) ? round($metricValues[3]['value'] * 100, 2) . '%' : '0%', ]; } else { $_SESSION['ga_error'] = "No data returned from Analytics. Check if the property ID is correct and has recent data."; } } else { $error_response = json_decode($analytics_response, true); $error_message = "HTTP {$analytics_http_code}"; if (is_array($error_response) && isset($error_response['error']['message'])) { $error_message = $error_response['error']['message']; } else { error_log("Unknown Google API Error. Code: {$analytics_http_code}. Response: " . $analytics_response); } $_SESSION['ga_error'] = "Error fetching Analytics data: " . $error_message; } } else { $_SESSION['ga_error'] = "Failed to refresh token, new access token not found."; } } else { $_SESSION['ga_error'] = "Error refreshing Google token. Please try disconnecting and reconnecting."; } // Redirect to clean the URL and prevent re-submission header('Location: dashboard.php'); exit(); } // Load data from session if available if (isset($_SESSION['ga_data'])) { $ga_data = $_SESSION['ga_data']; unset($_SESSION['ga_data']); // Clear after loading } if (isset($_SESSION['ga_error'])) { $ga_error = $_SESSION['ga_error']; unset($_SESSION['ga_error']); // Clear after loading } // Dummy data for other sections (can be replaced later) $client_name = "Example Client"; $chart_data = [ "labels" => ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], "values" => [5000, 5500, 6200, 7100, 8000, 8500, 9200, 9800, 10500, 11200, 12000, 12345], ]; ?>
This is required to fetch data from your Analytics account.