-
-
Save Linkyc89/015496b469883500c889fb7fbcfa3e6b to your computer and use it in GitHub Desktop.
Country listener (CZ/SK)
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
| <script> | |
| (function(){ | |
| var SELECT_ID = 'ShipmentCountrySelect'; | |
| var KEY = 'inweek_country'; // localStorage key | |
| var PUSHED_ONCE = false; | |
| function push(c){ | |
| if (!c || PUSHED_ONCE) return; | |
| window.dataLayer = window.dataLayer || []; | |
| dataLayer.push({event:'country_change', country: c}); | |
| PUSHED_ONCE = true; | |
| } | |
| function save(c){ | |
| try { localStorage.setItem(KEY, c); } catch(e){} | |
| } | |
| function readSaved(){ | |
| try { return (localStorage.getItem(KEY) || '').toLowerCase(); } catch(e){ return ''; } | |
| } | |
| function readFromSelect(){ | |
| var el = document.getElementById(SELECT_ID); | |
| if (!el) return ''; | |
| var val = (el.value || '').toLowerCase(); | |
| if (val === 'cz' || val === 'sk') return val; | |
| var opt = el.options && el.options[el.selectedIndex]; | |
| var txt = (opt && opt.text || '').toLowerCase(); | |
| if (/[čc]esk/.test(txt)) return 'cz'; | |
| if (/slovensk/.test(txt)) return 'sk'; | |
| return ''; | |
| } | |
| // 1) zkus select | |
| var c1 = readFromSelect(); | |
| if (c1){ save(c1); push(c1); } | |
| // 2) když select není, použij uloženou hodnotu (funguje i na thank-you) | |
| if (!c1){ | |
| var c2 = readSaved(); | |
| if (c2 === 'cz' || c2 === 'sk') push(c2); | |
| } | |
| // 3) sleduj změny selectu a ukládej | |
| document.addEventListener('change', function(e){ | |
| if (e.target && e.target.id === SELECT_ID){ | |
| var c = readFromSelect(); | |
| if (c){ save(c); push(c); } | |
| } | |
| }); | |
| // 4) kdyby select dorazil později (SPA / lazy load) | |
| new MutationObserver(function(){ | |
| var c = readFromSelect(); | |
| if (c){ save(c); push(c); } | |
| }).observe(document.documentElement, {childList:true, subtree:true}); | |
| })(); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment