Created
April 17, 2026 20:38
-
-
Save ceaksan/40a13d53efb4b8043e62e0a4c25aa845 to your computer and use it in GitHub Desktop.
Shopify Market Bridge - Consent-Gated Market Cookie Writer. Pairs with storageHelper and custom pixel to bridge Liquid localization data into the Shopify pixel sandbox.
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
| /** | |
| * Shopify Market Bridge - Consent-Gated Market Cookie Writer | |
| * | |
| * Theme app extension (app embed) snippet that reads Liquid localization | |
| * data from <script id="shopify-market-data"> and writes it to a cookie | |
| * that the custom pixel sandbox can read via browser.cookie.get(). | |
| * | |
| * Requires storageHelper utility: https://gist.github.com/ceaksan/26c8a59aa8a518732a9a40da967d8cd2 | |
| * | |
| * Consent gate: Shopify.customerPrivacy.analyticsProcessingAllowed() | |
| * Listens to the visitorConsentCollected DOM event to react to | |
| * consent changes. On revoke the cookie is deleted. | |
| * | |
| * @see https://ceaksan.com/tr/shopify-markets-cookie-bridge-consent-aware-veri-koprusu | |
| * @license MIT | |
| */ | |
| (function () { | |
| var MARKET_KEY = "_shopify_m"; | |
| // --- Market Data Source --- | |
| function getMarketData() { | |
| var el = document.getElementById("shopify-market-data"); | |
| if (el) { | |
| try { | |
| return JSON.parse(el.textContent); | |
| } catch (e) { | |
| console.warn("[marketBridge] parse error:", e); | |
| } | |
| } | |
| // Fallback: window.Shopify global | |
| if (typeof Shopify !== "undefined") { | |
| return { | |
| market: null, | |
| country: Shopify.country || null, | |
| language: Shopify.locale || null, | |
| locale: Shopify.locale || null, | |
| currency: Shopify.currency ? Shopify.currency.active : null, | |
| shop: Shopify.shop || null, | |
| }; | |
| } | |
| return null; | |
| } | |
| // --- Consent-Gated Writer --- | |
| function writeMarketData() { | |
| var data = getMarketData(); | |
| if (!data) return; | |
| if (!storageHelper.hasChanged(MARKET_KEY, data)) return; | |
| storageHelper.set(MARKET_KEY, data, { maxAge: 2592000 }); | |
| } | |
| function deleteMarketData() { | |
| storageHelper.remove(MARKET_KEY); | |
| } | |
| // --- Consent Handler --- | |
| var listenerAttached = false; | |
| function onConsentChange() { | |
| if ( | |
| typeof Shopify !== "undefined" && | |
| typeof Shopify.customerPrivacy !== "undefined" && | |
| Shopify.customerPrivacy.analyticsProcessingAllowed() | |
| ) { | |
| writeMarketData(); | |
| } else { | |
| deleteMarketData(); | |
| } | |
| } | |
| function attachConsentListener() { | |
| if (listenerAttached) return; | |
| document.addEventListener("visitorConsentCollected", onConsentChange); | |
| listenerAttached = true; | |
| } | |
| function handleConsent() { | |
| if ( | |
| typeof Shopify === "undefined" || | |
| typeof Shopify.customerPrivacy === "undefined" | |
| ) { | |
| attachConsentListener(); | |
| // Polling: catch late-loaded customerPrivacy API | |
| var attempts = 0; | |
| var poll = setInterval(function () { | |
| attempts++; | |
| if ( | |
| typeof Shopify !== "undefined" && | |
| typeof Shopify.customerPrivacy !== "undefined" | |
| ) { | |
| clearInterval(poll); | |
| onConsentChange(); | |
| } | |
| if (attempts >= 10) clearInterval(poll); | |
| }, 500); | |
| return; | |
| } | |
| onConsentChange(); | |
| attachConsentListener(); | |
| } | |
| handleConsent(); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment