Home / Admin / WP Simple Pay: Google GA4 Payment Conversion Tracking
Duplicate Snippet

Embed Snippet on Your Site

WP Simple Pay: Google GA4 Payment Conversion Tracking

Execute a script the first and only time a payment confirmation is viewed.

Code Preview
php
<?php
<?php
/**
 * @link https://library.wpcode.com/snippet/j57gxn45/
 */
add_action(
'simpay_payment_receipt_viewed',
/**
	 * Runs the first time the payment confirmation page is viewed.
	 * 
	 * @param array<string, mixed> $payment_confirmation_data
	 */
function( $payment_confirmation_data ) {
// Payment customer data (not used in this example).
$customer = $payment_confirmation_data['customer'];
// One-time payment data.
$payment = current( $payment_confirmation_data['paymentintents'] );
		printf(
'<script>
				gtag("event", "purchase", {
					transaction_id: "%s",
					value: %s,
					currency: "%s"
				} );
			</script>',
$payment->id,
$payment->amount / 100,
$payment->currency
		);
	}
);
// From: https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtag#make_a_purchase_or_issue_a_refund

Comments

Add a Comment