Skip to content

Instantly share code, notes, and snippets.

@mbaersch
Last active October 16, 2024 14:07
Show Gist options
  • Save mbaersch/a632ac8c055689aa0eeb789b07350e5b to your computer and use it in GitHub Desktop.
Save mbaersch/a632ac8c055689aa0eeb789b07350e5b to your computer and use it in GitHub Desktop.
Fakecentrics - Custom HTML Tag Code for Google Tag Manager: Dummy Consent Dialog for Testing Purposes

Fakecentrics - Shakespeare Edition

Custom HTML Tag Code for Google Tag Manager

Dummy Consent Dialog for Testing Purposes

Creates a simple fake consent dialog to accept or decline all services defined in a "consentKeys" array. pushes consent to dataLayer in a Usercentrics-like format.

Example consent push

image

<script>
/**
* Fakecentrics - Shakespeare Edition
* ===================================
* creates a simple fake consent dialog to accept or decline
* all services defined in a "consentKeys" array.
* pushes consent to dataLayer in a Usercentrics-like format.
* optional: init and update Google Consent Mode
*
* @version 1.1.0
* @author Markus Baersch <mail@markus-baersch.de>
*/
/*********************************** SETUP *****************************/
//add service names to be covered in dataLayer pushes
//set one or both to null or undefined if no Consent Mode settings are needed
var setConsentModeDefault = {'analytics_storage': 'denied', 'ad_storage': 'denied',
'ad_user_data': 'denied', 'ad_personalization': 'denied'};
var setConsentModeUpdate = {'analytics_storage': 'granted', 'ad_storage': 'granted',
'ad_user_data': 'granted', 'ad_personalization': 'granted'}
//Define your consent keys to be set to true after consent for the current session
var consentKeys = [
"Google Analytics 4",
"Google Ads",
"Solute",
"Facebook Pixel",
];
//session cookie name for consent choice
var fc_cookie = "fakeconsent";
//path to image file (will be scaled 150 pixels wide)
var fc_img = "/areyouhere.png";
//translate title and button captions
var fc_text = {title: "Sein oder Nichtsein?", accept: "Sein!", decline: "Nichtsein!"};
/********************************* END SETUP ***************************/
//Init GCM:
if (typeof(window.gtag) !== "function") {
window.gtag = function(){window.dataLayer.push(arguments);}
}
if (setConsentModeDefault) gtag('consent', 'default', setConsentModeDefault);
</script>
<style>
#fakeconsent{z-index:1000;position:fixed;background:#fff1df;border-radius: 16px;
padding:3em;top:50%;left:50%;color:#222;text-align:center;margin:auto;transform:translate(-50%, -50%);
border:5px solid #222;box-shadow: #000000ab 0 0 10000px 10000px}
.btrw {display:flex;gap:2em;justify-content:center;flex-wrap:wrap;margin:2em 3em 0 3em}
.btrw button{letter-spacing:1px;min-width:8em;border-radius:6px;border:5px solid #222;color:#fff1df;
background:#ff3434;padding:0.6em 1.8em; font-size:1em;font-weight:bold}
#fc_c{text-decoration:none;display:block;float:right;margin:-2em;background:#b49f83;color:#fff;
padding: 1px 10px;border-radius:50%}
#fc_i{width:150px;margin:2em auto}
#fc_t{font-size:2em;font-weight:bold}
</style>
<div id="fakeconsent" style="display:none">
<a id="fc_c" href="javascript:closeFakeConsent()">X</a>
<div id="fc_t">Cookies okay?</div>
<img id="fc_i" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==">
<div class="btrw">
<button id="fc_ba" onclick="closeFakeConsent(true)" style="background:#52b752">Accept</button>
<button id="fc_bd" onclick="closeFakeConsent(false)">Decline</button>
</div>
</div>
<script>
document.getElementById('fc_t').innerHTML = fc_text.title;
document.getElementById('fc_ba').innerHTML = fc_text.accept;
document.getElementById('fc_bd').innerHTML = fc_text.decline;
function push2dl(cns, ac, tp) {
if (cns === true && setConsentModeUpdate)
gtag('consent', 'update', setConsentModeUpdate);
var o={event: "consent_status",
action: ac||"onInitialPageLoad",
type: tp||"explicit",
};
consentKeys.forEach(function(x){o[x] = cns});
window.dataLayer.push(o);
}
function closeFakeConsent(cns) {
if (cns != undefined) {
document.cookie = fc_cookie+"="+cns.toString()+";path=/";
push2dl(cns, cns ? "onAcceptAllServices" : "onDenyAllServices");
} else {
push2dl(false, "onUpdateServices");
}
fc_dlg.style.display = "none";
}
var fc_dlg = document.getElementById("fakeconsent");
if (fc_img) document.getElementById("fc_i").src = fc_img;
if (document.cookie.indexOf(fc_cookie+"=") >= 0) {
push2dl(document.cookie.indexOf(fc_cookie+"=true") >= 0);
} else {
push2dl(false, null, "implicit");
fc_dlg.style.display = "block";
}
</script>
@mbaersch
Copy link
Author

mbaersch commented Dec 15, 2023

consent push to dataLayer:

image

@mbaersch
Copy link
Author

mbaersch commented Dec 15, 2023

example img (created by DALL-E) :

image

@mbaersch
Copy link
Author

Update 2024-16-10: Added Google Consent Mode Default and Update settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment