Skip to content

Commit

Permalink
Add Consent & Cleanup (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
aawnu authored May 25, 2024
2 parents 40e3966 + 5b996e7 commit c9da065
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 34 deletions.
46 changes: 20 additions & 26 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ public function getRequiredParams(): array
return $return;
}

public function setNonPersonalizedAds(bool $exclude)
{
$this->non_personalized_ads = $exclude;
return $this;
}

public function setClientId(string $id)
{
$this->client_id = $id;
Expand Down Expand Up @@ -112,7 +106,7 @@ public function addEvent(Facade\Type\EventType ...$events)
return $this;
}

public function consent()
public function consent(): ConsentHelper
{
return $this->consent;
}
Expand All @@ -130,30 +124,23 @@ public function post(): void
$url = $this->debug ? Facade\Type\AnalyticsType::URL_DEBUG : Facade\Type\AnalyticsType::URL_LIVE;
$url .= '?' . http_build_query(['measurement_id' => $this->measurement_id, 'api_secret' => $this->api_secret]);

$body = $this->toArray();
array_merge_recursive(
$body = array_replace_recursive(
$this->toArray(),
["user_properties" => $this->user_properties],
["consent" => $this->consent->toArray()],
);

$chunkUserProperties = array_chunk($this->user_properties, 25, true);
$this->user_properties = [];

$chunkEvents = array_chunk($this->events, 25);
$this->events = [];

$chunkMax = count($chunkEvents) > count($chunkUserProperties) ? count($chunkEvents) : count($chunkUserProperties);
if (count($chunkEvents) < 1) {
throw Ga4Exception::throwMissingEvents();
}

for ($chunk = 0; $chunk < $chunkMax; $chunk++) {
$body['user_properties'] = $chunkUserProperties[$chunk] ?? [];
if (empty($body['user_properties'])) {
unset($body['user_properties']);
}
$this->user_properties = [];
$this->events = [];

$body['events'] = $chunkEvents[$chunk] ?? [];
if (empty($body['events'])) {
unset($body['events']);
}
foreach ($chunkEvents as $events) {
$body['events'] = $events;

$kB = 1024;
if (($size = mb_strlen(json_encode($body))) > ($kB * 130)) {
Expand Down Expand Up @@ -204,13 +191,20 @@ public static function new(string $measurementId, string $apiSecret, bool $debug
* Deprecated references
*/

/** @deprecated 1.1.1 */
/** @deprecated 1.1.9 Please use `Analytics->consent->setAdPersonalizationPermission()` instead */
public function setNonPersonalizedAds(bool $exclude)
{
$this->consent->setAdPersonalizationPermission(!$exclude);
return $this;
}

/** @deprecated 1.1.1 Please use `Analytics->consent->setAdPersonalizationPermission()` instead */
public function allowPersonalisedAds(bool $allow)
{
$this->setNonPersonalizedAds(!$allow);
$this->consent->setAdPersonalizationPermission($allow);
}

/** @deprecated 1.1.1 */
/** @deprecated 1.1.1 Please use `Analytics->setTimestampMicros()` instead */
public function setTimestamp(int|float $microOrUnix)
{
$this->setTimestampMicros($microOrUnix);
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/Ga4Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public static function throwRequestInvalidBody(array $msg)
static::REQUEST_INVALID_BODY
);
}

public static function throwMissingEvents()
{
return new static("Request must include at least 1 event with a name", static::REQUEST_EMPTY_EVENTLIST);
}
}
3 changes: 2 additions & 1 deletion src/Facade/Type/AnalyticsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public function setTimestampMicros(int|float $microOrUnix);
public function setNonPersonalizedAds(bool $allow);

/**
* The user properties for the measurement
* The user properties for the measurement (Up to 25 custom per project, see link)
*
* @var user_properties
* @param AlexWestergaard\PhpGa4\Facade\Type\UserProperty $prop
* @link https://support.google.com/analytics/answer/14240153
*/
public function addUserProperty(UserPropertyType ...$props);

Expand Down
1 change: 1 addition & 0 deletions src/Facade/Type/Ga4ExceptionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ interface Ga4ExceptionType
const REQUEST_INVALID_BODY = 104005;
const REQUEST_MISSING_MEASUREMENT_ID = 104006;
const REQUEST_MISSING_API_SECRET = 104007;
const REQUEST_EMPTY_EVENTLIST = 104008;
}
4 changes: 2 additions & 2 deletions src/Helper/ConsentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class ConsentHelper
{
const GRANTED = "granted";
const DENIED = "denied";
const GRANTED = "GRANTED";
const DENIED = "DENIED";

private ?string $ad_user_data = null;
private ?string $ad_personalization = null;
Expand Down
10 changes: 5 additions & 5 deletions test/Unit/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use AlexWestergaard\PhpGa4\Facade;
use AlexWestergaard\PhpGa4\Event;
use AlexWestergaard\PhpGa4\Analytics;
use AlexWestergaard\PhpGa4\Event\Login;
use AlexWestergaard\PhpGa4Test\TestCase;

final class AnalyticsTest extends TestCase
Expand All @@ -17,7 +18,6 @@ public function test_can_configure_and_export()
$this->prefill['api_secret'],
$debug = true
)
->setNonPersonalizedAds($nonPersonalisedAds = true)
->setClientId($this->prefill['client_id'])
->setUserId($this->prefill['user_id'])
->setTimestampMicros($time = time())
Expand All @@ -27,7 +27,6 @@ public function test_can_configure_and_export()
$asArray = $analytics->toArray();
$this->assertIsArray($asArray);

$this->assertArrayHasKey('non_personalized_ads', $asArray);
$this->assertArrayHasKey('timestamp_micros', $asArray);
$this->assertArrayHasKey('client_id', $asArray);
$this->assertArrayHasKey('user_id', $asArray);
Expand All @@ -36,7 +35,6 @@ public function test_can_configure_and_export()

$timeAsMicro = $time * 1_000_000;

$this->assertEquals($nonPersonalisedAds, $asArray['non_personalized_ads']);
$this->assertEquals($timeAsMicro, $asArray['timestamp_micros']);
$this->assertEquals($this->prefill['client_id'], $asArray['client_id']);
$this->assertEquals($this->prefill['user_id'], $asArray['user_id']);
Expand All @@ -46,7 +44,7 @@ public function test_can_configure_and_export()

public function test_can_post_to_google()
{
$this->assertNull($this->analytics->post());
$this->assertNull($this->analytics->addEvent(Login::new())->post());
}

public function test_converts_to_full_microtime_stamp()
Expand All @@ -68,6 +66,8 @@ public function test_throws_if_microtime_older_than_three_days()

public function test_exports_userproperty_to_array()
{
$this->analytics->addEvent(Login::new());

$userProperty = UserProperty::new()
->setName('customer_tier')
->setValue('premium');
Expand Down Expand Up @@ -138,7 +138,7 @@ public function test_throws_on_too_large_request_package()
$userProperty->setValue($overflowValue);
}

$this->analytics->addUserProperty($userProperty)->post();
$this->analytics->addEvent(Login::new())->addUserProperty($userProperty)->post();
}

public function test_timeasmicro_throws_exceeding_max()
Expand Down
19 changes: 19 additions & 0 deletions test/Unit/ConsentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

namespace AlexWestergaard\PhpGa4Test\Unit;

use AlexWestergaard\PhpGa4\Event\Login;
use AlexWestergaard\PhpGa4\Helper\ConsentHelper;
use AlexWestergaard\PhpGa4Test\TestCase;

final class ConsentTest extends TestCase
{
public function test_no_consent_is_empty()
{
$this->analytics->addEvent(Login::new());

$export = $this->analytics->consent()->toArray();
$this->assertIsArray($export);
$this->assertCount(0, $export);
}

public function test_consent_ad_user_data_granted()
{
$this->analytics->addEvent(Login::new());

$this->analytics->consent()->setAdUserDataPermission(true);

$export = $this->analytics->consent()->toArray();
Expand All @@ -28,6 +33,8 @@ public function test_consent_ad_user_data_granted()

public function test_consent_ad_personalization_granted()
{
$this->analytics->addEvent(Login::new());

$this->analytics->consent()->setAdPersonalizationPermission(true);

$export = $this->analytics->consent()->toArray();
Expand All @@ -40,6 +47,8 @@ public function test_consent_ad_personalization_granted()

public function test_consent_granted()
{
$this->analytics->addEvent(Login::new());

$this->analytics->consent()->setAdUserDataPermission(true);
$this->analytics->consent()->setAdPersonalizationPermission(true);

Expand All @@ -54,6 +63,8 @@ public function test_consent_granted()

public function test_consent_granted_posted()
{
$this->analytics->addEvent(Login::new());

$this->analytics->consent()->setAdUserDataPermission(true);
$this->analytics->consent()->setAdPersonalizationPermission(true);

Expand All @@ -69,6 +80,8 @@ public function test_consent_granted_posted()

public function test_consent_ad_user_data_denied()
{
$this->analytics->addEvent(Login::new());

$this->analytics->consent()->setAdUserDataPermission(false);

$export = $this->analytics->consent()->toArray();
Expand All @@ -81,6 +94,8 @@ public function test_consent_ad_user_data_denied()

public function test_consent_ad_personalization_denied()
{
$this->analytics->addEvent(Login::new());

$this->analytics->consent()->setAdPersonalizationPermission(false);

$export = $this->analytics->consent()->toArray();
Expand All @@ -93,6 +108,8 @@ public function test_consent_ad_personalization_denied()

public function test_consent_denied()
{
$this->analytics->addEvent(Login::new());

$this->analytics->consent()->setAdUserDataPermission(false);
$this->analytics->consent()->setAdPersonalizationPermission(false);

Expand All @@ -107,6 +124,8 @@ public function test_consent_denied()

public function test_consent_denied_posted()
{
$this->analytics->addEvent(Login::new());

$this->analytics->consent()->setAdUserDataPermission(false);
$this->analytics->consent()->setAdPersonalizationPermission(false);

Expand Down

0 comments on commit c9da065

Please sign in to comment.