Skip to content

Commit bbc2e9c

Browse files
committed
Fixed: use JS script to track complete purchase event.
1 parent 5d759c9 commit bbc2e9c

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

src/Integrations.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
namespace Plausible\Analytics\WP;
1111

1212
class Integrations {
13-
const SCRIPT_WRAPPER = '<script defer id="plausible-analytics-integration-tracking">document.addEventListener("DOMContentLoaded", () => { %s });</script>';
13+
const PURCHASE_TRACKED_META_KEY = '_plausible_analytics_purchase_tracked';
14+
15+
const SCRIPT_WRAPPER = '<script defer id="plausible-analytics-integration-tracking">document.addEventListener("DOMContentLoaded", () => { %s });</script>';
1416

1517
/**
1618
* Build class.

src/Integrations/EDD.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Plausible\Analytics\WP\Integrations;
1111

12+
use Plausible\Analytics\WP\Integrations;
1213
use Plausible\Analytics\WP\Proxy;
1314

1415
/**
@@ -57,7 +58,7 @@ private function init( $init ) {
5758
add_action( 'edd_post_add_to_cart', [ $this, 'track_add_to_cart' ], 10, 3 );
5859
add_action( 'edd_pre_remove_from_cart', [ $this, 'track_remove_cart_item' ], 10 );
5960
add_action( 'edd_before_purchase_form', [ $this, 'track_entered_checkout' ] );
60-
add_action( 'edd_complete_purchase', [ $this, 'track_purchase' ], 10, 2 );
61+
add_action( 'wp_head', [ $this, 'track_purchase' ] );
6162
}
6263

6364
/**
@@ -173,26 +174,44 @@ public function track_entered_checkout() {
173174
}
174175

175176
/**
176-
* Tracks the "purchase" event with relevant order and payment data.
177+
* Tracks the "purchase" event with relevant payment data.
177178
*
178-
* @param int $order_id The unique identifier of the order.
179-
* @param object $payment The payment object containing details like total amount and currency.
179+
* We choose to render a JS script, instead of hooking into an action, because user-agent information
180+
* gets lost when the user is first redirected to a Payment Provider.
180181
*
181182
* @return void
182183
*/
183-
public function track_purchase( $order_id, $payment ) {
184-
$props = apply_filters(
185-
'plausible_analytics_edd_purchase_custom_properties',
186-
[
187-
'revenue' => [
188-
'amount' => $payment->total,
189-
'currency' => $payment->currency,
190-
],
191-
]
184+
public function track_purchase() {
185+
if ( ! edd_is_success_page() ) {
186+
return; // @codeCoverageIgnore
187+
}
188+
189+
$session = edd_get_purchase_session();
190+
$order = null;
191+
192+
if ( ! empty( $session[ 'purchase_key' ] ) ) {
193+
$order = edd_get_order_by( 'payment_key', $session[ 'purchase_key' ] );
194+
}
195+
196+
if ( ! $order || edd_get_order_meta( $order->id, Integrations::PURCHASE_TRACKED_META_KEY, true ) ) {
197+
return;
198+
}
199+
200+
$props = wp_json_encode(
201+
apply_filters(
202+
'plausible_analytics_edd_purchase_custom_properties',
203+
[
204+
'revenue' => [
205+
'amount' => $order->total,
206+
'currency' => $order->currency,
207+
],
208+
]
209+
)
192210
);
211+
$label = $this->event_goals[ 'purchase' ];
193212

194-
$proxy = new Proxy( false );
213+
echo sprintf( Integrations::SCRIPT_WRAPPER, "window.plausible( '$label', $props )" );
195214

196-
$proxy->do_request( $this->event_goals[ 'purchase' ], null, edd_get_checkout_uri(), $props );
215+
edd_add_order_meta( $order->id, Integrations::PURCHASE_TRACKED_META_KEY, true );
197216
}
198217
}

src/Integrations/WooCommerce.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use WC_Product;
1717

1818
class WooCommerce {
19-
const PURCHASE_TRACKED_META_KEY = '_plausible_analytics_purchase_tracked';
20-
2119
/**
2220
* @var array Custom Event Goals used to track Events in WooCommerce.
2321
*/
@@ -324,7 +322,7 @@ public function track_entered_checkout() {
324322
*/
325323
public function track_purchase( $order_id ) {
326324
$order = wc_get_order( $order_id );
327-
$is_tracked = $order->get_meta( self::PURCHASE_TRACKED_META_KEY );
325+
$is_tracked = $order->get_meta( Integrations::PURCHASE_TRACKED_META_KEY );
328326

329327
if ( $is_tracked ) {
330328
return; // @codeCoverageIgnore
@@ -339,7 +337,7 @@ public function track_purchase( $order_id ) {
339337

340338
echo sprintf( Integrations::SCRIPT_WRAPPER, "window.plausible( '$label', $props )" );
341339

342-
$order->add_meta_data( self::PURCHASE_TRACKED_META_KEY, true );
340+
$order->add_meta_data( Integrations::PURCHASE_TRACKED_META_KEY, true );
343341
$order->save();
344342
}
345343
}

0 commit comments

Comments
 (0)