Add PayPalInsightsLoader for Axo Block

This commit is contained in:
Daniel Dudzic 2024-10-23 18:45:33 +02:00
parent 654fcfc521
commit c89da49e8f
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
2 changed files with 77 additions and 0 deletions

View file

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

View file

@ -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' );
}
}