Skip to content

Commit

Permalink
Added auto-provisioning for Funnel.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan0sz committed Jun 7, 2024
1 parent c5345d7 commit 1033128
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
52 changes: 42 additions & 10 deletions src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
}
}

Expand All @@ -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,
Expand All @@ -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 );
}
}

/**
Expand Down Expand Up @@ -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
}
Expand All @@ -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 );
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 4 additions & 4 deletions src/Integrations/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/Admin/ProvisioningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testCreateGoals() {
}

/**
* @see Provisioning::maybe_create_woocommerce_goals()
* @see Provisioning::maybe_create_woocommerce_funnels()
* @return void
* @throws ApiException
*/
Expand Down Expand Up @@ -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' );

Expand Down

0 comments on commit 1033128

Please sign in to comment.