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

Add google consent, DEV-406 #26

Merged
merged 5 commits into from
Feb 28, 2024
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
6 changes: 6 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public const XML_PATH_COOKIEBOT_ID = 'web/cookiebot/id';
public const XML_PATH_DATA_CULTURE = 'web/cookiebot/data_culture';
public const XML_PATH_USE_EU_CDN = 'web/cookiebot/use_eu_cdn';
public const XML_PATH_USE_GOOGLE_CONSENT_MODE = 'web/cookiebot/use_google_consent_mode';

/**
* @var ScopeConfigInterface
Expand All @@ -24,23 +25,28 @@
$this->scopeConfig = $scopeConfig;
}

public function isEnabled(): bool

Check failure on line 28 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::isEnabled() should return bool but return statement is missing.

Check failure on line 28 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::isEnabled() should return bool but return statement is missing.
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_COOKIEBOT_ENABLED, ScopeInterface::SCOPE_STORE);
}

public function getId(): string

Check failure on line 33 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::getId() should return string but return statement is missing.

Check failure on line 33 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::getId() should return string but return statement is missing.
{
return (string)$this->scopeConfig->getValue(self::XML_PATH_COOKIEBOT_ID, ScopeInterface::SCOPE_STORE);
}

public function getDataCulture(): string

Check failure on line 38 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::getDataCulture() should return string but return statement is missing.

Check failure on line 38 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::getDataCulture() should return string but return statement is missing.
{
return (string)$this->scopeConfig->getValue(self::XML_PATH_DATA_CULTURE, ScopeInterface::SCOPE_STORE);
}

public function useEuCdn(): bool

Check failure on line 43 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::useEuCdn() should return bool but return statement is missing.

Check failure on line 43 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::useEuCdn() should return bool but return statement is missing.
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_USE_EU_CDN, ScopeInterface::SCOPE_STORE);
}

public function isGoogleConsentModeEnabled(): bool

Check failure on line 48 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::isGoogleConsentModeEnabled() should return bool but return statement is missing.

Check failure on line 48 in Model/Config.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method CustomGento\Cookiebot\Model\Config::isGoogleConsentModeEnabled() should return bool but return statement is missing.
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_USE_GOOGLE_CONSENT_MODE, ScopeInterface::SCOPE_STORE);
}
}
10 changes: 10 additions & 0 deletions Test/Integration/AddScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public function testScriptAddedOnHomepage(): void
self::assertStringContainsString($this->script, $this->getResponse()->getBody());
}

/**
* @magentoConfigFixture current_store web/cookiebot/use_google_consent_mode 1
* @magentoConfigFixture current_store web/cookiebot/id 123-456-789
*/
public function testGoogleConsentScriptAddedOnHomepage(): void
{
$this->dispatch('/');
self::assertStringContainsString('gtag("set", "ads_data_redaction", true);', $this->getResponse()->getBody());
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoConfigFixture current_store web/cookiebot/enabled 1
Expand Down
5 changes: 5 additions & 0 deletions ViewModel/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public function getScript(): string

return $this->scriptGenerator->generate();
}

public function isGoogleConsentModeEnabled(): bool
{
return $this->config->isGoogleConsentModeEnabled();
}
}
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<label>Use European CDN</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="use_google_consent_mode" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Google Consent Mode</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<id/>
<data_culture/>
<use_eu_cdn>0</use_eu_cdn>
<use_google_consent_mode>1</use_google_consent_mode>
</cookiebot>
</web>
</default>
Expand Down
25 changes: 25 additions & 0 deletions view/frontend/templates/script.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,28 @@
<?php /** @var CustomGento\Cookiebot\ViewModel\Script $viewModel */ ?>
<?php $viewModel = $block->getData('view_model') ?>
<?= /* @noEscape */ $viewModel->getScript() ?>

<?php
if ($viewModel->isGoogleConsentModeEnabled()):
?>
<script data-cookieconsent="ignore">
window.dataLayer = window.dataLayer || [];

function gtag() {
dataLayer.push(arguments);
}

gtag("consent", "default", {
ad_personalization: "denied",
ad_storage: "denied",
ad_user_data: "denied",
analytics_storage: "denied",
functionality_storage: "denied",
personalization_storage: "denied",
security_storage: "granted",
wait_for_update: 500,
});
gtag("set", "ads_data_redaction", true);
gtag("set", "url_passthrough", true);
</script>
<?php endif; ?>
Loading