Skip to content

Commit 15e09c2

Browse files
committed
Merge branch 'develop'
2 parents fcd5fbb + ab7f166 commit 15e09c2

File tree

6 files changed

+68
-39
lines changed

6 files changed

+68
-39
lines changed

give-google-analytics.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Give - Google Analytics Donation Tracking
44
* Plugin URI: https://givewp.com/addons/google-analytics/
55
* Description: Add Google Analytics Enhanced eCommerce tracking functionality to Give to track donations.
6-
* Version: 2.0.0
6+
* Version: 2.1.0
77
* Author: GiveWP
88
* Author URI: https://givewp.com
99
* Requires at least: 5.0
@@ -19,12 +19,12 @@
1919

2020
// Plugin version.
2121
if ( ! defined( 'GIVE_GOOGLE_ANALYTICS_VERSION' ) ) {
22-
define( 'GIVE_GOOGLE_ANALYTICS_VERSION', '2.0.0' );
22+
define( 'GIVE_GOOGLE_ANALYTICS_VERSION', '2.1.0' );
2323
}
2424

2525
// Min. Give version.
2626
if ( ! defined( 'GIVE_GOOGLE_ANALYTICS_MIN_GIVE_VERSION' ) ) {
27-
define( 'GIVE_GOOGLE_ANALYTICS_MIN_GIVE_VERSION', '2.21.3' );
27+
define( 'GIVE_GOOGLE_ANALYTICS_MIN_GIVE_VERSION', '2.23.0' );
2828
}
2929

3030
// Plugin File.

readme.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Contributors: givewp
33
Tags: donation analytics, donation, ecommerce, e-commerce, fundraising, fundraiser
44
Requires at least: 5.0
5-
Tested up to: 6.0
6-
Stable tag: 2.0.0
5+
Tested up to: 6.1
6+
Stable tag: 2.1.0
77
Requires PHP: 7.0
8-
Requires Give: 2.21.3
8+
Requires Give: 2.23.0
99
License: GPLv3
1010
License URI: http://www.gnu.org/licenses/gpl-3.0.html
1111

@@ -19,10 +19,9 @@ Also, This plugin assumes that you have already added Google analytics tracking
1919

2020
= Minimum Requirements =
2121

22-
* WordPress 4.8 or greater
23-
* PHP version 5.3 or greater
24-
* MySQL version 5.0 or greater
25-
* Some payment gateways require fsockopen support (for IPN access)
22+
* WordPress 5.0 or greater
23+
* PHP version 7.0 or greater
24+
* MySQL version 5.7 or greater
2625

2726
= Automatic installation =
2827

@@ -39,6 +38,8 @@ The manual installation method involves downloading our donation plugin and uplo
3938
Automatic updates should work like a charm; as always though, ensure you backup your site just in case.
4039

4140
== Changelog ==
41+
= 2.1.0: October 20th, 2022 =
42+
* Enhancement: Under the hood improvements to how recurring donations are handled
4243

4344
= 2.0.0: August 18th, 2022 =
4445
* New: Support for Google Analytics 4! Switch within the plugins settings when ready.

src/Donations/Actions/RecordDonationInGoogleAnalyticsWithGA4.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,21 @@ public function __invoke($donationId, $newDonationStatus)
6464
/**
6565
* This function triggers Google Analytic event for renewal payment.
6666
*
67+
* @since 2.1.0 use the new donation type property to identify renewals
6768
* @since 2.0.0
6869
*/
6970
public function handleRenewal(Give_Payment $givePayment)
7071
{
7172
if (
72-
DonationStatus::RENEWAL !== $givePayment->status ||
7373
!$this->settingRepository->canSendEvent(TrackingMode::GOOGLE_ANALYTICS_4) ||
7474
$this->donationRepository->isGoogleAnalyticEventSent($givePayment->ID)
7575
) {
7676
return;
7777
}
7878

79-
if ($donation = Donation::find($givePayment->ID)) {
79+
$donation = Donation::find($givePayment->ID);
80+
81+
if ($donation !== null && $donation->type->isRenewal()) {
8082
$this->sendEvent($donation);
8183
}
8284
}
@@ -104,7 +106,7 @@ private function sendEvent(Donation $donation)
104106
'content' => esc_html__(
105107
'Google Analytics ecommerce tracking beacon sent.',
106108
'give-google-analytics'
107-
)
109+
),
108110
]
109111
);
110112
}
@@ -139,12 +141,12 @@ private function getEventData(Donation $donation): array
139141
'item_category3' => $this->getDonationTypeLabel($donation),
140142
'item_list_name' => $this->settingRepository->getTrackListName(),
141143
'price' => $donation->amount->formatToDecimal(),
142-
'quantity' => 1
143-
]
144-
]
145-
]
146-
]
147-
]
144+
'quantity' => 1,
145+
],
146+
],
147+
],
148+
],
149+
],
148150
];
149151

150152
/**
@@ -159,15 +161,16 @@ private function getEventData(Donation $donation): array
159161
* This function returns donation type label.
160162
* This label used as product category which help to differentiate revenue in Google Analytics Dashboard.
161163
*
164+
* @since 2.1.0 use the new donation type property to identify renewals
162165
* @since 2.0.0
163166
*/
164167
private function getDonationTypeLabel(Donation $donation): string
165168
{
166-
if ($donation->status->isRenewal()) {
169+
if ($donation->type->isRenewal()) {
167170
return 'Renewal';
168171
}
169172

170-
if (give(DonationRepository::class)->isParentSubscription($donation->id)) {
173+
if ($donation->type->isSubscription()) {
171174
return 'Subscription';
172175
}
173176

@@ -177,13 +180,14 @@ private function getDonationTypeLabel(Donation $donation): string
177180
/**
178181
* This function return Google Analytics client session key.
179182
*
183+
* @since 2.1.0 use the new donation type property to identify renewals
180184
* @since 2.0.0
181185
*/
182186
private function getGoogleAnalyticsClientSession(Donation $donation): string
183187
{
184-
if ($donation->status->isRenewal()) {
188+
if ($donation->type->isRenewal()) {
185189
return $this->donationRepository
186-
->getGoogleAnalyticsClientSession($donation->parentId)
190+
->getGoogleAnalyticsClientSession(give()->subscriptions->getInitialDonationId($donation->subscriptionId))
187191
->gaSessionId;
188192
}
189193

@@ -195,12 +199,13 @@ private function getGoogleAnalyticsClientSession(Donation $donation): string
195199
/**
196200
* This function returns the Google Analytics client id which generates on frontend when donor process/view donation form or which website.
197201
*
202+
* @since 2.1.0 use the new donation type property to identify renewals
198203
* @since 2.0.0
199204
*/
200205
private function getGoogleAnalyticsClientTrackingId(Donation $donation): string
201206
{
202-
if ($donation->status->isRenewal()) {
203-
return $this->donationRepository->getGoogleAnalyticsClientTrackingId($donation->parentId);
207+
if ($donation->type->isRenewal()) {
208+
return $this->donationRepository->getGoogleAnalyticsClientTrackingId(give()->subscriptions->getInitialDonationId($donation->subscriptionId));
204209
}
205210

206211
return $this->donationRepository->getGoogleAnalyticsClientTrackingId($donation->id);

src/Donations/Actions/RefundDonationInGoogleAnalyticsWithGA4.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace GiveGoogleAnalytics\Donations\Actions;
44

5+
use Exception;
56
use Give\Donations\Models\Donation;
67
use Give\Donations\Models\DonationNote;
78
use Give\Donations\ValueObjects\DonationStatus;
89
use GiveGoogleAnalytics\Donations\Repositories\DonationRepository;
910
use GiveGoogleAnalytics\GoogleAnalytics\GA4\Client;
1011
use GiveGoogleAnalytics\GoogleAnalytics\ValueObjects\TrackingMode;
12+
use GiveGoogleAnalytics\Log\Log;
1113
use GiveGoogleAnalytics\Settings\Repositories\SettingRepository;
1214

1315
class RefundDonationInGoogleAnalyticsWithGA4
@@ -39,6 +41,7 @@ public function __construct(
3941
}
4042

4143
/**
44+
* @since 2.1.0 use the new donation type property to identify renewals
4245
* @since 2.0.0
4346
*
4447
* @param int $donationId
@@ -67,13 +70,12 @@ public function __invoke($donationId, $newDonationStatus, $oldDonationStatus)
6770
}
6871

6972
try {
70-
$isRenewal = $oldDonationStatus === DonationStatus::RENEWAL;
7173
$response = $this->client->postEvent(
7274
json_encode(
7375
$this->getEventData(
7476
$donation,
75-
$this->getGoogleAnalyticsClientTrackingId($donation, $isRenewal),
76-
$this->getGoogleAnalyticsClientSession($donation, $isRenewal)
77+
$this->getGoogleAnalyticsClientTrackingId($donation, $donation->type->isRenewal()),
78+
$this->getGoogleAnalyticsClientSession($donation, $donation->type->isRenewal())
7779
)
7880
)
7981
);
@@ -91,7 +93,11 @@ public function __invoke($donationId, $newDonationStatus, $oldDonationStatus)
9193
]
9294
);
9395
}
94-
} catch (\Exception $exception) {
96+
} catch (Exception $exception) {
97+
Log::error('Google Analytics refund beacon failed to send.', [
98+
'donationId' => $donationId,
99+
'exception' => $exception,
100+
]);
95101
}
96102
}
97103

@@ -130,12 +136,13 @@ private function getEventData(
130136
/**
131137
* This function returns the Google Analytics client id which generates on frontend when donor process/view donation form or which website.
132138
*
139+
* @since 2.1.0 switch to new method for retrieving initial donation
133140
* @since 2.0.0
134141
*/
135142
private function getGoogleAnalyticsClientTrackingId(Donation $donation, bool $isRenewal): string
136143
{
137144
if ($isRenewal) {
138-
return $this->donationRepository->getGoogleAnalyticsClientTrackingId($donation->parentId);
145+
return $this->donationRepository->getGoogleAnalyticsClientTrackingId(give()->subscriptions->getInitialDonationId($donation->subscriptionId));
139146
}
140147

141148
return $this->donationRepository->getGoogleAnalyticsClientTrackingId($donation->id);
@@ -144,13 +151,14 @@ private function getGoogleAnalyticsClientTrackingId(Donation $donation, bool $is
144151
/**
145152
* This function return Google Analytics client session key.
146153
*
154+
* @since 2.1.0 switch to new method for retrieving initial donation
147155
* @since 2.0.0
148156
*/
149157
private function getGoogleAnalyticsClientSession(Donation $donation, bool $isRenewal): string
150158
{
151159
if ($isRenewal) {
152160
return $this->donationRepository
153-
->getGoogleAnalyticsClientSession($donation->parentId)
161+
->getGoogleAnalyticsClientSession(give()->subscriptions->getInitialDonationId($donation->subscriptionId))
154162
->gaSessionId;
155163
}
156164

src/Donations/Repositories/DonationRepository.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,4 @@ public function getGoogleAnalyticsClientSession(int $donationId): GoogleAnalytic
7373
)
7474
);
7575
}
76-
77-
/**
78-
* @since 2.0.0
79-
*/
80-
public function isParentSubscription(int $donationId): bool
81-
{
82-
return '1' === give()->payment_meta->get_meta($donationId, '_give_subscription_payment', true);
83-
}
8476
}

src/Log/Log.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GiveGoogleAnalytics\Log;
6+
7+
/**
8+
* @since 1.0.0
9+
*/
10+
class Log extends \Give\Log\Log {
11+
/**
12+
* @inheritDoc
13+
* @since 1.0.0
14+
*
15+
* @param string $name
16+
* @param array $arguments
17+
*/
18+
public static function __callStatic( $name, $arguments ) {
19+
$arguments[1]['source'] = 'Google Analytics';
20+
21+
parent::__callStatic( $name, $arguments );
22+
}
23+
}

0 commit comments

Comments
 (0)