Skip to content

Commit

Permalink
Merge pull request #21 from googleanalytics/consent-update
Browse files Browse the repository at this point in the history
fix for personalization settings
  • Loading branch information
matt-landers authored May 13, 2024
2 parents d766dd4 + e8fa80c commit 9f485ea
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 108 deletions.
105 changes: 51 additions & 54 deletions src/public/partials/consent.eta
Original file line number Diff line number Diff line change
@@ -1,66 +1,63 @@

<div id="cookie-consent-banner" class="cookie-consent-banner">
<h3>Cookie settings</h3>
<p>We use cookies to provide you with the best possible experience. They also allow us to analyze user behavior in order to constantly improve the website for you.</p>
<button id="btn-accept-all" class="cookie-consent-button btn-success">Accept All</button>
<button id="btn-accept-some" class="cookie-consent-button btn-outline">Accept Selection</button>
<button id="btn-reject-all" class="cookie-consent-button btn-grayscale">Reject All</button>
<div class="cookie-consent-options">
<label><input id="consent-necessary" type="checkbox" value="Necessary" checked disabled>Necessary</label>
<label><input id="consent-analytics" type="checkbox" value="Analytics" checked>Analytics</label>
<label><input id="consent-preferences" type="checkbox" value="Preferences" checked>Preferences</label>
<label><input id="consent-marketing" type="checkbox" value="Marketing">Marketing</label>
<label><input id="consent-necessary" type="checkbox" value="Necessary" checked disabled>Necessary</label>
<label><input id="consent-analytics" type="checkbox" value="Analytics" checked>Analytics</label>
<label><input id="consent-preferences" type="checkbox" value="Preferences" checked>Preferences</label>
<label><input id="consent-marketing" type="checkbox" value="Marketing">Marketing</label>
</div>
</div>

<script>

function hideBanner() {
document.getElementById('cookie-consent-banner').style.display = 'none';
}
function hideBanner() {
document.getElementById('cookie-consent-banner').style.display = 'none';
}

if (localStorage.getItem('consentMode') === null) {

document.getElementById('btn-accept-all').addEventListener('click', function() {
setConsent({
necessary: true,
analytics: true,
preferences: true,
marketing: true
});
hideBanner();
});
document.getElementById('btn-accept-some').addEventListener('click', function() {
setConsent({
necessary: true,
analytics: document.getElementById('consent-analytics').checked,
preferences: document.getElementById('consent-preferences').checked,
marketing: document.getElementById('consent-marketing').checked
});
hideBanner();
});
document.getElementById('btn-reject-all').addEventListener('click', function() {
setConsent({
necessary: false,
analytics: false,
preferences: false,
marketing: false
});
hideBanner();
});
document.getElementById('cookie-consent-banner').style.display = 'block';
}

if(localStorage.getItem('consentMode') === null) {

document.getElementById('btn-accept-all').addEventListener('click', function() {
setConsent({
necessary: true,
analytics: true,
preferences: true,
marketing: true
});
hideBanner();
});
document.getElementById('btn-accept-some').addEventListener('click', function() {
setConsent({
necessary: true,
analytics: document.getElementById('consent-analytics').checked,
preferences: document.getElementById('consent-preferences').checked,
marketing: document.getElementById('consent-marketing').checked
});
hideBanner();
});
document.getElementById('btn-reject-all').addEventListener('click', function() {
setConsent({
necessary: false,
analytics: false,
preferences: false,
marketing: false
});
hideBanner();
});
document.getElementById('cookie-consent-banner').style.display = 'block';
}

function setConsent(consent) {
const consentMode = {
'functionality_storage': consent.necessary ? 'granted' : 'denied',
'security_storage': consent.necessary ? 'granted' : 'denied',
'ad_storage': consent.marketing ? 'granted' : 'denied',
'analytics_storage': consent.analytics ? 'granted' : 'denied',
'personalization': consent.preferences ? 'granted' : 'denied',
};
gtag('consent', 'update', consentMode);
localStorage.setItem('consentMode', JSON.stringify(consentMode));
}

</script>
function setConsent(consent) {
const consentMode = {
'functionality_storage': consent.necessary ? 'granted' : 'denied',
'security_storage': consent.necessary ? 'granted' : 'denied',
'ad_storage': consent.marketing ? 'granted' : 'denied',
'analytics_storage': consent.analytics ? 'granted' : 'denied',
'personalization_storage': consent.preferences ? 'granted' : 'denied',
};
gtag('consent', 'update', consentMode);
localStorage.setItem('consentMode', JSON.stringify(consentMode));
}
</script>
105 changes: 51 additions & 54 deletions tutorials/4-consent-mode/src/public/partials/consent.eta
Original file line number Diff line number Diff line change
@@ -1,66 +1,63 @@

<div id="cookie-consent-banner" class="cookie-consent-banner">
<h3>Cookie settings</h3>
<p>We use cookies to provide you with the best possible experience. They also allow us to analyze user behavior in order to constantly improve the website for you.</p>
<button id="btn-accept-all" class="cookie-consent-button btn-success">Accept All</button>
<button id="btn-accept-some" class="cookie-consent-button btn-outline">Accept Selection</button>
<button id="btn-reject-all" class="cookie-consent-button btn-grayscale">Reject All</button>
<div class="cookie-consent-options">
<label><input id="consent-necessary" type="checkbox" value="Necessary" checked disabled>Necessary</label>
<label><input id="consent-analytics" type="checkbox" value="Analytics" checked>Analytics</label>
<label><input id="consent-preferences" type="checkbox" value="Preferences" checked>Preferences</label>
<label><input id="consent-marketing" type="checkbox" value="Marketing">Marketing</label>
<label><input id="consent-necessary" type="checkbox" value="Necessary" checked disabled>Necessary</label>
<label><input id="consent-analytics" type="checkbox" value="Analytics" checked>Analytics</label>
<label><input id="consent-preferences" type="checkbox" value="Preferences" checked>Preferences</label>
<label><input id="consent-marketing" type="checkbox" value="Marketing">Marketing</label>
</div>
</div>

<script>

function hideBanner() {
document.getElementById('cookie-consent-banner').style.display = 'none';
}
function hideBanner() {
document.getElementById('cookie-consent-banner').style.display = 'none';
}

if (localStorage.getItem('consentMode') === null) {

document.getElementById('btn-accept-all').addEventListener('click', function() {
setConsent({
necessary: true,
analytics: true,
preferences: true,
marketing: true
});
hideBanner();
});
document.getElementById('btn-accept-some').addEventListener('click', function() {
setConsent({
necessary: true,
analytics: document.getElementById('consent-analytics').checked,
preferences: document.getElementById('consent-preferences').checked,
marketing: document.getElementById('consent-marketing').checked
});
hideBanner();
});
document.getElementById('btn-reject-all').addEventListener('click', function() {
setConsent({
necessary: false,
analytics: false,
preferences: false,
marketing: false
});
hideBanner();
});
document.getElementById('cookie-consent-banner').style.display = 'block';
}

if(localStorage.getItem('consentMode') === null) {

document.getElementById('btn-accept-all').addEventListener('click', function() {
setConsent({
necessary: true,
analytics: true,
preferences: true,
marketing: true
});
hideBanner();
});
document.getElementById('btn-accept-some').addEventListener('click', function() {
setConsent({
necessary: true,
analytics: document.getElementById('consent-analytics').checked,
preferences: document.getElementById('consent-preferences').checked,
marketing: document.getElementById('consent-marketing').checked
});
hideBanner();
});
document.getElementById('btn-reject-all').addEventListener('click', function() {
setConsent({
necessary: false,
analytics: false,
preferences: false,
marketing: false
});
hideBanner();
});
document.getElementById('cookie-consent-banner').style.display = 'block';
}

function setConsent(consent) {
const consentMode = {
'functionality_storage': consent.necessary ? 'granted' : 'denied',
'security_storage': consent.necessary ? 'granted' : 'denied',
'ad_storage': consent.marketing ? 'granted' : 'denied',
'analytics_storage': consent.analytics ? 'granted' : 'denied',
'personalization': consent.preferences ? 'granted' : 'denied',
};
gtag('consent', 'update', consentMode);
localStorage.setItem('consentMode', JSON.stringify(consentMode));
}

</script>
function setConsent(consent) {
const consentMode = {
'functionality_storage': consent.necessary ? 'granted' : 'denied',
'security_storage': consent.necessary ? 'granted' : 'denied',
'ad_storage': consent.marketing ? 'granted' : 'denied',
'analytics_storage': consent.analytics ? 'granted' : 'denied',
'personalization_storage': consent.preferences ? 'granted' : 'denied',
};
gtag('consent', 'update', consentMode);
localStorage.setItem('consentMode', JSON.stringify(consentMode));
}
</script>

0 comments on commit 9f485ea

Please sign in to comment.