-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMerchantMetrics.php
199 lines (169 loc) · 5.87 KB
/
MerchantMetrics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\API\Google;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Query\AdsCampaignReportQuery;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Query\MerchantFreeListingReportQuery;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\Ads\GoogleAdsClient;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\TransientsInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\WP;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\SearchResponse;
use DateTime;
use Exception;
use Google\Ads\GoogleAds\V12\Services\GoogleAdsRow;
use Google\ApiCore\PagedListResponse;
/**
* Class MerchantMetrics
*
* @since 1.7.0
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\API\Google
*/
class MerchantMetrics implements OptionsAwareInterface {
use OptionsAwareTrait;
/**
* The Google shopping client.
*
* @var ShoppingContent
*/
protected $shopping_client;
/**
* The Google ads client.
*
* @var GoogleAdsClient
*/
protected $ads_client;
/**
* @var WP
*/
protected $wp;
/**
* @var TransientsInterface
*/
protected $transients;
protected const MAX_QUERY_START_DATE = '2020-01-01';
/**
* MerchantMetrics constructor.
*
* @param ShoppingContent $shopping_client
* @param GoogleAdsClient $ads_client
* @param WP $wp
* @param TransientsInterface $transients
*/
public function __construct( ShoppingContent $shopping_client, GoogleAdsClient $ads_client, WP $wp, TransientsInterface $transients ) {
$this->shopping_client = $shopping_client;
$this->ads_client = $ads_client;
$this->wp = $wp;
$this->transients = $transients;
}
/**
* Get free listing metrics.
*
* @return array Of metrics or empty if no metrics were available.
* @type int $clicks Number of free clicks.
* @type int $impressions NUmber of free impressions.
*
* @throws Exception When unable to get clicks data.
*/
public function get_free_listing_metrics(): array {
if ( ! $this->options->get_merchant_id() ) {
// Merchant account not set up
return [];
}
// Google API requires a date clause to be set but there doesn't seem to be any limits on how wide the range
$query = ( new MerchantFreeListingReportQuery( [] ) )
->set_client( $this->shopping_client, $this->options->get_merchant_id() )
->where_date_between( self::MAX_QUERY_START_DATE, $this->get_tomorrow() )
->fields( [ 'clicks', 'impressions' ] );
/** @var SearchResponse $response */
$response = $query->get_results();
if ( empty( $response ) || empty( $response->getResults() ) ) {
return [];
}
$report_row = $response->getResults()[0];
return [
'clicks' => (int) $report_row->getMetrics()->getClicks(),
'impressions' => (int) $report_row->getMetrics()->getImpressions(),
];
}
/**
* Get free listing metrics but cached for 12 hours.
*
* PLEASE NOTE: These metrics will not be 100% accurate since there is no invalidation apart from the 12 hour refresh.
*
* @return array Of metrics or empty if no metrics were available.
* @type int $clicks Number of free clicks.
* @type int $impressions NUmber of free impressions.
*
* @throws Exception When unable to get data.
*/
public function get_cached_free_listing_metrics(): array {
$value = $this->transients->get( TransientsInterface::FREE_LISTING_METRICS );
if ( $value === null ) {
$value = $this->get_free_listing_metrics();
$this->transients->set( TransientsInterface::FREE_LISTING_METRICS, $value, HOUR_IN_SECONDS * 12 );
}
return $value;
}
/**
* Get ads metrics across all campaigns.
*
* @return array Of metrics or empty if no metrics were available.
*
* @throws Exception When unable to get data.
*/
public function get_ads_metrics(): array {
if ( ! $this->options->get_ads_id() ) {
// Ads account not set up
return [];
}
// Google API requires a date clause to be set but there doesn't seem to be any limits on how wide the range
$query = ( new AdsCampaignReportQuery( [] ) )
->set_client( $this->ads_client, $this->options->get_ads_id() )
->where_date_between( self::MAX_QUERY_START_DATE, $this->get_tomorrow() )
->fields( [ 'clicks', 'conversions', 'impressions' ] );
/** @var PagedListResponse $response */
$response = $query->get_results();
$page = $response->getPage();
if ( $page && $page->getIterator()->current() ) {
/** @var GoogleAdsRow $row */
$row = $page->getIterator()->current();
$metrics = $row->getMetrics();
if ( $metrics ) {
return [
'clicks' => $metrics->getClicks(),
'conversions' => (int) $metrics->getConversions(),
'impressions' => $metrics->getImpressions(),
];
}
}
return [];
}
/**
* Get ads metrics across all campaigns but cached for 12 hours.
*
* PLEASE NOTE: These metrics will not be 100% accurate since there is no invalidation apart from the 12 hour refresh.
*
* @return array Of metrics or empty if no metrics were available.
*
* @throws Exception When unable to get data.
*/
public function get_cached_ads_metrics(): array {
$value = $this->transients->get( TransientsInterface::ADS_METRICS );
if ( $value === null ) {
$value = $this->get_ads_metrics();
$this->transients->set( TransientsInterface::ADS_METRICS, $value, HOUR_IN_SECONDS * 12 );
}
return $value;
}
/**
* Get tomorrow's date to ensure we include any metrics from the current day.
*
* @return string
*/
protected function get_tomorrow(): string {
return ( new DateTime( 'tomorrow', $this->wp->wp_timezone() ) )->format( 'Y-m-d' );
}
}