types
1 year ago
adsense-report-api.php
1 year ago
class-ad-type-adsense.php
3 months ago
class-adsense-report-data.php
1 year ago
class-adsense-report.php
1 year ago
class-gadsense-data.php
1 year ago
class-mapi.php
1 year ago
class-network-adsense.php
1 year ago
adsense-report-api.php
173 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName |
| 2 | /** |
| 3 | * Report API for AdSense. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.2 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Retrieve report data from Google. |
| 12 | */ |
| 13 | class Advanced_Ads_AdSense_Report_Api { |
| 14 | |
| 15 | /** |
| 16 | * Version of the AdSense Management API in use (for getting fresh data). |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | const API_VERSION = '2.0'; |
| 21 | |
| 22 | /** |
| 23 | * Report API endpoint. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | private $endpoint_url; |
| 28 | |
| 29 | /** |
| 30 | * Report type |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private $type; |
| 35 | |
| 36 | /** |
| 37 | * The API access token or an error array. |
| 38 | * |
| 39 | * @var array|string |
| 40 | */ |
| 41 | private $access_token; |
| 42 | |
| 43 | /** |
| 44 | * The current connected AdSense account. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | private $publisher_id; |
| 49 | |
| 50 | /** |
| 51 | * Instance constructor. |
| 52 | * |
| 53 | * @param string $type report type. |
| 54 | */ |
| 55 | public function __construct( $type ) { |
| 56 | $publisher_id = Advanced_Ads_AdSense_Data::get_instance()->get_adsense_id(); |
| 57 | $this->type = $type; |
| 58 | $this->access_token = Advanced_Ads_AdSense_MAPI::get_access_token( $publisher_id ); |
| 59 | $this->publisher_id = $publisher_id; |
| 60 | |
| 61 | $endpoint_args = [ |
| 62 | 'startDate.year' => '%SY%', // Start date's year - integer (4 digits). |
| 63 | 'startDate.month' => '%SM%', // Start date's month - integer. |
| 64 | 'startDate.day' => '%SD%', // Start date's integer - integer. |
| 65 | 'endDate.year' => '%EY%', // End date's year - integer (4 digits). |
| 66 | 'endDate.month' => '%EM%', // End date's month - integer. |
| 67 | 'endDate.day' => '%ED%', // End date's integer - integer. |
| 68 | 'dimension1' => '%DIM%', // Primary reporting dimension (domain name or ad unit name). |
| 69 | 'dimension2' => 'DATE', // Secondary reporting dimension. |
| 70 | 'metrics' => 'ESTIMATED_EARNINGS', // Report metrics. |
| 71 | 'reportingTimeZone' => 'ACCOUNT_TIME_ZONE', // Time zone used in report data. |
| 72 | ]; |
| 73 | $this->endpoint_url = str_replace( [ 'dimension1', 'dimension2' ], 'dimensions', add_query_arg( $endpoint_args, 'https://adsense.googleapis.com/v2/accounts/%pubid%/reports:generate' ) ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Checks if the current setup has an access token. |
| 78 | * |
| 79 | * @return bool true if there is a token. |
| 80 | */ |
| 81 | public function has_token() { |
| 82 | return is_string( $this->access_token ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get access token related error message. |
| 87 | * |
| 88 | * @return array Array of error messages. |
| 89 | */ |
| 90 | public function get_token_error() { |
| 91 | return is_string( $this->access_token ) ? [] : $this->access_token; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Check if there is an error related to access tokens. |
| 96 | * |
| 97 | * @return bool true if any error happened when requesting an access token. |
| 98 | */ |
| 99 | public function has_token_error() { |
| 100 | return ! is_string( $this->access_token ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Perform the actual call to Google for fresh data. |
| 105 | * |
| 106 | * @return array associative array with the response or with error data in case of failure. |
| 107 | */ |
| 108 | public function call_google() { |
| 109 | $dimension = 'unit' === $this->type ? 'AD_UNIT_ID' : 'DOMAIN_NAME'; |
| 110 | $today = new DateTimeImmutable(); |
| 111 | $start_date = $today->sub( date_interval_create_from_date_string( '28 days' ) ); |
| 112 | // Replace placeholder in the endpoint with actual arguments. |
| 113 | $url = str_replace( |
| 114 | [ |
| 115 | '%pubid%', |
| 116 | '%DIM%', |
| 117 | '%SY%', |
| 118 | '%SM%', |
| 119 | '%SD%', |
| 120 | '%EY%', |
| 121 | '%EM%', |
| 122 | '%ED%', |
| 123 | ], |
| 124 | [ |
| 125 | $this->publisher_id, |
| 126 | $dimension, |
| 127 | $start_date->format( 'Y' ), |
| 128 | $start_date->format( 'n' ), |
| 129 | $start_date->format( 'j' ), |
| 130 | $today->format( 'Y' ), |
| 131 | $today->format( 'n' ), |
| 132 | $today->format( 'j' ), |
| 133 | ], |
| 134 | $this->endpoint_url |
| 135 | ); |
| 136 | |
| 137 | $headers = [ |
| 138 | 'Authorization' => 'Bearer ' . $this->access_token, |
| 139 | ]; |
| 140 | |
| 141 | $response = wp_remote_get( $url, [ 'headers' => $headers ] ); |
| 142 | Advanced_Ads_AdSense_MAPI::log( 'Fetched AdSense Report from ' . $url ); |
| 143 | |
| 144 | if ( is_wp_error( $response ) ) { |
| 145 | return [ |
| 146 | 'status' => false, |
| 147 | /* translators: AdSense ID. */ |
| 148 | 'msg' => sprintf( esc_html__( 'Error while retrieving report for "%s".', 'advanced-ads' ), $this->publisher_id ), |
| 149 | 'raw' => $response->get_error_message(), |
| 150 | ]; |
| 151 | } |
| 152 | |
| 153 | $response_body = json_decode( $response['body'], true ); |
| 154 | |
| 155 | if ( ! isset( $response_body['startDate'] ) ) { |
| 156 | return [ |
| 157 | 'status' => false, |
| 158 | /* translators: AdSense ID. */ |
| 159 | 'msg' => sprintf( esc_html__( 'Invalid response while retrieving report for "%s".', 'advanced-ads' ), $this->publisher_id ), |
| 160 | 'raw' => $response['body'], |
| 161 | ]; |
| 162 | } |
| 163 | |
| 164 | $response_body['api_version'] = self::API_VERSION; |
| 165 | $response_body['timestamp'] = time(); |
| 166 | |
| 167 | return [ |
| 168 | 'status' => true, |
| 169 | 'response_body' => $response_body, |
| 170 | ]; |
| 171 | } |
| 172 | } |
| 173 |