| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| add_action('wp_head', 'voelgoed_meta_pixel_base_with_advanced_matching', 20);
|
| function voelgoed_meta_pixel_base_with_advanced_matching() {
|
|
|
|
|
|
|
|
|
| $pixel_id = '';
|
|
|
| $am = [];
|
|
|
|
|
| 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));
|
| }
|
|
|
|
|
| $am['external_id'] = (string) $user->ID;
|
| }
|
|
|
|
|
| if ( function_exists('WC') && WC()->customer ) {
|
| $phone = WC()->customer->get_billing_phone();
|
| if ( $phone ) {
|
|
|
| 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