Skip to content

Instantly share code, notes, and snippets.

@ceaksan
Created April 17, 2026 20:38
Show Gist options
  • Select an option

  • Save ceaksan/861f4e467e53f4d136818e532c5535d2 to your computer and use it in GitHub Desktop.

Select an option

Save ceaksan/861f4e467e53f4d136818e532c5535d2 to your computer and use it in GitHub Desktop.
Shopify Custom Pixel - Market-Aware Event Enrichment via cookie bridge.
/**
* Shopify Custom Pixel - Market-Aware Event Enrichment
*
* Reads the market cookie written by the market bridge (shopify_market_bridge.js)
* from the pixel sandbox via browser.cookie.get() and enriches
* storefront events. For checkout events, Shopify's native
* event.data.checkout.localization object is used as the authoritative source,
* and the cookie value is used for cross-validation.
*
* Drop this code into Shopify Admin > Settings > Customer Events > Custom Pixel.
*
* Pairs with:
* - storageHelper: https://gist.github.com/ceaksan/26c8a59aa8a518732a9a40da967d8cd2
* - market bridge: https://gist.github.com/ceaksan/shopify_market_bridge.js
*
* @see https://ceaksan.com/tr/shopify-markets-cookie-bridge-consent-aware-veri-koprusu
* @license MIT
*/
// Consent state (from init payload, stable for page lifetime)
var consentGranted = init.customerPrivacy.analyticsProcessingAllowed;
// Market data cache
var cachedMarket = null;
async function getMarketData() {
if (cachedMarket) return cachedMarket;
var raw = await browser.cookie.get("_shopify_m");
if (raw) {
try {
cachedMarket = JSON.parse(decodeURIComponent(raw));
return cachedMarket;
} catch (e) {
console.warn("[pixel] Market cookie parse error:", e);
}
}
return null;
}
// Retry for race condition on first page load
async function getMarketDataWithRetry(retries) {
retries = retries || 3;
var data = await getMarketData();
if (data) return data;
for (var i = 0; i < retries; i++) {
await new Promise(function (resolve) {
setTimeout(resolve, 150);
});
cachedMarket = null;
data = await getMarketData();
if (data) return data;
}
return null;
}
// Storefront events: read market from cookie
analytics.subscribe("page_viewed", async function (event) {
if (!consentGranted) return;
var market = await getMarketData();
// Forward event + market data to GA4/Ads
// (See https://ceaksan.com/tr/shopify-multi-market-ga4-google-ads-olcum-id-eslemesi)
});
analytics.subscribe("product_added_to_cart", async function (event) {
if (!consentGranted) return;
var market = await getMarketData();
var cartLine = event.data.cartLine;
// Cross-validate market.currency vs cartLine.cost.totalAmount.currencyCode
if (
market &&
market.currency &&
market.currency !== cartLine.cost.totalAmount.currencyCode
) {
console.warn(
"[pixel] Currency mismatch: cookie=" +
market.currency +
" cart=" +
cartLine.cost.totalAmount.currencyCode,
);
}
});
// Checkout events: native localization is authoritative
analytics.subscribe("checkout_started", async function (event) {
if (!consentGranted) return;
var localization = event.data.checkout.localization;
// localization.country.isoCode, localization.language.isoCode
// localization.market.handle, localization.market.id
// Optional cross-validation with cookie
var market = await getMarketData();
if (market && market.country !== localization.country.isoCode) {
console.warn(
"[pixel] Market mismatch: cookie=" +
market.country +
" native=" +
localization.country.isoCode,
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment