Skip to content

Commit

Permalink
Merge pull request #26 from customgento/DEV-406-add-google-consent
Browse files Browse the repository at this point in the history
Add google consent, DEV-406
  • Loading branch information
sprankhub authored Feb 28, 2024
2 parents 524cd42 + 45f3a27 commit f624669
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
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 @@ class Config
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 Down Expand Up @@ -43,4 +44,9 @@ public function useEuCdn(): bool
{
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; ?>

0 comments on commit f624669

Please sign in to comment.