From 1033128a4caf80570687254c7f081641a76bbc0f Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 15:45:07 +0200 Subject: [PATCH] Added auto-provisioning for Funnel. --- src/Admin/Provisioning.php | 52 ++++++++++++++++---- src/Client.php | 17 +++++++ src/Integrations/WooCommerce.php | 8 +-- tests/integration/Admin/ProvisioningTest.php | 4 +- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php index 8889770f..a7ebfe17 100644 --- a/src/Admin/Provisioning.php +++ b/src/Admin/Provisioning.php @@ -12,6 +12,8 @@ use Plausible\Analytics\WP\Client; use Plausible\Analytics\WP\Client\ApiException; use Plausible\Analytics\WP\Client\Model\GoalCreateRequestCustomEvent; +use Plausible\Analytics\WP\Client\Model\GoalCreateRequestPageview; +use Plausible\Analytics\WP\Client\Model\GoalCreateRequestRevenue; use Plausible\Analytics\WP\Helpers; use Plausible\Analytics\WP\Integrations; use Plausible\Analytics\WP\Integrations\WooCommerce; @@ -89,7 +91,7 @@ private function init() { add_action( 'update_option_plausible_analytics_settings', [ $this, 'create_shared_link' ], 10, 2 ); add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_create_goals' ], 10, 2 ); - add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_create_woocommerce_goals' ], 10, 2 ); + add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_create_woocommerce_funnels' ], 10, 2 ); add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_delete_goals' ], 11, 2 ); add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_create_custom_properties' ], 11, 2 ); } @@ -145,13 +147,13 @@ public function maybe_create_goals( $old_settings, $settings ) { continue; // @codeCoverageIgnore } - $goals[] = $this->create_request_custom_event( $this->custom_event_goals[ $measurement ] ); + $goals[] = $this->create_goal_request( $this->custom_event_goals[ $measurement ] ); if ( $measurement === 'search' ) { global $wp_rewrite; $search_url = str_replace( '%search%', '', $wp_rewrite->get_search_permastruct() ); - $goals[] = $this->create_request_custom_event( null, 'Pageview', '', $search_url ); + $goals[] = $this->create_goal_request( null, 'Pageview', '', $search_url ); } } @@ -163,9 +165,9 @@ public function maybe_create_goals( $old_settings, $settings ) { * @param string $type CustomEvent|Revenue|Pageview * @param string $currency Required if $type is Revenue * - * @return GoalCreateRequestCustomEvent + * @return GoalCreateRequestCustomEvent|GoalCreateRequestPageview|GoalCreateRequestRevenue */ - private function create_request_custom_event( $name, $type = 'CustomEvent', $currency = '', $path = '' ) { + private function create_goal_request( $name, $type = 'CustomEvent', $currency = '', $path = '' ) { $props = [ 'goal' => [ 'event_name' => $name, @@ -183,7 +185,14 @@ private function create_request_custom_event( $name, $type = 'CustomEvent', $cur $props[ 'goal' ][ 'path' ] = $path; } - return new Client\Model\GoalCreateRequestCustomEvent( $props ); + switch ( $type ) { + case 'Pageview': + return new Client\Model\GoalCreateRequestPageview( $props ); + case 'Revenue': + return new Client\Model\GoalCreateRequestRevenue( $props ); + default: // CustomEvent + return new Client\Model\GoalCreateRequestCustomEvent( $props ); + } } /** @@ -223,7 +232,7 @@ private function create_goals( $goals ) { * * @return void */ - public function maybe_create_woocommerce_goals( $old_settings, $settings ) { + public function maybe_create_woocommerce_funnels( $old_settings, $settings ) { if ( ! Helpers::is_enhanced_measurement_enabled( 'revenue', $settings[ 'enhanced_measurements' ] ) || ! Integrations::is_wc_active() ) { return; // @codeCoverageIgnore } @@ -233,15 +242,38 @@ public function maybe_create_woocommerce_goals( $old_settings, $settings ) { foreach ( $woocommerce->event_goals as $event_key => $event_goal ) { if ( $event_key === 'purchase' ) { - $goals[] = $this->create_request_custom_event( $event_goal, 'Revenue', get_woocommerce_currency() ); + $goals[] = $this->create_goal_request( $event_goal, 'Revenue', get_woocommerce_currency() ); continue; } - $goals[] = $this->create_request_custom_event( $event_goal ); + $goals[] = $this->create_goal_request( $event_goal ); } - $this->create_goals( $goals ); + $this->create_funnel( __( 'Woo Purchase Funnel', 'plausible-analytics' ), $goals ); + } + + /** + * Creates a funnel and creates goals if they don't exist. + * + * @param $name + * @param $steps + * + * @return void + * + * @codeCoverageIgnore Because this method should be mocked in tests. + */ + private function create_funnel( $name, $steps ) { + $create_request = new Client\Model\FunnelCreateRequest( + [ + 'funnel' => [ + 'name' => $name, + 'steps' => $steps, + ], + ] + ); + + $this->client->create_funnel( $create_request ); } /** diff --git a/src/Client.php b/src/Client.php index dadf3cef..c04c9625 100644 --- a/src/Client.php +++ b/src/Client.php @@ -232,6 +232,23 @@ public function create_goals( $goals ) { } } + /** + * Allows creating Funnels in bulk. + * + * @param \Plausible\Analytics\WP\Client\Model\FunnelCreateRequest $funnel + * + * @return Client\Model\Funnel|PaymentRequiredError|UnauthorizedError|UnprocessableEntityError|void + * + * @codeCoverageIgnore + */ + public function create_funnel( $funnel ) { + try { + return $this->api_instance->plausibleWebPluginsAPIControllersFunnelsCreate( $funnel ); + } catch ( Exception $e ) { + $this->send_json_error( $e, __( 'Something went wrong while creating Funnel: %s', 'plausible-analytics' ) ); + } + } + /** * Delete a Custom Event Goal by ID. * diff --git a/src/Integrations/WooCommerce.php b/src/Integrations/WooCommerce.php index d0b5c6b9..0dcf9d4d 100644 --- a/src/Integrations/WooCommerce.php +++ b/src/Integrations/WooCommerce.php @@ -47,10 +47,10 @@ class WooCommerce { */ public function __construct( $init = true ) { $this->event_goals = [ - 'add-to-cart' => __( 'Add Item To Cart', 'plausible-analytics' ), - 'remove-from-cart' => __( 'Remove Cart Item', 'plausible-analytics' ), - 'checkout' => __( 'Entered Checkout', 'plausible-analytics' ), - 'purchase' => __( 'Purchase', 'plausible-analytics' ), + 'add-to-cart' => __( 'Woo Add to Cart', 'plausible-analytics' ), + 'remove-from-cart' => __( 'Woo Remove from Cart', 'plausible-analytics' ), + 'checkout' => __( 'Woo Start Checkout', 'plausible-analytics' ), + 'purchase' => __( 'Woo Complete Purchase', 'plausible-analytics' ), ]; $this->init( $init ); diff --git a/tests/integration/Admin/ProvisioningTest.php b/tests/integration/Admin/ProvisioningTest.php index 72d38910..160275da 100644 --- a/tests/integration/Admin/ProvisioningTest.php +++ b/tests/integration/Admin/ProvisioningTest.php @@ -105,7 +105,7 @@ public function testCreateGoals() { } /** - * @see Provisioning::maybe_create_woocommerce_goals() + * @see Provisioning::maybe_create_woocommerce_funnels() * @return void * @throws ApiException */ @@ -153,7 +153,7 @@ public function testCreateWooCommerceGoals() { add_filter( 'plausible_analytics_integrations_woocommerce', '__return_true' ); when( 'get_woocommerce_currency' )->justReturn( 'EUR' ); - $class->maybe_create_woocommerce_goals( [], $settings ); + $class->maybe_create_woocommerce_funnels( [], $settings ); remove_filter( 'plausible_analytics_integrations_woocommerce', '__return_true' );