Meta Pixel Bridge (Shopify Custom Pixel)
An optional Shopify Custom Pixel snippet that fires Meta (fbq) and TikTok (ttq) browser-side
events for upper-funnel activity, sharing Shopify's own event identifier with the server-side events AxiTrace already
sends via Conversions API - so Meta and TikTok can deduplicate the pair instead of double-counting.
AxiTrace's Shopify app already forwards your events server-side via Facebook Conversions API and TikTok Events API - that pipeline works today and needs nothing from this page. A redundant browser pixel is a second, independent copy of the same events, fired directly from the customer's browser via a Shopify Custom Pixel . Meta and TikTok both support receiving the same logical event twice (once server-side, once browser-side) and merging them into one, as long as both copies share the same event ID. Having both improves match quality and gives you a browser-side signal even if a customer's network briefly drops the server-side call.
This bridge covers PageView, ViewContent, AddToCart and InitiateCheckout only. It never subscribes
to Shopify's checkout_completed event, and it must not be extended to do so without re-reading this
section.
Purchase carries real revenue, and Shopify itself documents checkout_completed as lossy (Shop Pay
accelerated checkouts and platform outages can skip it entirely). AxiTrace's server-side Conversions API
integration already sends Purchase reliably from your order webhooks. Adding a second, browser-fired Purchase
event on top of that - matched only by an unverified cross-pixel-instance event ID - risks
double-counting or under-counting real revenue if the identity assumption ever turns out to be wrong for your
store. That risk is not worth taking for a number that feeds your ad account's optimization and your own
revenue reporting. Purchase stays 100% CAPI-only by design.
How the event ID matching works
Every event Shopify's Customer Events system delivers to a pixel carries a Shopify-generated event.id.
Both the AxiTrace app pixel (already installed with your Shopify connection, forwarding to our servers) and this
Custom Pixel are independent subscribers to the same underlying Shopify event - so they both
receive the same event.id for the same customer action. The Custom Pixel below passes that ID straight
through to Meta as {eventID: event.id} and to TikTok as {event_id: event.id}, matching
exactly what AxiTrace's server-side forwarding sends as its own event_id for the corresponding event.
That holds for every event the AxiTrace app pixel sends. With the
AxiTrace storefront embed on, the app pixel
sends only the checkout events, and page views, product views and add to carts come from the AxiTrace web SDK on
the page, with the SDK's own event ids. For those three events a browser copy carrying Shopify's
event.id has no server twin to pair with, so Meta would count both. The current snippet (v2) handles
this for you.
Snippet v2 and the storefront embed
The first line of the current snippet reads // AxiTrace Meta Pixel Bridge v2 - Shopify Custom Pixel.
On every page it reads the first-party page marker _axi_em that the storefront embed and the AxiTrace
web SDK write, with the same rule the AxiTrace app pixel uses:
- The embed owns the page (the marker reads
1.followed by a timestamp of the current page): the bridge skips PageView, ViewContent and AddToCart. AxiTrace sends them to Conversions API from the SDK; Meta receives them once, server-side. - The embed ran but the SDK has not answered yet (
p.): the bridge waits up to 4 seconds and then follows whichever state the marker reaches; if it never becomes1., it fires. - No marker, a marker from an earlier page, or
0.(the SDK could not send): the app pixel sends the page as before, and the bridge fires with Shopify'sevent.idexactly like v1. - InitiateCheckout always fires: checkout runs where the embed never does, so its server twin always comes from the app pixel.
If you installed the bridge before November 2026 and use (or plan to turn on) the storefront embed, replace the code of your custom pixel with the current snippet. Without the embed, v1 and v2 behave the same. The Ad-blocker protection card in your AxiTrace panel warns you when the embed is live and storefront events from your store go to Meta.
Setup
- Open your workspace's Facebook Conversions API connection page in the AxiTrace admin panel - a personalized version of the snippet below, with your Pixel ID (and TikTok Pixel ID, if connected) already filled in, is shown there once your Pixel ID is saved.
- In your Shopify admin, go to Settings → Customer events.
- Click Add custom pixel, name it (e.g. "AxiTrace Meta Pixel Bridge"), and paste the snippet into the code editor. Updating an existing bridge: open that custom pixel, replace all of its code with the current snippet and save.
- Set the pixel's data-collection permissions the same way as the AxiTrace app pixel (see the consent callout below).
- Save and connect the pixel.
Example snippet
The example below uses placeholder pixel IDs. The copy in your admin panel already has your real IDs substituted.
// AxiTrace Meta Pixel Bridge v2 - Shopify Custom Pixel
// Fires Meta (fbq) and, when configured, TikTok (ttq) browser-side
// events for upper-funnel activity only, sharing Shopify's event.id
// with AxiTrace's server-side Conversions API forwarding so
// Meta/TikTok can deduplicate the pair instead of double-counting.
//
// Scope: PageView, ViewContent, AddToCart, InitiateCheckout ONLY.
// Purchase (checkout_completed) is intentionally NOT subscribed here
// - see the AxiTrace docs page linked from your admin panel for why.
//
// v2: when the AxiTrace storefront embed owns a page (cookie _axi_em
// reads 1.<timestamp> for the current page), AxiTrace sends that
// page's PageView, ViewContent and AddToCart with its own event ids,
// so this bridge skips them and fires InitiateCheckout only. On every
// other page it fires exactly as before.
(function () {
"use strict";
var META_PIXEL_ID = "YOUR_META_PIXEL_ID";
// --- Storefront embed hand-over (same rule as the AxiTrace app
// pixel): p.<ms> the embed ran on this page, 1.<ms> the AxiTrace
// web SDK owns the page, 0.<ms> it could not send. A marker older
// than this page counts as absent; p waits up to 4 s for 1 or 0. ---
var EMBED_MARKER_COOKIE = "_axi_em";
var EMBED_MARKER_SHAPE = /^([p01])\.(\d{13})$/;
var EMBED_HANDOVER_MS = 4000;
var EMBED_POLL_MS = 250;
var EMBED_PAGE_SLACK_MS = 1000;
var pageViewedAt = 0;
var embedChain = Promise.resolve();
function readMarker() {
if (typeof browser === "undefined" || !browser || !browser.cookie) {
return Promise.resolve(null);
}
return Promise.resolve()
.then(function () { return browser.cookie.get(EMBED_MARKER_COOKIE); })
.then(function (raw) {
var match = EMBED_MARKER_SHAPE.exec(raw || "");
return match ? { state: match[1], ts: parseInt(match[2], 10) } : null;
}, function () { return null; });
}
function embedOwnsPage(pageAt) {
return readMarker().then(function (marker) {
if (!marker || marker.ts < pageAt - EMBED_PAGE_SLACK_MS) return false;
if (marker.state === "1") return true;
if (marker.state === "0") return false;
var deadline = marker.ts + EMBED_HANDOVER_MS;
function poll() {
if (Date.now() >= deadline) return false;
return new Promise(function (resolve) {
setTimeout(resolve, Math.max(1, Math.min(EMBED_POLL_MS, deadline - Date.now())));
}).then(readMarker).then(function (next) {
if (!next || next.state === "0") return false;
if (next.state === "1") return true;
return poll();
});
}
return poll();
});
}
// Runs fire() unless the storefront embed owns the page. One event
// at a time, so a held PageView is never overtaken.
function storefrontEvent(name, event, fire) {
var eventAt = Date.parse(get(event, ["timestamp"], "")) || Date.now();
if (name === "page_viewed" && !pageViewedAt) pageViewedAt = eventAt;
var pageAt = pageViewedAt || eventAt;
var decided = embedChain.then(function () { return embedOwnsPage(pageAt); })
.then(function (owned) { if (!owned) fire(); }, function () { fire(); });
embedChain = decided.then(function () {}, function () {});
}
function get(obj, path, fallback) {
var cur = obj;
for (var i = 0; i < path.length; i++) {
if (cur === null || typeof cur === "undefined") {
return fallback;
}
cur = cur[path[i]];
}
return (cur === null || typeof cur === "undefined") ? fallback : cur;
}
function pageUrl(event) {
return get(event, ["context", "document", "location", "href"], undefined);
}
// --- Meta Pixel loader (init only - this snippet controls every
// track() call explicitly, no auto-tracked PageView) ---
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version="2.0";n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,"script","https://connect.facebook.net/en_US/fbevents.js");
fbq("init", META_PIXEL_ID);
var TIKTOK_PIXEL_ID = "YOUR_TIKTOK_PIXEL_ID";
// --- TikTok Pixel loader (load only - this snippet controls
// every track() call explicitly, no auto-tracked page event) ---
!function (w, d, t) {
w.TiktokAnalyticsObject = t;
var ttq = (w[t] = w[t] || []);
ttq.methods = ["page", "track", "identify", "instances", "debug", "on", "off",
"once", "ready", "alias", "group", "enableCookie", "disableCookie",
"holdConsent", "revokeConsent", "grantConsent"];
ttq.setAndDefer = function (obj, method) {
obj[method] = function () {
obj.push([method].concat(Array.prototype.slice.call(arguments, 0)));
};
};
for (var i = 0; i < ttq.methods.length; i++) {
ttq.setAndDefer(ttq, ttq.methods[i]);
}
ttq.load = function (pixelId, options) {
var src = "https://analytics.tiktok.com/i18n/pixel/events.js";
ttq._i = ttq._i || {};
ttq._i[pixelId] = [];
ttq._i[pixelId]._u = src;
ttq._t = ttq._t || {};
ttq._t[pixelId] = +new Date();
ttq._o = ttq._o || {};
ttq._o[pixelId] = options || {};
var script = d.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = src + "?sdkid=" + pixelId + "&lib=" + t;
var firstScript = d.getElementsByTagName("script")[0];
firstScript.parentNode.insertBefore(script, firstScript);
};
ttq.load(TIKTOK_PIXEL_ID);
}(window, document, "ttq");
// Upper-funnel Shopify Customer Events only. checkout_completed
// (Purchase) is NEVER subscribed here by design.
analytics.subscribe("page_viewed", function (event) {
storefrontEvent("page_viewed", event, function () {
var url = pageUrl(event);
fbq("track", "PageView", { page_url: url }, { eventID: event.id });
if (typeof ttq !== "undefined") {
ttq.track("Pageview", { url: url }, { event_id: event.id });
}
});
});
analytics.subscribe("product_viewed", function (event) {
storefrontEvent("product_viewed", event, function () {
var variant = get(event, ["data", "productVariant"], null);
var url = pageUrl(event);
var value = Number(get(variant, ["price", "amount"], 0)) || 0;
var currency = get(variant, ["price", "currencyCode"], "");
var productId = String(get(variant, ["product", "id"], ""));
fbq("track", "ViewContent", {
content_ids: [productId],
content_type: "product",
value: value,
currency: currency,
page_url: url
}, { eventID: event.id });
if (typeof ttq !== "undefined") {
ttq.track("ViewContent", {
contents: [{ content_id: productId, content_type: "product" }],
value: value,
currency: currency
}, { event_id: event.id });
}
});
});
analytics.subscribe("product_added_to_cart", function (event) {
storefrontEvent("product_added_to_cart", event, function () {
var line = get(event, ["data", "cartLine"], null);
var variant = get(line, ["merchandise"], null);
var qty = Number(get(line, ["quantity"], 1)) || 1;
var totalAmount = Number(get(line, ["cost", "totalAmount", "amount"], 0)) || 0;
var price = Number(get(variant, ["price", "amount"], 0)) || 0;
var value = totalAmount > 0 ? totalAmount : price * qty;
var currency = get(line, ["cost", "totalAmount", "currencyCode"], "") ||
get(variant, ["price", "currencyCode"], "");
var productId = String(get(variant, ["product", "id"], ""));
var url = pageUrl(event);
fbq("track", "AddToCart", {
content_ids: [productId],
content_type: "product",
value: value,
currency: currency,
page_url: url
}, { eventID: event.id });
if (typeof ttq !== "undefined") {
ttq.track("AddToCart", {
contents: [{ content_id: productId, content_type: "product", quantity: qty }],
value: value,
currency: currency
}, { event_id: event.id });
}
});
});
analytics.subscribe("checkout_started", function (event) {
var checkout = get(event, ["data", "checkout"], null);
var lineItems = get(checkout, ["lineItems"], []) || [];
var value = Number(get(checkout, ["totalPrice", "amount"], 0)) || 0;
var currency = get(checkout, ["totalPrice", "currencyCode"], "");
var url = pageUrl(event);
var contentIds = lineItems.map(function (li) {
return String(get(li, ["variant", "product", "id"], ""));
});
fbq("track", "InitiateCheckout", {
content_ids: contentIds,
content_type: "product",
value: value,
currency: currency,
num_items: lineItems.length,
page_url: url
}, { eventID: event.id });
if (typeof ttq !== "undefined") {
ttq.track("InitiateCheckout", {
contents: contentIds.map(function (id) {
return { content_id: id, content_type: "product" };
}),
value: value,
currency: currency
}, { event_id: event.id });
}
});
// NOTE: checkout_completed / Purchase is intentionally NOT
// subscribed here. AxiTrace's server-side Conversions API
// integration already sends Purchase reliably; adding a second,
// unverified client-side Purchase path risks double- or
// mis-counted revenue. Purchase coverage stays CAPI-only.
})();
Shopify gates Custom Pixels behind customer privacy consent by default. Set this pixel's permissions the same
way as the AxiTrace app pixel: analytics = false, marketing = false,
sale_of_data = disabled - i.e. do not require marketing/analytics consent to
run it. If you leave the default gating in place, EEA/UK visitors who decline cookies will silently produce
zero bridge coverage while everything still looks "connected". This exact failure mode has previously affected
the AxiTrace app pixel itself, so double-check this setting explicitly rather than assuming the default is fine.
Verifying deduplication
- Open Meta Events Manager → your pixel → Test Events.
- Browse your store in the same tab Test Events is watching: view a product, add it to cart, start checkout.
- For each event, confirm you see one entry with status "Deduplicated" (or two entries, browser + server, explicitly matched) rather than two separate, unmatched entries. With the storefront embed on, PageView, ViewContent and AddToCart show only the server entry, which is the expected result of v2.
- If you also have a TikTok connection, repeat the same check in TikTok's Events Manager test tool.
- If events show as separate/unmatched instead of deduplicated, see the Pixel Deduplication troubleshooting guide.
- Covers PageView, ViewContent, AddToCart, InitiateCheckout - browser-side, in addition to your existing server-side Conversions API events.
- Purchase stays CAPI-only. This snippet never subscribes to
checkout_completed. - Snippet v2 skips PageView, ViewContent and AddToCart on pages the AxiTrace storefront embed owns; replace an older snippet when you use the embed.
- Set consent permissions to match the AxiTrace app pixel, or coverage silently drops to zero for consent-declining visitors.
- Verify with Meta/TikTok Test Events before relying on the reported coverage numbers.