_post->sq->doseo) { $isAMP = false; if (!$this->_post->sq->do_fpixel) { add_filter('sq_facebook_pixel', array($this, 'returnFalse')); return; } if(function_exists('is_user_logged_in') && is_user_logged_in() && !SQ_Classes_Helpers_Tools::getOption('sq_tracking_logged_users') ){ return; } if (function_exists('is_amp_endpoint') && is_amp_endpoint()) { $isAMP = true; } if (function_exists('is_amp') && is_amp()) { $isAMP = true; } if (SQ_Classes_Helpers_Tools::getOption('sq_auto_amp')) { $isAMP = true; } if (!$isAMP) { add_filter('sq_facebook_pixel', array($this, 'generatePixel')); add_filter('sq_facebook_pixel', array($this, 'packPixel'), 99); } else { add_filter('sq_facebook_pixel', array($this, 'returnFalse')); add_filter('sq_facebook_pixel_amp', array($this, 'packPixelAMP'), 99); } } else { add_filter('sq_facebook_pixel', array($this, 'returnFalse')); } } public function addOGPrefix($prefix = '') { $prefix .= 'og: http://ogp.me/ns#'; return $prefix; } public function generatePixel($events = array()) { $codes = json_decode(json_encode(SQ_Classes_Helpers_Tools::getOption('codes'))); if (isset($codes->facebook_pixel) && $codes->facebook_pixel <> '') { $domain = str_replace(array('http://', 'http://', 'www.'), '', get_bloginfo('url')); if ($this->isHomePage()) { $events[] = array( 'type' => 'track', 'name' => 'PageView', 'params' => array('page' => get_bloginfo('url'), 'domain' => $domain) ); } else { if (isset($this->_post->ID)) { $params['content_ids'] = array((string)$this->_post->ID); } $params['content_type'] = $this->_post->post_type; if ($this->_post->post_type == 'category') { $category = get_category(get_query_var('cat'), false); if (isset($category->name)) { $params['content_category'] = $category->name; } } elseif ($this->_post->post_type == 'product') { $params['content_name'] = $this->_post->post_title; $cat = get_the_terms($this->_post->ID, 'product_cat'); if (!empty($cat) && !is_wp_error($cat)) { $params['content_category'] = $cat[0]->name; } if (isset($_POST['product_id']) && isset($params['content_ids']) && isset($params['content_type'])) { if (function_exists('wc_get_product') && function_exists('get_woocommerce_currency')) { if ($product = wc_get_product((int)$_POST['product_id'])) { $params['value'] = $product->get_price(); $params['currency'] = get_woocommerce_currency(); } } $events[] = array( 'type' => 'track', 'name' => 'AddToCart', 'params' => $params ); } } elseif ($this->_post->post_type == 'search') { $search = get_search_query(true); if ($search <> '') { $params['search_string'] = $search; $events[] = array( 'type' => 'track', 'name' => 'Search', 'params' => $params ); } } elseif ($this->_post->post_type == 'checkout' && isset($this->_post->ID)) { global $woocommerce; if (isset($woocommerce->cart->total) && $woocommerce->cart->total > 0) { $params['value'] = $woocommerce->cart->total; if (isset($woocommerce->cart->cart_contents) && !empty($woocommerce->cart->cart_contents)) { $quantity = 0; foreach ($woocommerce->cart->cart_contents as $product) { $quantity += $product['quantity']; } if ($quantity > 0) { $params['num_items'] = $quantity; } } $events[] = array( 'type' => 'track', 'name' => 'InitiateCheckout', 'params' => $params ); } elseif (SQ_Classes_Helpers_Tools::getIsset('key')) { $params['content_type'] = 'purchase'; global $wpdb; $sql = "SELECT `post_id` FROM `" . $wpdb->postmeta . "` WHERE `meta_key` = '_order_key' AND `meta_value`='" . SQ_Classes_Helpers_Tools::getValue('key') . "'"; if ($post = $wpdb->get_row($sql)) { if ($order = wc_get_order($post->post_id)) { $params['content_type'] = "checkout"; $params['value'] = $order->get_total(); $params['currency'] = $order->get_order_currency(); $events[] = array( 'type' => 'track', 'name' => 'Purchase', 'params' => $params ); } } } } else { $cat = get_the_terms($this->_post->ID, 'category'); if (!empty($cat) && !is_wp_error($cat)) { $params['content_category'] = $cat[0]->name; } } $params['page'] = $this->_post->url; $params['domain'] = $domain; if (isset($params['content_ids']) && isset($params['content_type'])) { $events[] = array( 'type' => 'track', 'name' => 'ViewContent', 'params' => $params ); } else { $events[] = array( 'type' => 'trackCustom', 'name' => 'GeneralEvent', 'params' => $params ); } $events[] = array( 'type' => 'track', 'name' => 'PageView', 'params' => array('page' => $params['page'], 'domain' => $params['domain']) ); } } return $this->clean($events); } public function clean($var) { if (is_array($var)) { return array_map(array($this, 'clean'), $var); } else { return is_string($var) ? ((strpos($var, '://') !== false) ? esc_url($var) : sanitize_text_field($var)) : $var; } } public function packPixel($events = '') { //If custom Facebook Pixel is set if ($this->_post->sq->fpixel) { return $this->_post->sq->fpixel; } else { if (!empty($events)) { $codes = json_decode(json_encode(SQ_Classes_Helpers_Tools::getOption('codes'))); $track = ''; foreach ($events as $event) { $track .= "fbq('" . $event['type'] . "', '" . $event['name'] . "', '" . json_encode($event['params']) . "');"; } return sprintf("" . "\n", $codes->facebook_pixel, $track, $codes->facebook_pixel); } } return false; } public function packPixelAMP() { $codes = json_decode(json_encode(SQ_Classes_Helpers_Tools::getOption('codes'))); if (isset($codes->facebook_pixel) && $codes->facebook_pixel <> '') { $events = $this->generatePixel(); $track = ''; foreach ($events as $event) { if (isset($event['params'])) { $cd = ''; foreach ($event['params'] as $key => $value) { if (is_array($value) && !empty($value)) { $cd .= sprintf('&cd[%s]=%s', $key, join(',', $value)); } else { $cd .= sprintf('&cd[%s]=%s', $key, urlencode($value)); } } } $track .= sprintf('', $codes->facebook_pixel, $event['name'], $cd) . "\n"; } return $track; } return false; } }