Home / eCommerce / META PIXEL BASE + MANUAL ADVANCED MATCHING (WooCommerce/WordPress)
Duplicate Snippet

Embed Snippet on Your Site

META PIXEL BASE + MANUAL ADVANCED MATCHING (WooCommerce/WordPress)

Code Preview
php
<?php
/**
 * =============================================================================
 * META PIXEL BASE + MANUAL ADVANCED MATCHING (WooCommerce/WordPress)
 * Pixel ID:
 * =============================================================================
 * IMPORTANT:
 * - Remove any other hard-coded Meta Pixel base code (to avoid duplicates).
 * - Only run this if you have user consent to send PII.
 * =============================================================================
 */
add_action('wp_head', 'voelgoed_meta_pixel_base_with_advanced_matching', 20);
function voelgoed_meta_pixel_base_with_advanced_matching() {
    // If you run a consent system, gate this here.
    // Example (pseudo): if (function_exists('your_consent_fn') && !your_consent_fn()) return;
    $pixel_id = '';
    $am = []; // Advanced Matching object
    // Logged-in user data
    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
        if ( $user && ! empty($user->user_email) ) {
            $am['em'] = strtolower(trim($user->user_email));
        }
        if ( $user && ! empty($user->first_name) ) {
            $am['fn'] = strtolower(trim($user->first_name));
        }
        if ( $user && ! empty($user->last_name) ) {
            $am['ln'] = strtolower(trim($user->last_name));
        }
        // external_id can help matching (use a stable internal ID)
        $am['external_id'] = (string) $user->ID;
    }
    // WooCommerce billing data (if available)
    if ( function_exists('WC') && WC()->customer ) {
        $phone = WC()->customer->get_billing_phone();
        if ( $phone ) {
            // Normalize phone to digits (Meta recommends consistent formatting)
            if ( function_exists('wc_sanitize_phone_number') ) {
                $phone = wc_sanitize_phone_number($phone);
            } else {
                $phone = preg_replace('/\D+/', '', $phone);
            }
            if ( ! empty($phone) ) {
                $am['ph'] = $phone;
            }
        }
        $city = WC()->customer->get_billing_city();
        if ( $city ) $am['ct'] = strtolower(trim($city));
        $state = WC()->customer->get_billing_state();
        if ( $state ) $am['st'] = strtolower(trim($state));
        $postcode = WC()->customer->get_billing_postcode();
        if ( $postcode ) $am['zp'] = preg_replace('/\s+/', '', trim($postcode));
        $country = WC()->customer->get_billing_country();
        if ( $country ) $am['country'] = strtolower(trim($country));
    }
    ?>
    <!-- Meta Pixel Code (Base + Manual Advanced Matching) -->
    <script>
      !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');
      <?php if ( ! empty($am) ) : ?>
        fbq('init', <?php echo wp_json_encode($pixel_id); ?>, <?php echo wp_json_encode($am); ?>);
      <?php else : ?>
        fbq('init', <?php echo wp_json_encode($pixel_id); ?>);
      <?php endif; ?>
      fbq('track', 'PageView');
    </script>
    <noscript>
      <img alt="" height="1" width="1" style="display:none"
      src="https://www.facebook.com/tr?id=<?php echo esc_attr($pixel_id); ?>&ev=PageView&noscript=1"/>
    </noscript>
    <!-- End Meta Pixel Code -->
    <?php
}

Comments

Add a Comment