← All changes
|
includes/integrations/class-google-analytics-client.php
+18
-7
1.0.0
→
2.7.0
View file →
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /** |
| 3 | 4 | * Google Analytics Client Class |
| 4 | 5 | * |
| 5 | 6 | * Handles communication with Google Analytics API for website analytics data |
| @@ -57,11 +58,12 @@ | ||
| 57 | 58 | * |
| 58 | 59 | * @param string $api_key Google Analytics API key |
| 59 | 60 | * @param string $property_id GA4 Property ID (properties/XXXXXXXXX) |
| 60 | 61 | * @param int $timeout Request timeout in seconds |
| 62 | + * @param string|null $access_token OAuth Access Token (optional) | |
| 61 | 63 | */ |
| 62 | - public function __construct(string $api_key, string $property_id, int $timeout = 30) { | |
| 63 | - parent::__construct($api_key, $timeout); | |
| 64 | + public function __construct(string $api_key, string $property_id, int $timeout = 30, ?string $access_token = null) { | |
| 65 | + parent::__construct($api_key, $timeout, $access_token); | |
| 64 | 66 | $this->property_id = $property_id; |
| 65 | 67 | } |
| 66 | 68 | |
| 67 | 69 | /** |
| @@ -97,11 +99,17 @@ | ||
| 97 | 99 | * @throws \Exception If API request fails |
| 98 | 100 | */ |
| 99 | 101 | public function get_metadata(): array { |
| 100 | 102 | $endpoint = "/{$this->property_id}/metadata"; |
| 101 | - $params = ['key' => $this->api_key]; | |
| 103 | + $params = []; | |
| 102 | 104 | |
| 103 | 105 | $full_url = self::API_BASE_URL . $endpoint; |
| 106 | + | |
| 107 | + // Only append API key if no access token is present | |
| 108 | + if (!empty($this->api_key) && empty($this->access_token)) { | |
| 109 | + $params['key'] = $this->api_key; | |
| 110 | + } | |
| 111 | + | |
| 104 | 112 | return $this->make_request($full_url, $params, 'GET'); |
| 105 | 113 | } |
| 106 | 114 | |
| 107 | 115 | /** |
| @@ -127,9 +135,9 @@ | ||
| 127 | 135 | 'startDate' => $start_date, |
| 128 | 136 | 'endDate' => $end_date |
| 129 | 137 | ] |
| 130 | 138 | ], |
| 131 | - 'metrics' => array_map(function($metric) { | |
| 139 | + 'metrics' => array_map(function ($metric) { | |
| 132 | 140 | return ['name' => $metric]; |
| 133 | 141 | }, $metrics) |
| 134 | 142 | ]; |
| 135 | 143 | |
| @@ -134,14 +142,17 @@ | ||
| 134 | 142 | ]; |
| 135 | 143 | |
| 136 | 144 | // Add dimensions if provided |
| 137 | 145 | if (!empty($dimensions)) { |
| 138 | - $request_body['dimensions'] = array_map(function($dimension) { | |
| 146 | + $request_body['dimensions'] = array_map(function ($dimension) { | |
| 139 | 147 | return ['name' => $dimension]; |
| 140 | 148 | }, $dimensions); |
| 141 | 149 | } |
| 142 | 150 | |
| 143 | - $full_url = self::API_BASE_URL . $endpoint . '?key=' . $this->api_key; | |
| 151 | + $full_url = self::API_BASE_URL . $endpoint; | |
| 152 | + | |
| 153 | + // The API key is sent by make_request() in the x-goog-api-key header — never | |
| 154 | + // in the query string, which is captured by proxy/access logs. | |
| 144 | 155 | return $this->make_request($full_url, $request_body, 'POST'); |
| 145 | 156 | } |
| 146 | 157 | |
| 147 | 158 | /** |
| @@ -298,9 +309,9 @@ | ||
| 298 | 309 | * |
| 299 | 310 | * @return string Measurement ID |
| 300 | 311 | */ |
| 301 | 312 | public function get_measurement_id(): string { |
| 302 | - return $this->measurement_id; | |
| 313 | + return $this->property_id; | |
| 303 | 314 | } |
| 304 | 315 | |
| 305 | 316 | /** |
| 306 | 317 | * Get property ID |