Shopify - Pixel Tracking

Ruler Analytics Shopify Pixel Tracking for Advanced Checkout in Shopify

**Overview **

This guide walks you through adding the Ruler Custom Pixel to your Shopify store. Once installed, the pixel will track page views and completed checkout events, enabling Ruler Analytics to attribute revenue to the correct marketing sources.

The pixel does two things:

  • identifies and tracks visitors across your checkout flow via a Shopify cookie
  • fires a conversion event when a purchase is completed, passing order details back to Ruler

Please ensure you have the following ready before starting:

  • Your Ruler Site ID (available in your Ruler Analytics account settings)

  • Admin access to your Shopify store

  • Access to Shopify Admin → Settings → Customer Events

📘

Note

You must replace site_id in the pixel code with your actual Ruler Site ID before saving. The pixel will not function correctly without this.

Implementation Steps

  • Go to Customer Events in Shopify Admin

  • Navigate to Settings → Customer Events in your Shopify Admin panel.

  • Click ‘Add custom pixel’ and give it a name (e.g. “Ruler Analytics”).

  • Copy the full pixel code from the section below and paste it into the code editor.

window.__raconfig = {
    uid: '<site_id>',
    __shopify: {}
}
 
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
 
function bootstrap(callback) {
    var ra = document.createElement('script');
    ra.type = 'text/javascript';
    ra.src = 'https://ruler.nyltx.com/lib/1.0/ra-bootstrap.min.js';
    __raconfig.__ready = callback;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ra, s);
}
 
function setShopifyConfig(clientId, href) {
    __raconfig.__shopify.href = href;
    __raconfig.__shopify.raid = `SHOPIFY:${clientId}`;
    bootstrap(() => { });
}
 
function retryFindShopifyClientId(retries, interval, href) {
    let attempts = 0;
    const findClientIdAttempt = () => {
        const shopifyClientId = readCookie("_shopify_y");
        if (shopifyClientId) {
            setShopifyConfig(shopifyClientId, href);
            clearInterval(timer);
        } else if (++attempts >= retries) {
            clearInterval(timer);
        }
    };
    const timer = setInterval(findClientIdAttempt, interval);
}
 
analytics.subscribe("page_viewed", event => {
    retryFindShopifyClientId(20, 500, event.context.document.location.href);
});
 
analytics.subscribe("checkout_completed", event => {
    function trackConversion() {
        const checkout = event.data.checkout;
        const checkoutTotalPrice = checkout.totalPrice.amount;
        const payload = {
            'email': hashedValue,
            'orderId': event.data.checkout.order.id,
            'currency': event.data.checkout.currencyCode,
            'totalPrice': checkoutTotalPrice
        };
        const itemNames = checkout.lineItems.map(item => {
            return item.product?.title || item.title;
        });
        itemNames.forEach((name, index) => {
            payload[`item${index + 1}`] = name;
        });
        RulerAnalytics.trackConversionAsync(payload);
    }
    if (!RulerAnalytics) {
        bootstrap(trackConversion);
    } else {
        trackConversion();
    }
});

  • Find site_id in line 2 of the code and replace it with your Ruler Site ID, you can find that in your Ruler account under Settings > Site Setup.
  • Click ‘Save’, then set the pixel status to ‘Connected’ to activate it.

Troubleshooting

Pixel not tracking conversions Confirm the pixel status is set to ‘Connected’ in Shopify Customer Events Verify your Site ID has been correctly substituted for site_id Check that the pixel code has been saved without any modifications to the script logic

Visitor sessions not being attributed The _shopify_y cookie must be present. This is set by Shopify automatically for most stores Ensure cookies are not being blocked by a consent management platform before the pixel fires

Need further help? Contact the Ruler Analytics support team or your account manager.