Skip to content

Commit

Permalink
Fixed: re-factored add_to_cart_on_product_page so quantity and other …
Browse files Browse the repository at this point in the history
…properties are properly sent.
  • Loading branch information
Dan0sz committed May 30, 2024
1 parent 9a85fad commit 93444fd
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 132 deletions.
113 changes: 94 additions & 19 deletions src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

use Plausible\Analytics\WP\Client;
use Plausible\Analytics\WP\Client\ApiException;
use Plausible\Analytics\WP\Client\Model\GoalCreateRequestCustomEvent;
use Plausible\Analytics\WP\Helpers;
use Plausible\Analytics\WP\Integrations;
use Plausible\Analytics\WP\Integrations\WooCommerce;

class Provisioning {
/**
Expand All @@ -21,11 +25,7 @@ class Provisioning {
/**
* @var string[] $custom_event_goals
*/
private $custom_event_goals = [
'404' => '404',
'outbound-links' => 'Outbound Link: Click',
'file-downloads' => 'File Download',
];
private $custom_event_goals = [];

/**
* @var string[] $custom_pageview_properties
Expand Down Expand Up @@ -59,6 +59,12 @@ public function __construct( $client = null ) {
$this->client = new Client();
}

$this->custom_event_goals = [
'404' => __( '404', 'plausible-analytics' ),
'outbound-links' => __( 'Outbound Link: Click', 'plausible-analytics' ),
'file-downloads' => __( 'File Download', 'plausible-analytics' ),
];

$this->init();
}

Expand All @@ -76,7 +82,8 @@ private function init() {
}

add_action( 'update_option_plausible_analytics_settings', [ $this, 'create_shared_link' ], 10, 2 );
add_action( 'update_option_plausible_analytics_settings', [ $this, 'create_goals' ], 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_delete_goals' ], 11, 2 );
add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_create_custom_properties' ], 11, 2 );
}
Expand Down Expand Up @@ -117,36 +124,62 @@ public function create_shared_link( $old_settings, $settings ) {
* @param $old_settings
* @param $settings
*/
public function create_goals( $old_settings, $settings ) {
public function maybe_create_goals( $old_settings, $settings ) {
$enhanced_measurements = array_filter( $settings[ 'enhanced_measurements' ] );

if ( empty( $enhanced_measurements ) ) {
return; // @codeCoverageIgnore
}

$custom_event_keys = array_keys( $this->custom_event_goals );
$create_request = new Client\Model\GoalCreateRequestBulkGetOrCreate();
$goals = [];

foreach ( $enhanced_measurements as $measurement ) {
if ( ! in_array( $measurement, $custom_event_keys ) ) {
continue; // @codeCoverageIgnore
}

$goals[] = new Client\Model\GoalCreateRequestCustomEvent(
[
'goal' => [
'event_name' => $this->custom_event_goals[ $measurement ],
],
'goal_type' => 'Goal.CustomEvent',
]
);
$goals[] = $this->create_request_custom_event( $this->custom_event_goals[ $measurement ] );
}

$this->create_goals( $goals );
}

/**
* @param string $name Event Name
* @param string $type CustomEvent|Revenue|Pageview
* @param string $currency Required if $type is Revenue
*
* @return GoalCreateRequestCustomEvent
*/
private function create_request_custom_event( $name, $type = 'CustomEvent', $currency = '' ) {
$props = [
'goal' => [
'event_name' => $name,
],
'goal_type' => "Goal.$type",
];

if ( $type === 'Revenue' ) {
$props[ 'goal' ][ 'currency' ] = $currency;
}

return new Client\Model\GoalCreateRequestCustomEvent( $props );
}

/**
* Create the goals using the API client and updates the IDs in the database.
*
* @param $goals
*
* @return void
*/
private function create_goals( $goals ) {
if ( empty( $goals ) ) {
return; // @codeCoverageIgnore
}

$create_request = new Client\Model\GoalCreateRequestBulkGetOrCreate();
$create_request->setGoals( $goals );
$response = $this->client->create_goals( $create_request );

Expand All @@ -165,6 +198,33 @@ public function create_goals( $old_settings, $settings ) {
}
}

/**
* @param $old_settings
* @param $settings
*
* @return void
*/
public function maybe_create_woocommerce_goals( $old_settings, $settings ) {
if ( ! Helpers::is_enhanced_measurement_enabled( 'revenue', $settings[ 'enhanced_measurements' ] ) || ! Integrations::is_wc_active() ) {
return;
}

$goals = [];
$woocommerce = new WooCommerce( false );

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() );

continue;
}

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

$this->create_goals( $goals );
}

/**
* Delete Custom Event Goals when an Enhanced Measurement is disabled.
*
Expand Down Expand Up @@ -206,15 +266,30 @@ public function maybe_delete_goals( $old_settings, $settings ) {
public function maybe_create_custom_properties( $old_settings, $settings ) {
$enhanced_measurements = $settings[ 'enhanced_measurements' ];

if ( ! in_array( 'pageview-props', $enhanced_measurements ) ) {
if ( ! Helpers::is_enhanced_measurement_enabled( 'pageview-props', $enhanced_measurements ) &&
! Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) ) {
return; // @codeCoverageIgnore
}

$create_request = new Client\Model\CustomPropEnableRequestBulkEnable();
$properties = [];

foreach ( $this->custom_pageview_properties as $property ) {
$properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $property ] ] );
/**
* Enable Custom Properties for Authors & Categories option.
*/
if ( Helpers::is_enhanced_measurement_enabled( 'pageview-props', $enhanced_measurements ) ) {
foreach ( $this->custom_pageview_properties as $property ) {
$properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $property ] ] );
}
}

/**
* 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 ) {
$properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $property ] ] );
}
}

$create_request->setCustomProps( $properties );
Expand Down
39 changes: 0 additions & 39 deletions src/ECommerce.php

This file was deleted.

32 changes: 31 additions & 1 deletion src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,20 @@ public static function get_filename( $local = false ) {
}

foreach ( [ 'outbound-links', 'file-downloads', 'tagged-events', 'revenue', 'pageview-props', 'compat', 'hash' ] as $extension ) {
if ( is_array( $settings[ 'enhanced_measurements' ] ) && in_array( $extension, $settings[ 'enhanced_measurements' ], true ) ) {
if ( self::is_enhanced_measurement_enabled( $extension ) ) {
$file_name .= '.' . $extension;
}
}

/**
* Custom Events needs to be enabled, if Revenue Tracking is enabled and any of the available integrations are available.
*/
if ( ! self::is_enhanced_measurement_enabled( 'tagged-events' ) &&
self::is_enhanced_measurement_enabled( 'revenue' ) &&
( Integrations::is_wc_active() || Integrations::is_edd_active() ) ) {
$file_name .= '.' . 'tagged-events';
}

// Load exclusions.js if any excluded pages are set.
if ( ! empty( $settings[ 'excluded_pages' ] ) ) {
$file_name .= '.' . 'exclusions';
Expand Down Expand Up @@ -169,6 +178,27 @@ public static function get_proxy_resources() {
return $resources;
}

/**
* Check if a certain Enhanced Measurement is enabled.
*
* @param string $name Name of the option to check, valid values are
* 404|outbound-links|file-downloads|tagged-events|revenue|pageview-props|hash|compat.
* @param array $enhanced_measurements Allows checking against a different set of options.
*
* @return bool
*/
public static function is_enhanced_measurement_enabled( $name, $enhanced_measurements = [] ) {
if ( empty( $enhanced_measurements ) ) {
$enhanced_measurements = Helpers::get_settings()[ 'enhanced_measurements' ];
}

if ( ! is_array( $enhanced_measurements ) ) {
return false;
}

return in_array( $name, $enhanced_measurements );
}

/**
* Returns the URL of the domain where Plausible Analytics is hosted: self-hosted or cloud.
*
Expand Down
57 changes: 57 additions & 0 deletions src/Integrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* Plausible Analytics | Integrations
*
* @since 2.1.0
* @package WordPress
* @subpackage Plausible Analytics
*/

namespace Plausible\Analytics\WP;

class Integrations {
const SCRIPT_WRAPPER = '<script defer id="plausible-analytics-integration-tracking">document.addEventListener("DOMContentLoaded", () => { %s });</script>';

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

/**
* Run available integrations.
*
* @return void
*/
private function init() {
// WooCommerce
if ( self::is_wc_active() ) {
new Integrations\WooCommerce();
}

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

/**
* Checks if WooCommerce is installed and activated.
*
* @return bool
*/
public static function is_wc_active() {
return function_exists( 'WC' );
}

/**
* Checks if Easy Digital Downloads is installed and activated.
*
* @return bool
*/
public static function is_edd_active() {
return function_exists( 'EDD' );
}
}
Loading

0 comments on commit 93444fd

Please sign in to comment.