Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: Easy Digital Downloads integration #237

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,33 @@
use Plausible\Analytics\WP\Integrations\WooCommerce;

class Provisioning {
const CUSTOM_PROPERTIES = [
'cart_total',
'cart_total_items',
'id',
'name',
'price',
'product_id',
'product_name',
'quantity',
'shipping',
'subtotal',
'subtotal_tax',
'tax_class',
'total',
'total_tax',
'variation_id',
];

/**
* @var ClientFactory
* @var Client $client
*/
private $client_factory;
public $client;

/**
* @var Client $client
* @var ClientFactory
*/
private $client;
private $client_factory;

/**
* @var string[] $custom_event_goals
Expand Down Expand Up @@ -90,9 +108,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_funnel' ], 10, 2 );
add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_delete_goals' ], 11, 2 );
add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_delete_woocommerce_goals' ], 11, 2 );
add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_create_custom_properties' ], 11, 2 );
}

Expand Down Expand Up @@ -179,7 +195,7 @@ public function create_goal_request( $name, $type = 'CustomEvent', $currency = '
*
* @return void
*/
private function create_goals( $goals ) {
public function create_goals( $goals ) {
if ( empty( $goals ) ) {
return; // @codeCoverageIgnore
}
Expand Down Expand Up @@ -253,7 +269,7 @@ public function maybe_create_woocommerce_funnel( $old_settings, $settings ) {
* @return void
* @codeCoverageIgnore Because this method should be mocked in tests if needed.
*/
private function create_funnel( $name, $steps ) {
public function create_funnel( $name, $steps ) {
$create_request = new Client\Model\FunnelCreateRequest(
[
'funnel' => [
Expand Down Expand Up @@ -365,7 +381,7 @@ public function maybe_delete_woocommerce_goals( $old_settings, $settings ) {
* @return false|mixed
* @codeCoverageIgnore Because it can't be unit tested.
*/
private function array_search_contains( $string, $haystack ) {
public function array_search_contains( $string, $haystack ) {
if ( preg_match( '/\([A-Z]*?\)/', $string ) ) {
$string = preg_replace( '/ \([A-Z]*?\)/', '', $string );
}
Expand Down Expand Up @@ -410,8 +426,8 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
/**
* Create Custom Properties for WooCommerce integration.
*/
if ( Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) && Integrations::is_wc_active() ) {
foreach ( WooCommerce::CUSTOM_PROPERTIES as $property ) {
if ( Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) && ( Integrations::is_wc_active() || Integrations::is_edd_active() ) ) {
foreach ( self::CUSTOM_PROPERTIES as $property ) {
$properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $property ] ] );
}
}
Expand Down
110 changes: 110 additions & 0 deletions src/Admin/Provisioning/Integrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Plausible Analytics | Provisioning | Integrations
* @since 2.3.0
* @package WordPress
* @subpackage Plausible Analytics
*/

namespace Plausible\Analytics\WP\Admin\Provisioning;

use Plausible\Analytics\WP\Admin\Provisioning;

class Integrations {
/**
* @var Provisioning
*/
private $provisioning;

/**
* Build class.
*
* We use DI to prevent circular dependency.
*/
public function __construct() {
$this->provisioning = new Provisioning();

$this->init();
}

/**
* Action & filter hooks.
*
* We use Dependency Injection to prevent circular dependency.
*
* @return void
* @codeCoverageIgnore This is merely a wrapper to load classes. No need to test.
*/
private function init() {
new Integrations\WooCommerce( $this );
new Integrations\EDD( $this );
}

/**
* @param array $event_goals
* @param string $funnel_name
*
* @return void
* @codeCoverageIgnore We don't want to test the API.
*/
public function create_integration_funnel( $event_goals, $funnel_name ) {
$goals = [];

foreach ( $event_goals as $event_key => $event_goal ) {
// Don't add this goal to the funnel. Create it separately instead.
if ( $event_key === 'remove-from-cart' ) {
$this->provisioning->create_goals( [ $this->provisioning->create_goal_request( $event_goal ) ] );

continue;
}

if ( $event_key === 'purchase' ) {
if ( \Plausible\Analytics\WP\Integrations::is_edd_active() ) {
$currency = edd_get_currency();
} else {
$currency = get_woocommerce_currency();
}

$goals[] = $this->provisioning->create_goal_request( $event_goal, 'Revenue', $currency );

continue;
}

if ( $event_key === 'view-product' ) {
$path = preg_replace( '/^.*?\//', '', $event_goal );
$goals[] = $this->provisioning->create_goal_request( $event_goal, 'Pageview', null, '/' . $path );

continue;
}

$goals[] = $this->provisioning->create_goal_request( $event_goal );
}

$this->provisioning->create_funnel( $funnel_name, $goals );
}

/**
* Deletes the integration-specific goals using the stored goal IDs.
*
* @param object $integration The integration object containing event goals to be deleted.
*
* @return void
* @codeCoverageIgnore Because we don't want to test the API.
*/
public function delete_integration_goals( $integration ) {
$goals = get_option( 'plausible_analytics_enhanced_measurements_goal_ids', [] );

foreach ( $goals as $id => $name ) {
$key = $this->provisioning->array_search_contains( $name, $integration->event_goals );

if ( $key ) {
$this->provisioning->client->delete_goal( $id );

unset( $goals[ $id ] );
}
}

// Refresh the stored IDs in the DB.
update_option( 'plausible_analytics_enhanced_measurements_goal_ids', $goals );
}
}
82 changes: 82 additions & 0 deletions src/Admin/Provisioning/Integrations/EDD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Plausible Analytics | Provisioning | Integrations | EDD
* @since 2.3.0
* @package WordPress
* @subpackage Plausible Analytics
*/

namespace Plausible\Analytics\WP\Admin\Provisioning\Integrations;

use Plausible\Analytics\WP\Admin\Provisioning;
use Plausible\Analytics\WP\Helpers;
use Plausible\Analytics\WP\Integrations;

class EDD {
/**
* @var Provisioning\Integrations $integrations
*/
private $integrations;

/**
* @return void
*/
public function __construct( $integrations ) {
$this->integrations = $integrations;

$this->init();
}

/**
* Action and filter hooks.
*
* @return void
*/
private function init() {
add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_create_edd_funnel' ], 10, 2 );
add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_delete_edd_goals' ], 11, 2 );
}

/**
* Creates an EDD purchase funnel if enhanced measurement is enabled and EDD is active.
*
* @param array $old_settings The previous settings before the update.
* @param array $settings The updated settings array.
*
* @return void
*
* @codeCoverageIgnore Because it interacts with the Plugins API
*/
public function maybe_create_edd_funnel( $old_settings, $settings ) {
if ( ! Helpers::is_enhanced_measurement_enabled( 'revenue', $settings[ 'enhanced_measurements' ] ) || ! Integrations::is_edd_active() ) {
return; // @codeCoverageIgnore
}

$edd = new Integrations\EDD( false );

$this->integrations->create_integration_funnel( $edd->event_goals, __( 'EDD Purchase Funnel', 'plausible-analytics' ) );
}

/**
* * Delete all custom EDD event goals if Revenue setting is disabled. The funnel is deleted when the minimum
* * required no. of goals is no longer met.
*
* @param array $old_settings The previous settings before the update.
* @param array $settings The current updated settings.
*
* @return void
*
* @codeCoverageIgnore Because it interacts with the Plugins API.
*/
public function maybe_delete_edd_goals( $old_settings, $settings ) {
$enhanced_measurements = array_filter( $settings[ 'enhanced_measurements' ] );

if ( Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) ) {
return;
}

$edd_integration = new Integrations\EDD( false );

$this->integrations->delete_integration_goals( $edd_integration );
}
}
84 changes: 84 additions & 0 deletions src/Admin/Provisioning/Integrations/WooCommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Plausible Analytics | Provisioning | Integrations | WooCommerce
* @since 2.3.0
* @package WordPress
* @subpackage Plausible Analytics
*/

namespace Plausible\Analytics\WP\Admin\Provisioning\Integrations;

use Plausible\Analytics\WP\Admin\Provisioning;
use Plausible\Analytics\WP\Helpers;
use Plausible\Analytics\WP\Integrations;

class WooCommerce {
/**
* @var Provisioning\Integrations $integrations
*/
private $integrations;

/**
* Build class.
*/
public function __construct( $integrations ) {
$this->integrations = $integrations;

$this->init();
}

/**
* Action & filters hooks.
*
* @return void
*/
private function init() {
add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_create_woocommerce_funnel' ], 10, 2 );
add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_delete_woocommerce_goals' ], 11, 2 );
}

/**
* Checks whether the WooCommerce funnel should be created based on the provided settings
* and creates the funnel if the conditions are met.
*
* @param array $old_settings The previous settings before the update.
* @param array $settings The updated settings to check for enhanced measurement and WooCommerce integration.
*
* @return void
*
* @codeCoverageIgnore Because it interacts with the Plugins API.
*/
public function maybe_create_woocommerce_funnel( $old_settings, $settings ) {
if ( ! Helpers::is_enhanced_measurement_enabled( 'revenue', $settings[ 'enhanced_measurements' ] ) || ! Integrations::is_wc_active() ) {
return; // @codeCoverageIgnore
}

$woocommerce = new Integrations\WooCommerce( false );

$this->integrations->create_integration_funnel( $woocommerce->event_goals, __( 'Woo Purchase Funnel', 'plausible-analytics' ) );
}

/**
* Delete all custom WooCommerce event goals if Revenue setting is disabled. The funnel is deleted when the minimum
* required no. of goals is no longer met.
*
* @param $old_settings
* @param $settings
*
* @return void
*
* @codeCoverageIgnore Because we don't want to test if the API is working.
*/
public function maybe_delete_woocommerce_goals( $old_settings, $settings ) {
$enhanced_measurements = array_filter( $settings[ 'enhanced_measurements' ] );

// Setting is enabled, no need to continue.
if ( Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) ) {
return;
}

$woo_integration = new Integrations\WooCommerce( false );

$this->integrations->delete_integration_goals( $woo_integration );
}
}
8 changes: 3 additions & 5 deletions src/Integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ private function init() {

// Easy Digital Downloads
if ( self::is_edd_active() ) {
// new Integrations\EDD();
new Integrations\EDD();
}

// Form Plugins
if ( self::is_form_submit_active() ) {
new Integrations\FormSubmit();
}
Expand All @@ -60,9 +61,6 @@ public static function is_edd_active() {
* @return mixed|null
*/
public static function is_form_submit_active() {
return apply_filters(
'plausible_analytics_integrations_form_submit',
Helpers::is_enhanced_measurement_enabled( 'form-completions' )
);
return apply_filters( 'plausible_analytics_integrations_form_submit', Helpers::is_enhanced_measurement_enabled( 'form-completions' ) );
}
}
Loading