PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/google-listings-and-ads/src/API/Google/AdsConversionAction.php

https://gitlab.com/remyvianne/krowkaramel
PHP | 177 lines | 115 code | 19 blank | 43 comment | 7 complexity | cf2b7258f4a17544e571e6aea6430c3c MD5 | raw file
  1. <?php
  2. declare( strict_types=1 );
  3. namespace Automattic\WooCommerce\GoogleListingsAndAds\API\Google;
  4. use Automattic\WooCommerce\GoogleListingsAndAds\Google\Ads\GoogleAdsClient;
  5. use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface;
  6. use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
  7. use Exception;
  8. use Google\Ads\GoogleAds\V9\Resources\ConversionAction as ConversionAction;
  9. use Google\Ads\GoogleAds\V9\Common\TagSnippet;
  10. use Google\Ads\GoogleAds\V9\Enums\ConversionActionCategoryEnum\ConversionActionCategory;
  11. use Google\Ads\GoogleAds\V9\Enums\ConversionActionStatusEnum\ConversionActionStatus;
  12. use Google\Ads\GoogleAds\V9\Enums\ConversionActionTypeEnum\ConversionActionType;
  13. use Google\Ads\GoogleAds\V9\Enums\TrackingCodePageFormatEnum\TrackingCodePageFormat;
  14. use Google\Ads\GoogleAds\V9\Enums\TrackingCodeTypeEnum\TrackingCodeType;
  15. use Google\Ads\GoogleAds\V9\Resources\ConversionAction\ValueSettings;
  16. use Google\Ads\GoogleAds\V9\Services\ConversionActionOperation;
  17. use Google\Ads\GoogleAds\V9\Services\ConversionActionServiceClient;
  18. use Google\Ads\GoogleAds\V9\Services\MutateConversionActionResult;
  19. use Google\ApiCore\ApiException;
  20. /**
  21. * Class AdsConversionAction
  22. *
  23. * @package Automattic\WooCommerce\GoogleListingsAndAds\API\Google
  24. */
  25. class AdsConversionAction implements OptionsAwareInterface {
  26. use ApiExceptionTrait;
  27. use OptionsAwareTrait;
  28. /**
  29. * The Google Ads Client.
  30. *
  31. * @var GoogleAdsClient
  32. */
  33. protected $client;
  34. /**
  35. * AdsConversionAction constructor.
  36. *
  37. * @param GoogleAdsClient $client
  38. */
  39. public function __construct( GoogleAdsClient $client ) {
  40. $this->client = $client;
  41. }
  42. /**
  43. * Create the 'Google Listings and Ads purchase action' conversion action.
  44. *
  45. * @return array An array with some conversion action details.
  46. * @throws Exception If the conversion action can't be created or retrieved.
  47. */
  48. public function create_conversion_action(): array {
  49. try {
  50. $unique = sprintf( '%04x', mt_rand( 0, 0xffff ) );
  51. $conversion_action_operation = new ConversionActionOperation();
  52. $conversion_action_operation->setCreate(
  53. new ConversionAction(
  54. [
  55. 'name' => apply_filters(
  56. 'woocommerce_gla_conversion_action_name',
  57. sprintf(
  58. /* translators: %1 is a random 4-digit string */
  59. __( '[%1$s] Google Listings and Ads purchase action', 'google-listings-and-ads' ),
  60. $unique
  61. )
  62. ),
  63. 'category' => ConversionActionCategory::PURCHASE,
  64. 'type' => ConversionActionType::WEBPAGE,
  65. 'status' => ConversionActionStatus::ENABLED,
  66. 'value_settings' => new ValueSettings(
  67. [
  68. 'default_value' => 0,
  69. 'always_use_default_value' => false,
  70. ]
  71. ),
  72. ]
  73. )
  74. );
  75. // Create the conversion.
  76. $response = $this->client->getConversionActionServiceClient()->mutateConversionActions(
  77. $this->options->get_ads_id(),
  78. [ $conversion_action_operation ]
  79. );
  80. /** @var MutateConversionActionResult $added_conversion_action */
  81. $added_conversion_action = $response->getResults()->offsetGet( 0 );
  82. return $this->get_conversion_action( $added_conversion_action->getResourceName() );
  83. } catch ( Exception $e ) {
  84. do_action( 'woocommerce_gla_ads_client_exception', $e, __METHOD__ );
  85. $message = $e->getMessage();
  86. if ( $e instanceof ApiException ) {
  87. if ( $this->has_api_exception_error( $e, 'DUPLICATE_NAME' ) ) {
  88. $message = __( 'A conversion action with this name already exists', 'google-listings-and-ads' );
  89. } else {
  90. $message = $e->getBasicMessage();
  91. }
  92. }
  93. throw new Exception(
  94. /* translators: %s Error message */
  95. sprintf( __( 'Error creating conversion action: %s', 'google-listings-and-ads' ), $message ),
  96. $e->getCode()
  97. );
  98. }
  99. }
  100. /**
  101. * Retrieve a Conversion Action.
  102. *
  103. * @param string|int $resource_name The Conversion Action to retrieve (also accepts the Conversion Action ID).
  104. *
  105. * @return array An array with some conversion action details.
  106. * @throws Exception If the Conversion Action can't be retrieved.
  107. */
  108. public function get_conversion_action( $resource_name ): array {
  109. try {
  110. // Accept IDs too
  111. if ( is_numeric( $resource_name ) ) {
  112. $resource_name = ConversionActionServiceClient::conversionActionName( $this->options->get_ads_id(), $resource_name );
  113. }
  114. $ca_client = $this->client->getConversionActionServiceClient();
  115. $conversion_action = $ca_client->getConversionAction( $resource_name );
  116. return $this->convert_conversion_action( $conversion_action );
  117. } catch ( Exception $e ) {
  118. do_action( 'woocommerce_gla_ads_client_exception', $e, __METHOD__ );
  119. $message = $e->getMessage();
  120. if ( $e instanceof ApiException ) {
  121. $message = $e->getBasicMessage();
  122. }
  123. throw new Exception(
  124. /* translators: %s Error message */
  125. sprintf( __( 'Error retrieving conversion action: %s', 'google-listings-and-ads' ), $message ),
  126. $e->getCode()
  127. );
  128. }
  129. }
  130. /**
  131. * Convert conversion action data to an array.
  132. *
  133. * @param ConversionAction $conversion_action
  134. *
  135. * @return array An array with some conversion action details.
  136. */
  137. private function convert_conversion_action( ConversionAction $conversion_action ): array {
  138. $return = [
  139. 'id' => $conversion_action->getId(),
  140. 'name' => $conversion_action->getName(),
  141. 'status' => ConversionActionStatus::name( $conversion_action->getStatus() ),
  142. ];
  143. foreach ( $conversion_action->getTagSnippets() as $t ) {
  144. /** @var TagSnippet $t */
  145. if ( $t->getType() !== TrackingCodeType::WEBPAGE ) {
  146. continue;
  147. }
  148. if ( $t->getPageFormat() !== TrackingCodePageFormat::HTML ) {
  149. continue;
  150. }
  151. preg_match( "#send_to': '([^/]+)/([^']+)'#", $t->getEventSnippet(), $matches );
  152. $return['conversion_id'] = $matches[1];
  153. $return['conversion_label'] = $matches[2];
  154. break;
  155. }
  156. return $return;
  157. }
  158. }