diff --git a/modules/ppcp-axo-block/resources/js/plugins/PayPalInsightsLoader.js b/modules/ppcp-axo-block/resources/js/plugins/PayPalInsightsLoader.js new file mode 100644 index 000000000..9f114593b --- /dev/null +++ b/modules/ppcp-axo-block/resources/js/plugins/PayPalInsightsLoader.js @@ -0,0 +1,35 @@ +import { registerPlugin } from '@wordpress/plugins'; +import { useSelect } from '@wordpress/data'; +import { useEffect, useState } from '@wordpress/element'; +import PayPalInsights from '../../../../ppcp-axo/resources/js/Insights/PayPalInsights'; + +const PayPalInsightsLoader = () => { + + // Subscribe to checkout store data + const checkoutData = useSelect( ( select ) => { + return { + paymentMethod: select( 'wc/store/checkout' ).getPaymentMethod(), + orderTotal: select( 'wc/store/checkout' ).getOrderTotal(), + // Add other data points you need + }; + } ); + + // Watch for changes and trigger analytics events + useEffect( () => { + if ( isScriptLoaded && window.YourAnalyticsObject ) { + // Example tracking calls + window.YourAnalyticsObject.track( 'checkout_updated', { + payment_method: checkoutData.paymentMethod, + order_total: checkoutData.orderTotal, + } ); + } + }, [ isScriptLoaded, checkoutData ] ); + + return null; // This component doesn't render anything +}; + +// Register the plugin to run with the checkout block +registerPlugin( 'wc-ppcp-paypal-insights', { + render: PayPalInsightsLoader, + scope: 'woocommerce-checkout', +} ); diff --git a/modules/ppcp-axo-block/src/AxoBlockModule.php b/modules/ppcp-axo-block/src/AxoBlockModule.php index 669cc7cc5..2a7ee73b6 100644 --- a/modules/ppcp-axo-block/src/AxoBlockModule.php +++ b/modules/ppcp-axo-block/src/AxoBlockModule.php @@ -133,6 +133,15 @@ class AxoBlockModule implements ServiceModule, ExtendingModule, ExecutableModule wp_enqueue_style( 'wc-ppcp-axo-block' ); } ); + + // Enqueue the PayPal Insights script + add_action( + 'wp_enqueue_scripts', + function () use ($c) { + $this->enqueue_paypal_insights_script($c); + } + ); + return true; } @@ -166,4 +175,37 @@ class AxoBlockModule implements ServiceModule, ExtendingModule, ExecutableModule return $localized_script_data; } + + /** + * Enqueues PayPal Insights analytics script for the Checkout block. + * + * @param ContainerInterface $c The service container. + * @return void + */ + private function enqueue_paypal_insights_script( ContainerInterface $c ): void { + if ( ! has_block( 'woocommerce/checkout' ) ) { + return; + } + + $module_url = $c->get( 'axoblock.url' ); + $asset_version = $c->get( 'ppcp.asset-version' ); + + wp_register_script( + 'wc-ppcp-paypal-insights', + untrailingslashit( $module_url ) . '/resources/js/plugins/PayPalInsights.js', + array( 'wp-plugins', 'wp-data', 'wp-element', 'wc-blocks-registry' ), + $asset_version, + true + ); + + wp_localize_script( + 'wc-ppcp-paypal-insights', + 'ppcpPayPalInsightsData', + array( + 'isAxoEnabled' => true, + ) + ); + + wp_enqueue_script( 'wc-ppcp-paypal-insights' ); + } }