Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add GA4 donation events #2326

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions includes/data-events/connectors/ga4/class-ga4.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class GA4 {
public static $watched_events = [
'reader_logged_in',
'reader_registered',
'donation_new',
'donation_subscription_cancelled',
'newsletter_subscribed',
];

Expand Down Expand Up @@ -140,7 +142,7 @@ public static function filter_event_body( $body, $event_name ) {
/**
* Handler for the reader_logged_in event.
*
* @param int $params The GA4 event parameters.
* @param array $params The GA4 event parameters.
* @param array $data Data associated with the Data Events api event.
*
* @return array $params The final version of the GA4 event params that will be sent to GA.
Expand All @@ -152,7 +154,7 @@ public static function handle_reader_logged_in( $params, $data ) {
/**
* Handler for the reader_registered event.
*
* @param int $params The GA4 event parameters.
* @param array $params The GA4 event parameters.
* @param array $data Data associated with the Data Events api event.
*
* @return array $params The final version of the GA4 event params that will be sent to GA.
Expand All @@ -168,10 +170,73 @@ public static function handle_reader_registered( $params, $data ) {
return $params;
}

/**
* Handler for the donation_new event.
*
* @param array $params The GA4 event parameters.
* @param array $data Data associated with the Data Events api event.
*
* @return array $params The final version of the GA4 event params that will be sent to GA.
*/
public static function handle_donation_new( $params, $data ) {
$params['amount'] = $data['amount'];
$params['currency'] = $data['currency'];
$params['recurrence'] = $data['recurrence'];
$params['platform'] = $data['platform'];
$params['referer'] = $data['referer'] ?? '';
$params['popup_id'] = $data['popup_id'] ?? '';
$params['range'] = self::get_donation_amount_range( $data['amount'] );
return $params;
}

/**
* Handler for the donation_subscription_cancelled event.
*
* @param array $params The GA4 event parameters.
* @param array $data Data associated with the Data Events api event.
*
* @return array $params The final version of the GA4 event params that will be sent to GA.
*/
public static function handle_donation_subscription_cancelled( $params, $data ) {
$params['amount'] = $data['amount'];
$params['currency'] = $data['currency'];
$params['recurrence'] = $data['recurrence'];
$params['platform'] = $data['platform'];
$params['range'] = self::get_donation_amount_range( $data['amount'] );
return $params;
}

/**
* Gets the value of the donation range metadata based on the donation amount.
*
* @param mixed $amount The donation amount.
* @return string
*/
public static function get_donation_amount_range( $amount ) {

$amount = (float) $amount;

if ( 0.0 === $amount ) {
return '';
} elseif ( $amount < 20 ) {
return 'under-20';
} elseif ( $amount < 51 ) {
return '20-50';
} elseif ( $amount < 101 ) {
return '51-100';
} elseif ( $amount < 201 ) {
return '101-200';
} elseif ( $amount < 501 ) {
return '201-500';
} else {
return 'over-500';
}
}

/**
* Handler for the newsletter_subscribed event.
*
* @param int $params The GA4 event parameters.
* @param array $params The GA4 event parameters.
* @param array $data Data associated with the Data Events api event.
*
* @return array $params The final version of the GA4 event params that will be sent to GA.
Expand Down
44 changes: 44 additions & 0 deletions tests/unit-tests/ga4-connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Newspack\Data_Events\Connectors\GA4\Event;
use Newspack\Data_Events\Connectors\GA4;

/**
* Tests the GA 4 connector.
Expand Down Expand Up @@ -321,4 +322,47 @@ public function validate_params_data() {
],
];
}

/**
* Data provider for test_validate_param_name
*/
public function get_donation_amount_range_data() {
return [
[
12,
'under-20',
],
[
'zdxasd',
'',
],
[
0,
'',
],
[
'53.12',
'51-100',
],
[
420,
'201-500',
],
[
200,
'101-200',
],
];
}

/**
* Tests the sanitize_value method
*
* @param mixed $value The param value.
* @param mixed $expected The expected result.
* @dataProvider get_donation_amount_range_data
*/
public function test_get_donation_amount_range( $value, $expected ) {
$this->assertEquals( $expected, GA4::get_donation_amount_range( $value ) );
}
}