1. Knowledge base
  2. Meta (Facebook / Instagram)

How to create a custom pixel for Meta in Shopify

Follow the steps in this guide to create a custom pixel for Meta in Shopify

Part 1: Create a custom pixel "PM - Meta"

  1. Click "Settings"

    Google Chrome, Mail, and Brave Browser Workflow - Step 1-1

  2. Click "Customer events"

    Set Up Custom Pixel for Customer Events in Shopify - Step 3


  3. Click "Add custom pixel"

    Set Up Custom Pixel for Customer Events in Shopify - Step 4


  4. Click the "Pixel name" field and type "PM - Meta"

    Set Up Custom Pixel for Customer Events in Shopify - Step 7

  5. Click "Add pixel"

    Set Up Custom Pixel for Customer Events in Shopify - Step 7 (1)
  6. Copy this script
    // Consent Configuration
    let customerPrivacyStatus = init.customerPrivacy;
    let cc_statistics = false;
    let cc_marketing = false;

    // Meta Pixel Code
    !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');

    // Update consent
    function updateConsent(){
      if (!customerPrivacyStatus || 
        customerPrivacyStatus.analyticsProcessingAllowed === undefined || 
        customerPrivacyStatus.marketingAllowed === undefined) {
      try {
        const cmp_a = browser.cookie.get('_cmp_a');
        const cookiedecoded = JSON.parse(decodeURIComponent(cmp_a));
        cc_statistics = cookiedecoded?.purposes?.a ?? cc_statistics;
        cc_marketing = cookiedecoded?.purposes?.m ?? cc_marketing;
      } catch (e) {
        // Handle any errors that might occur
      }
    } else {
      cc_statistics = customerPrivacyStatus.analyticsProcessingAllowed;
      cc_marketing = customerPrivacyStatus.marketingAllowed;
    }
    fbq('consent', cc_marketing ? 'grant' : 'revoke');
    }
    updateConsent();
    fbq('init', 'YOUR_DATASET_ID'); // Replace YOUR_DATASET_ID


    // Event - Visitor Consent Collected
    api.customerPrivacy.subscribe('visitorConsentCollected', (event) => {
    customerPrivacyStatus = event.customerPrivacy;
      updateConsent();
    })

    // Event - Page view
    analytics.subscribe("page_viewed", (event) => {
      fbq('track', 'PageView');
    });

    // Event - Product view
    analytics.subscribe("product_viewed", (event) => {
      fbq('track', 'ViewContent', {
        content_ids:  [event.data?.productVariant?.id],
        content_name: event.data?.productVariant?.title,
        currency: event.data?.productVariant?.price.currencyCode,
        value: event.data?.productVariant?.price.amount,
      });
    });

    // Event - Search
    analytics.subscribe("search_submitted", (event) => {
      fbq('track', 'Search', {
        search_string: event.searchResult.query
      });
    });

    // Event - Add to cart
    analytics.subscribe("product_added_to_cart", (event) => {
      fbq('track', 'AddToCart', {
        content_ids: [event.data?.cartLine?.merchandise?.productVariant?.id],
        content_name: event.data?.cartLine?.merchandise?.productVariant?.title,
        currency: event.data?.cartLine?.merchandise?.productVariant?.price?.currencyCode,
        value: event.data?.cartLine?.merchandise?.productVariant?.price.amount,
      });
    });

    // Event - Add payment method
    analytics.subscribe("payment_info_submitted", (event) => {
      fbq('track', 'AddPaymentInfo');
    });

    // Event - Begin checkout
    analytics.subscribe("checkout_started", (event) => {
      fbq('track', 'InitiateCheckout');
    });

    // Event - Purchase (Sent Server-side by ProfitMetrics.io)
    // analytics.subscribe("checkout_completed", (event) => {
    //   fbq('track', 'purchase', {
    //     currency: event.data?.checkout?.currencyCode,
    //     value: event.data?.checkout?.totalPrice?.amount,
    //   });
    // });
  7. Remove all existing text in the Code field and paste the script into the Code field. Remember to replace YOUR_PIXEL_ID with your actual pixel id / dataset id

    Set Up Custom Pixel for Customer Events in Shopify - Step 15

  8. Click "Save"

    Set Up Custom Pixel for Customer Events in Shopify - Step 15 (1)

  9. Click "Connect"


    Set Up Custom Pixel for Customer Events in Shopify - Step 16

  10. Click "Connect" again

    Set Up Custom Pixel for Customer Events in Shopify - Step 17