trackingId = Settings::get( 'remarketing_id' );
$this->sendTo = Settings::get( 'remarketing_label' );
if ( $this->is_activated() ) {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueueScript' ] );
add_action( 'wp_head', [ $this, 'loadBaseScript' ] );
}
// Ajax adds to cart
add_action( 'wp_ajax_add_to_cart_facebook_pixel', [ $this, 'ajax_add_to_cart_data' ] );
add_action( 'wp_ajax_nopriv_add_to_cart_facebook_pixel', [ $this, 'ajax_add_to_cart_data' ] );
}
/**
* Is Facebook Pixel Tracking Enabled.
*
* @return bool
*/
public function is_activated() {
return ! empty( $this->trackingId ) && ( 'enable' === Settings::get( 'disable_remarketing' ) );
}
/**
* Enqueue Ajax Add to Cart Event Code.
*
* @return void
*/
public function enqueueScript() {
wp_enqueue_script( 'woo-feed-google-remarketing,', WOO_FEED_PLUGIN_URL . 'admin/js/woo-feed-google-remarketing.min.js', [
'jquery',
'wp-util'
], '1.0.0', true );
}
/**
* Load Base Script.
*
* @return void
*/
public function loadBaseScript() {
// @ToDo Language Code check. Currently passing all to `en_US`
?>
sendTo;
$data['aw_remarketing_only'] = true;
$value = 0;
foreach ( $ids as $id ) {
$product = wc_get_product( $id );
if ( ! is_object( $product ) ) {
continue;
}
$data['items'][]['id'] = $product->get_id();
$value += (int) $product->get_price();
}
$data['value'] = $value;
return $data;
}
return false;
}
/**
* Load ViewContent Event Script.
*
* @return void
*/
public function ViewContent() {
if ( is_product() ) {
global $post;
$_product = wc_get_product( $post->ID );
$id = $_product->get_ID();
$ids = [ $id ];
if ( "variable" === $_product->get_type() ) {
$ids = $_product->get_children();
}
$data = $this->get_content_info( $ids );
if ( $data ) {
?>
gtag( 'event', 'view_item', );
get_content_info( [ $product_id ] );
if ( $data ) {
?>
gtag( 'event', 'add_to_cart', );
cart->is_empty() ) {
$ids = [];
foreach ( WC()->cart->get_cart() as $cart_item ) {
$ids[] = $cart_item['product_id'];
}
$data = $this->get_content_info( $ids );
if ( $data ) {
?>
gtag( 'event', 'add_to_cart', );
query_vars['order-received'] ) ) {
$order = wc_get_order( $wp_query->query_vars['order-received'] );
$ids = [];
foreach ( $order->get_items() as $item ) {
$ids[] = $item->get_product_id();
}
$data = $this->get_content_info( $ids );
if ( $data ) {
?>
gtag( 'event', 'purchase', );
get_content_info( [ $product_id ] );
}
wp_send_json_success( json_encode( $data ) );
}
}