diff --git a/src/blocks/donate/class-wp-rest-newspack-donate-controller.php b/src/blocks/donate/class-wp-rest-newspack-donate-controller.php index 65e63d7e3..2eab2205b 100644 --- a/src/blocks/donate/class-wp-rest-newspack-donate-controller.php +++ b/src/blocks/donate/class-wp-rest-newspack-donate-controller.php @@ -85,6 +85,9 @@ public function register_routes() { 'payment_method_id' => [ 'sanitize_callback' => 'sanitize_text_field', ], + 'origin' => [ + 'sanitize_callback' => 'sanitize_text_field', + ], ], 'permission_callback' => '__return_true', ], @@ -123,6 +126,7 @@ public function api_process_donation( $request ) { $frequency = $request->get_param( 'frequency' ); $full_name = $request->get_param( 'full_name' ); + $origin = $request->get_param( 'origin' ); $user_id = self::$current_user_id; @@ -156,6 +160,7 @@ public function api_process_donation( $request ) { 'newsletterOptIn' => $request->get_param( 'newsletter_opt_in' ), 'userId' => $user_id, 'current_page_url' => \wp_get_referer(), + 'origin' => $origin, ], 'payment_metadata' => $payment_metadata, 'payment_method_id' => $request->get_param( 'payment_method_id' ), diff --git a/src/blocks/donate/streamlined/index.test.js b/src/blocks/donate/streamlined/index.test.js index 9c1998e50..3e7f97964 100644 --- a/src/blocks/donate/streamlined/index.test.js +++ b/src/blocks/donate/streamlined/index.test.js @@ -123,6 +123,7 @@ describe( 'Streamlined Donate block processing', () => { newsletter_opt_in: false, clientId: 'amp-123', payment_method_id: 'pm_123', + origin: null, }, }, 'post' diff --git a/src/blocks/donate/streamlined/index.ts b/src/blocks/donate/streamlined/index.ts index e68e39aa1..ae9bbf08f 100644 --- a/src/blocks/donate/streamlined/index.ts +++ b/src/blocks/donate/streamlined/index.ts @@ -108,6 +108,14 @@ export const processStreamlinedElements = ( parentElement = document ) => } } const formValues = utils.getDonationFormValues( formElement ); + const promptOrigin = formElement.closest( 'amp-layout.newspack-popup' ); + + // If the donation originated from a Campaigns prompt, append the prompt ID to the event label. + const origin = + promptOrigin && promptOrigin.hasAttribute( 'amp-access' ) + ? promptOrigin.getAttribute( 'amp-access' ) + : null; + const apiRequestPayload = { captchaToken, tokenData: token, @@ -117,8 +125,10 @@ export const processStreamlinedElements = ( parentElement = document ) => frequency: formValues.donation_frequency, newsletter_opt_in: Boolean( formValues.newsletter_opt_in ), clientId: formValues.cid, + origin, ...requestPayloadOverrides, }; + const chargeResultData = await utils.sendAPIRequest( '/donate', apiRequestPayload ); // Error handling.