|
9 | 9 |
|
10 | 10 | namespace Plausible\Analytics\WP\Integrations; |
11 | 11 |
|
| 12 | +use Plausible\Analytics\WP\Integrations; |
12 | 13 | use Plausible\Analytics\WP\Proxy; |
13 | 14 |
|
14 | 15 | /** |
@@ -57,7 +58,7 @@ private function init( $init ) { |
57 | 58 | add_action( 'edd_post_add_to_cart', [ $this, 'track_add_to_cart' ], 10, 3 ); |
58 | 59 | add_action( 'edd_pre_remove_from_cart', [ $this, 'track_remove_cart_item' ], 10 ); |
59 | 60 | 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' ] ); |
61 | 62 | } |
62 | 63 |
|
63 | 64 | /** |
@@ -173,26 +174,44 @@ public function track_entered_checkout() { |
173 | 174 | } |
174 | 175 |
|
175 | 176 | /** |
176 | | - * Tracks the "purchase" event with relevant order and payment data. |
| 177 | + * Tracks the "purchase" event with relevant payment data. |
177 | 178 | * |
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. |
180 | 181 | * |
181 | 182 | * @return void |
182 | 183 | */ |
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 | + ) |
192 | 210 | ); |
| 211 | + $label = $this->event_goals[ 'purchase' ]; |
193 | 212 |
|
194 | | - $proxy = new Proxy( false ); |
| 213 | + echo sprintf( Integrations::SCRIPT_WRAPPER, "window.plausible( '$label', $props )" ); |
195 | 214 |
|
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 ); |
197 | 216 | } |
198 | 217 | } |
0 commit comments