-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAdsConversionAction.php
190 lines (169 loc) · 6.29 KB
/
AdsConversionAction.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
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\API\Google;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Query\AdsConversionActionQuery;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\Ads\GoogleAdsClient;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
use Exception;
use Google\Ads\GoogleAds\V12\Common\TagSnippet;
use Google\Ads\GoogleAds\V12\Enums\ConversionActionCategoryEnum\ConversionActionCategory;
use Google\Ads\GoogleAds\V12\Enums\ConversionActionStatusEnum\ConversionActionStatus;
use Google\Ads\GoogleAds\V12\Enums\ConversionActionTypeEnum\ConversionActionType;
use Google\Ads\GoogleAds\V12\Enums\TrackingCodePageFormatEnum\TrackingCodePageFormat;
use Google\Ads\GoogleAds\V12\Enums\TrackingCodeTypeEnum\TrackingCodeType;
use Google\Ads\GoogleAds\V12\Resources\ConversionAction;
use Google\Ads\GoogleAds\V12\Resources\ConversionAction\ValueSettings;
use Google\Ads\GoogleAds\V12\Services\ConversionActionOperation;
use Google\Ads\GoogleAds\V12\Services\ConversionActionServiceClient;
use Google\Ads\GoogleAds\V12\Services\GoogleAdsRow;
use Google\Ads\GoogleAds\V12\Services\MutateConversionActionResult;
use Google\ApiCore\ApiException;
/**
* Class AdsConversionAction
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\API\Google
*/
class AdsConversionAction implements OptionsAwareInterface {
use ApiExceptionTrait;
use OptionsAwareTrait;
/**
* The Google Ads Client.
*
* @var GoogleAdsClient
*/
protected $client;
/**
* AdsConversionAction constructor.
*
* @param GoogleAdsClient $client
*/
public function __construct( GoogleAdsClient $client ) {
$this->client = $client;
}
/**
* Create the 'Google Listings and Ads purchase action' conversion action.
*
* @return array An array with some conversion action details.
* @throws Exception If the conversion action can't be created or retrieved.
*/
public function create_conversion_action(): array {
try {
$unique = sprintf( '%04x', mt_rand( 0, 0xffff ) );
$conversion_action_operation = new ConversionActionOperation();
$conversion_action_operation->setCreate(
new ConversionAction(
[
'name' => apply_filters(
'woocommerce_gla_conversion_action_name',
sprintf(
/* translators: %1 is a random 4-digit string */
__( '[%1$s] Google Listings and Ads purchase action', 'google-listings-and-ads' ),
$unique
)
),
'category' => ConversionActionCategory::PURCHASE,
'type' => ConversionActionType::WEBPAGE,
'status' => ConversionActionStatus::ENABLED,
'value_settings' => new ValueSettings(
[
'default_value' => 0,
'always_use_default_value' => false,
]
),
]
)
);
// Create the conversion.
$response = $this->client->getConversionActionServiceClient()->mutateConversionActions(
$this->options->get_ads_id(),
[ $conversion_action_operation ]
);
/** @var MutateConversionActionResult $added_conversion_action */
$added_conversion_action = $response->getResults()->offsetGet( 0 );
return $this->get_conversion_action( $added_conversion_action->getResourceName() );
} catch ( Exception $e ) {
do_action( 'woocommerce_gla_ads_client_exception', $e, __METHOD__ );
$message = $e->getMessage();
$code = $e->getCode();
if ( $e instanceof ApiException ) {
if ( $this->has_api_exception_error( $e, 'DUPLICATE_NAME' ) ) {
$message = __( 'A conversion action with this name already exists', 'google-listings-and-ads' );
} else {
$message = $e->getBasicMessage();
}
$code = $this->map_grpc_code_to_http_status_code( $e );
}
throw new Exception(
/* translators: %s Error message */
sprintf( __( 'Error creating conversion action: %s', 'google-listings-and-ads' ), $message ),
$code
);
}
}
/**
* Retrieve a Conversion Action.
*
* @param string|int $resource_name The Conversion Action to retrieve (also accepts the Conversion Action ID).
*
* @return array An array with some conversion action details.
* @throws Exception If the Conversion Action can't be retrieved.
*/
public function get_conversion_action( $resource_name ): array {
try {
// Accept IDs too
if ( is_numeric( $resource_name ) ) {
$resource_name = ConversionActionServiceClient::conversionActionName( $this->options->get_ads_id(), $resource_name );
}
$results = ( new AdsConversionActionQuery() )->set_client( $this->client, $this->options->get_ads_id() )
->where( 'conversion_action.resource_name', $resource_name, '=' )
->get_results();
// Get only the first element from results.
foreach ( $results->iterateAllElements() as $row ) {
return $this->convert_conversion_action( $row );
}
} catch ( Exception $e ) {
do_action( 'woocommerce_gla_ads_client_exception', $e, __METHOD__ );
$message = $e->getMessage();
$code = $e->getCode();
if ( $e instanceof ApiException ) {
$message = $e->getBasicMessage();
$code = $this->map_grpc_code_to_http_status_code( $e );
}
throw new Exception(
/* translators: %s Error message */
sprintf( __( 'Error retrieving conversion action: %s', 'google-listings-and-ads' ), $message ),
$code
);
}
}
/**
* Convert conversion action data to an array.
*
* @param GoogleAdsRow $row Data row returned from a query request.
*
* @return array An array with some conversion action details.
*/
private function convert_conversion_action( GoogleAdsRow $row ): array {
$conversion_action = $row->getConversionAction();
$return = [
'id' => $conversion_action->getId(),
'name' => $conversion_action->getName(),
'status' => ConversionActionStatus::name( $conversion_action->getStatus() ),
];
foreach ( $conversion_action->getTagSnippets() as $t ) {
/** @var TagSnippet $t */
if ( $t->getType() !== TrackingCodeType::WEBPAGE ) {
continue;
}
if ( $t->getPageFormat() !== TrackingCodePageFormat::HTML ) {
continue;
}
preg_match( "#send_to': '([^/]+)/([^']+)'#", $t->getEventSnippet(), $matches );
$return['conversion_id'] = $matches[1];
$return['conversion_label'] = $matches[2];
break;
}
return $return;
}
}