-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from AlexisPPLIN/feature/consent-mode
Add support for consent mode
- Loading branch information
Showing
6 changed files
with
299 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* User: Alexis POUPELIN (AlexisPPLIN) | ||
* Date: 08.04.2024 | ||
* Time: 11:00 | ||
*/ | ||
|
||
namespace Br33f\Ga4\MeasurementProtocol\Dto\Common; | ||
|
||
use Br33f\Ga4\MeasurementProtocol\Dto\ExportableInterface; | ||
use Br33f\Ga4\MeasurementProtocol\Enum\ConsentCode; | ||
|
||
class ConsentProperty implements ExportableInterface | ||
{ | ||
/** | ||
* Sets consent for sending user data from the request's events and user properties to Google for advertising purposes. | ||
* Must be either {@see ConsentCode::GRANTED} or {@see ConsentCode::DENIED} | ||
* @var string | ||
*/ | ||
protected $ad_user_data; | ||
|
||
/** | ||
* Sets consent for personalized advertising for the user. | ||
* Must be either {@see ConsentCode::GRANTED} or {@see ConsentCode::DENIED} | ||
* @var string | ||
*/ | ||
protected $ad_personalization; | ||
|
||
/** | ||
* ConsentProperty constructor | ||
* Each parameters must be either {@see ConsentCode::GRANTED} or {@see ConsentCode::DENIED} | ||
* @param string|null $ad_user_data | ||
* @param string|null $ad_personalization | ||
*/ | ||
public function __construct(?string $ad_user_data = null, ?string $ad_personalization = null) | ||
{ | ||
$this->ad_user_data = $ad_user_data; | ||
$this->ad_personalization = $ad_personalization; | ||
} | ||
|
||
public function export() : array | ||
{ | ||
$result = []; | ||
|
||
if (isset($this->ad_user_data)) { | ||
$result['ad_user_data'] = $this->ad_user_data; | ||
} | ||
|
||
if (isset($this->ad_personalization)) { | ||
$result['ad_personalization'] = $this->ad_personalization; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getAdUserData() : ?string | ||
{ | ||
return $this->ad_user_data; | ||
} | ||
|
||
/** | ||
* Must be either {@see ConsentCode::GRANTED} or {@see ConsentCode::DENIED} | ||
* @param string|null $ad_user_data | ||
*/ | ||
public function setAdUserData(?string $ad_user_data) : void | ||
{ | ||
$this->ad_user_data = $ad_user_data; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getAdPersonalization() : ?string | ||
{ | ||
return $this->ad_personalization; | ||
} | ||
|
||
/** | ||
* Must be either {@see ConsentCode::GRANTED} or {@see ConsentCode::DENIED} | ||
* @param string|null $ad_personalization | ||
*/ | ||
public function setAdPersonalization(?string $ad_personalization) : void | ||
{ | ||
$this->ad_personalization = $ad_personalization; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* User: Alexis POUPELIN (AlexisPPLIN) | ||
* Date: 08.04.2024 | ||
* Time: 11:00 | ||
*/ | ||
|
||
namespace Br33f\Ga4\MeasurementProtocol\Enum; | ||
|
||
|
||
class ConsentCode | ||
{ | ||
const GRANTED = 'GRANTED'; | ||
const DENIED = 'DENIED'; | ||
} |
85 changes: 85 additions & 0 deletions
85
tests/Ga4/MeasurementProtocol/Dto/Common/ConsentPropertyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* User: Alexis POUPELIN (AlexisPPLIN) | ||
* Date: 08.04.2024 | ||
* Time: 11:00 | ||
*/ | ||
|
||
namespace Tests\Ga4\MeasurementProtocol\Dto\Common; | ||
|
||
use Br33f\Ga4\MeasurementProtocol\Dto\Common\ConsentProperty; | ||
use Br33f\Ga4\MeasurementProtocol\Enum\ConsentCode; | ||
use Tests\Common\BaseTestCase; | ||
|
||
class ConsentPropertyTest extends BaseTestCase | ||
{ | ||
/** | ||
* @var ConsentProperty | ||
*/ | ||
protected $consentProperty; | ||
|
||
public function testDefaultConstructor() | ||
{ | ||
$constructedConsentProperty = new ConsentProperty(); | ||
|
||
$this->assertNull($constructedConsentProperty->getAdUserData()); | ||
$this->assertNull($constructedConsentProperty->getAdPersonalization()); | ||
} | ||
|
||
public function testParametrizedConstructor() | ||
{ | ||
$ad_user_data = ConsentCode::DENIED; | ||
$ad_personalization = ConsentCode::GRANTED; | ||
$constructedConsentProperty = new ConsentProperty($ad_user_data, $ad_personalization); | ||
|
||
$this->assertEquals($ad_user_data, $constructedConsentProperty->getAdUserData()); | ||
$this->assertEquals($ad_personalization, $constructedConsentProperty->getAdPersonalization()); | ||
} | ||
|
||
public function testAdUserData() | ||
{ | ||
$ad_user_data = ConsentCode::DENIED; | ||
$this->consentProperty->setAdUserData($ad_user_data); | ||
|
||
$this->assertEquals($ad_user_data, $this->consentProperty->getAdUserData()); | ||
} | ||
|
||
public function testAdPersonalization() | ||
{ | ||
$ad_personalization = ConsentCode::GRANTED; | ||
$this->consentProperty->setAdPersonalization($ad_personalization); | ||
|
||
$this->assertEquals($ad_personalization, $this->consentProperty->getAdPersonalization()); | ||
} | ||
|
||
public function testExportEmpty() | ||
{ | ||
$emptyConsentProperty = new ConsentProperty(); | ||
|
||
$this->assertEquals([], $emptyConsentProperty->export()); | ||
} | ||
|
||
public function testPartialExport() | ||
{ | ||
$ad_user_data = ConsentCode::DENIED; | ||
$ad_personalization = null; | ||
$constructedConsentProperty = new ConsentProperty($ad_user_data, $ad_personalization); | ||
|
||
$this->assertEquals(['ad_user_data' => 'DENIED'], $constructedConsentProperty->export()); | ||
} | ||
|
||
public function testExport() | ||
{ | ||
$ad_user_data = ConsentCode::DENIED; | ||
$ad_personalization = ConsentCode::GRANTED; | ||
$constructedConsentProperty = new ConsentProperty($ad_user_data, $ad_personalization); | ||
|
||
$this->assertEquals(['ad_user_data' => 'DENIED', 'ad_personalization' => 'GRANTED'], $constructedConsentProperty->export()); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->consentProperty = new ConsentProperty(); | ||
} | ||
} |
Oops, something went wrong.