← All changes
|
includes/integrations/class-google-analytics-client.php
+20
-4
1.31.0
→
2.14.2
View file →
| @@ -84,10 +84,16 @@ | ||
| 84 | 84 | 'dimensions_count' => count($result['dimensions'] ?? []), |
| 85 | 85 | 'metrics_count' => count($result['metrics'] ?? []) |
| 86 | 86 | ]; |
| 87 | 87 | } catch (\Exception $e) { |
| 88 | + // `message` as well as `error`: the success branch above reports | |
| 89 | + // its outcome as `message`, and every consumer reads that key, so | |
| 90 | + // a failure that only set `error` was reported to the admin as an | |
| 91 | + // error with no reason at all (#852). `error` is kept for any | |
| 92 | + // caller that already reads it. | |
| 88 | 93 | return [ |
| 89 | 94 | 'success' => false, |
| 95 | + 'message' => $e->getMessage(), | |
| 90 | 96 | 'error' => $e->getMessage() |
| 91 | 97 | ]; |
| 92 | 98 | } |
| 93 | 99 | } |
| @@ -114,8 +120,16 @@ | ||
| 114 | 120 | |
| 115 | 121 | /** |
| 116 | 122 | * Run analytics report for specified metrics and date range |
| 117 | 123 | * |
| 124 | + * The window is exactly `$date_range` days long and ends yesterday (UTC): | |
| 125 | + * "30d" is the 30 complete days before today. GA4's startDate and endDate | |
| 126 | + * are both inclusive, so the start is `days - 1` days before the end. | |
| 127 | + * Today is left out because it is a partial day, which is also what | |
| 128 | + * GA4's own "Last 30 days" does. Search Console ends two days earlier | |
| 129 | + * (D-2) because its data lags; GA4's does not, so it is not held back | |
| 130 | + * to match (#923). | |
| 131 | + * | |
| 118 | 132 | * @param string $date_range Date range ('7d', '30d', '90d') |
| 119 | 133 | * @param array $metrics Metrics to retrieve (GA4 metric names) |
| 120 | 134 | * @param array $dimensions Dimensions to group by |
| 121 | 135 | * @return array Analytics report data |
| @@ -123,12 +137,14 @@ | ||
| 123 | 137 | */ |
| 124 | 138 | public function run_report(string $date_range = '30d', array $metrics = ['sessions'], array $dimensions = []): array { |
| 125 | 139 | $endpoint = "/{$this->property_id}:runReport"; |
| 126 | 140 | |
| 127 | - // Convert date range to start/end dates | |
| 128 | - $end_date = gmdate('Y-m-d'); | |
| 129 | - $days = (int) str_replace('d', '', $date_range); | |
| 130 | - $start_date = gmdate('Y-m-d', strtotime("-{$days} days")); | |
| 141 | + // Convert date range to start/end dates, from one anchor so a request | |
| 142 | + // straddling midnight cannot mix two days. | |
| 143 | + $days = max(1, (int) str_replace('d', '', $date_range)); | |
| 144 | + $end_ts = strtotime('-1 day', time()); | |
| 145 | + $end_date = gmdate('Y-m-d', $end_ts); | |
| 146 | + $start_date = gmdate('Y-m-d', strtotime('-' . ($days - 1) . ' days', $end_ts)); | |
| 131 | 147 | |
| 132 | 148 | $request_body = [ |
| 133 | 149 | 'dateRanges' => [ |
| 134 | 150 | [ |