Last active
May 19, 2025 05:16
-
-
Save DIntriglia/f9b7f72133891b7cdd481d9d576eca4f to your computer and use it in GitHub Desktop.
Termageddon + Usercentrics - Geolocation Hide/Show (js)
This file contains hidden or 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
/** For implementation, usage, and testing information, please see this article: | |
https://termageddon.freshdesk.com/support/solutions/articles/66000530255-geolocation-feature-for-all-web-building-platforms- | |
**/ | |
const termageddon_usercentrics_geolocation = (config = {}) => { | |
const tuSessionStorageName = "uc_user_country"; | |
const tuDebug = config.debug || false; | |
const tuPSLHide = config["psl_hide"] || true; | |
const tuToggle = "div#usercentrics-root,aside#usercentrics-cmp-ui"; | |
if (tuDebug) console.log("UC: Local Geolocation AJAX script initialized"); | |
const getSessionStorage = (name) => { | |
const value = sessionStorage.getItem(name); | |
return value ? JSON.parse(value) : null; | |
}; | |
const getQueryParams = (param) => { | |
const params = new Proxy(new URLSearchParams(window.location.search), { | |
get: (searchParams, prop) => searchParams.get(prop), | |
}); | |
return params[param]; | |
}; | |
const shouldShowBasedOnLocation = (countryCode, regionCode) => { | |
// Special case for EU - check if country is in EU country list | |
// IF EU is configured, check if the country is in the EU list. | |
if (config.show_locations["EU"]) { | |
const euCountries = [ | |
"AT", | |
"BE", | |
"BG", | |
"HR", | |
"CY", | |
"CZ", | |
"DK", | |
"EE", | |
"FI", | |
"FR", | |
"DE", | |
"GR", | |
"HU", | |
"IE", | |
"IT", | |
"LV", | |
"LT", | |
"LU", | |
"MT", | |
"NL", | |
"PL", | |
"PT", | |
"RO", | |
"SK", | |
"SI", | |
"ES", | |
"SE", | |
"NO", | |
"IS", | |
"LI", | |
]; | |
//If EU is enabled via boolean, show if in any EU country | |
if ( | |
config.show_locations["EU"] === true && | |
euCountries.includes(countryCode) | |
) { | |
if (tuDebug) | |
console.log( | |
"UC: Showing consent widget for all EU countries", | |
countryCode | |
); | |
return true; | |
} | |
//If EU is enabled via country list, show if in that specific EU country. | |
if ( | |
Array.isArray(config.show_locations["EU"]) && | |
config.show_locations["EU"].includes(countryCode) | |
) { | |
if (tuDebug) | |
console.log( | |
"UC: Showing consent widget for specific EU country", | |
countryCode | |
); | |
return true; | |
} | |
} | |
// Default case - check if location is explicitly enabled in config | |
// This can be a boolean or an array of valid region codes to show for that country. | |
// This allows for US, CA, UK. EU is multiple countries, so it has a fallback above. | |
if ( | |
config.show_locations[countryCode] && | |
(config.show_locations[countryCode] === true || | |
(Array.isArray(config.show_locations[countryCode]) && | |
config.show_locations[countryCode].includes(regionCode))) | |
) { | |
if (tuDebug) | |
console.log( | |
"UC: Showing consent widget for specific country & region", | |
countryCode, | |
regionCode | |
); | |
return true; | |
} | |
return false; | |
}; | |
const updateCookieConsent = (hide) => { | |
if (!hide) { | |
if (tuDebug) console.log("UC: Showing consent widget"); | |
//Show Consent Options | |
if (tuPSLHide) | |
jQuery("#usercentrics-psl, .usercentrics-psl").show(); | |
jQuery(tuToggle).show(); | |
// Check if consent is still required. If not, close the CMP as it's not needed. | |
if (!UC_UI.isConsentRequired()) return UC_UI.closeCMP(); | |
//Otherwise, show the first layer. | |
return UC_UI.showFirstLayer(); | |
} else { | |
if (tuDebug) console.log("UC: Hiding consent widget"); | |
//Hide Consent Options | |
if (tuPSLHide) | |
jQuery("#usercentrics-psl, .usercentrics-psl").hide(); | |
jQuery(tuToggle).hide(); | |
//Check for already acceptance. | |
if (UC_UI.areAllConsentsAccepted()) return; | |
} | |
UC_UI.acceptAllConsents().then(() => { | |
if (tuDebug) console.log("UC: All consents have been accepted."); | |
UC_UI.closeCMP().then(() => { | |
if (tuDebug) console.log("UC: CMP Widget has been closed."); | |
}); | |
}); | |
}; | |
const main = () => { | |
// Check for Usercentrics Integration | |
if (typeof UC_UI === "undefined") | |
return console.error("Usercentrics not loaded"); | |
//Check query variable from browser | |
const query_hide = | |
getQueryParams("enable-usercentrics") === "" ? true : false; | |
//Check for local cookie to use instead of calling. | |
const sessionStorageData = getSessionStorage(tuSessionStorageName); | |
if (!sessionStorageData) { | |
console.warn( | |
"UC: WARNING - Session storage value for 'uc_user_country' not found. Are you using Usercentrics v2?" | |
); | |
return; | |
} | |
if (typeof sessionStorageData.regionCode === "undefined") return; | |
// Build Ajax Query. | |
const { regionCode, countryCode } = sessionStorageData; | |
// Output debug message to console. | |
if (tuDebug) { | |
console.log( | |
"UC: TERMAGEDDON USERCENTRICS (SESSION STORAGE)" + | |
"\n" + | |
"Country Code: " + | |
countryCode + | |
"\n" + | |
"Region Code: " + | |
regionCode | |
); | |
} | |
if (query_hide) { | |
if (tuDebug) | |
console.log( | |
"UC: Enabling due to query parameter override.", | |
"Showing Usercentrics" | |
); | |
return updateCookieConsent(false); | |
} | |
// Show based on the current location | |
const shouldShow = shouldShowBasedOnLocation(countryCode, regionCode); | |
updateCookieConsent(!shouldShow); | |
}; | |
window.addEventListener("UC_UI_INITIALIZED", main); | |
}; |
This file contains hidden or 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
/** For implementation, usage, and testing information, please see this article: | |
https://termageddon.freshdesk.com/support/solutions/articles/66000530255-geolocation-feature-for-all-web-building-platforms- | |
**/ | |
const termageddon_usercentrics_geolocation=(config={})=>{const tuSessionStorageName="uc_user_country",tuDebug=config.debug||!1,tuPSLHide=config.psl_hide||!0,tuToggle="div#usercentrics-root,aside#usercentrics-cmp-ui";tuDebug&&console.log("UC: Local Geolocation AJAX script initialized");const getSessionStorage=name=>{const value=sessionStorage.getItem(name);return value?JSON.parse(value):null},getQueryParams=param=>{const params=new Proxy(new URLSearchParams(window.location.search),{get:(searchParams,prop)=>searchParams.get(prop)});return params[param]},shouldShowBasedOnLocation=(countryCode,regionCode)=>{if(config.show_locations.EU){const euCountries=["AT","BE","BG","HR","CY","CZ","DK","EE","FI","FR","DE","GR","HU","IE","IT","LV","LT","LU","MT","NL","PL","PT","RO","SK","SI","ES","SE","NO","IS","LI"];if(!0===config.show_locations.EU&&euCountries.includes(countryCode))return tuDebug&&console.log("UC: Showing consent widget for all EU countries",countryCode),!0;if(Array.isArray(config.show_locations.EU)&&config.show_locations.EU.includes(countryCode))return tuDebug&&console.log("UC: Showing consent widget for specific EU country",countryCode),!0}return!(!config.show_locations[countryCode]||!(!0===config.show_locations[countryCode]||Array.isArray(config.show_locations[countryCode])&&config.show_locations[countryCode].includes(regionCode)))&&(tuDebug&&console.log("UC: Showing consent widget for specific country & region",countryCode,regionCode),!0)},updateCookieConsent=hide=>{if(!hide)return tuDebug&&console.log("UC: Showing consent widget"),tuPSLHide&&jQuery("#usercentrics-psl, .usercentrics-psl").show(),jQuery(tuToggle).show(),UC_UI.isConsentRequired()?UC_UI.showFirstLayer():UC_UI.closeCMP();tuDebug&&console.log("UC: Hiding consent widget"),tuPSLHide&&jQuery("#usercentrics-psl, .usercentrics-psl").hide(),jQuery(tuToggle).hide(),UC_UI.areAllConsentsAccepted()||UC_UI.acceptAllConsents().then(()=>{tuDebug&&console.log("UC: All consents have been accepted."),UC_UI.closeCMP().then(()=>{tuDebug&&console.log("UC: CMP Widget has been closed.")})})},main=()=>{if("undefined"==typeof UC_UI)return console.error("Usercentrics not loaded");const query_hide=""===getQueryParams("enable-usercentrics"),sessionStorageData=getSessionStorage("uc_user_country");if(!sessionStorageData)return void console.warn("UC: WARNING - Session storage value for 'uc_user_country' not found. Are you using Usercentrics v2?");if(void 0===sessionStorageData.regionCode)return;const{regionCode:regionCode,countryCode:countryCode}=sessionStorageData;if(tuDebug&&console.log("UC: TERMAGEDDON USERCENTRICS (SESSION STORAGE)\nCountry Code: "+countryCode+"\nRegion Code: "+regionCode),query_hide)return tuDebug&&console.log("UC: Enabling due to query parameter override.","Showing Usercentrics"),updateCookieConsent(!1);const shouldShow=shouldShowBasedOnLocation(countryCode,regionCode);updateCookieConsent(!shouldShow)};window.addEventListener("UC_UI_INITIALIZED",main)}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment