get_tracking_id() ) { if($this->disable_tracking()){ return; } $this->get_event_hooks(); foreach($this->event_hooks as $key=>$value){ if (is_array($value)){ $number_args = null; if(is_int($value[0])){ $number_args = $value[0]; unset($value[0]); $value = array_values($value); } if($value[0]=='filter'){ unset($value[0]); $value = array_values($value); foreach($value as $single_hook){ if (array_key_exists($key, $this->event_settings)){ if(isset($number_args)){ add_filter($single_hook,array($this,$key),10,$number_args); }else{ add_filter($single_hook,array($this,$key)); } } } }else{ foreach($value as $single_hook){ if (array_key_exists($key, $this->event_settings)){ if(isset($number_args)){ add_action($single_hook,array($this,$key),10,$number_args); }else{ add_action($single_hook,array($this,$key)); } } } } }else{ if (array_key_exists($key, $this->event_settings)){ add_action($value,array($this,$key)); } } } add_action( 'wp_head',array( $this,'integration_notification'),5); add_action( 'wp_head',array( $this, 'get_tracking_code' ), 9 ); add_action( 'login_head',array( $this, 'get_tracking_code' ), 9 ); add_action( 'template_redirect',array( $this, 'capture_js' ), 9 ); add_filter( 'woocommerce_queued_js',array( $this, 'print_js' ), 11 ); } } /* getting tracking id */ public function get_tracking_id() { if(get_option('awca_auth_settings')){ $auth_settings = get_option('awca_auth_settings'); if(!empty($auth_settings['api_secret'])){ $this->api_secret = $auth_settings['api_secret']; } if(isset($auth_settings['property_id'])){ $property = $auth_settings['property_id'] ; $pieces = explode( '|', $property ); $this->tracking_id = $pieces[1]; $this->cid = $this->get_cid(true); return $pieces[1]; }else{ if( isset($auth_settings['tracking_id']) ){ $this->tracking_id = $auth_settings['tracking_id']; $this->cid = $this->get_cid(true); return $auth_settings['tracking_id']; }else{ return false; } } }else{ return false; } } /* adding tracking code to website */ public function get_tracking_code() { if ( $this->disable_tracking() ) { return; } $tracking_id = esc_js($this->get_tracking_id()); ?> disable_tracking() ) { return; } if(get_option('print_js')){ delete_option('print_js'); } return $js; } /* getting events and their respective hooks*/ public function get_event_hooks(){ $settings = AWCA_Settings::get_instance(); $this->event_hooks = $settings->awca_event_hooks; $this->event_settings = $settings->awca_event_settings; if(get_option('awca_event_settings')){ $this->event_settings = get_option('awca_event_settings'); } } /* getting cid for event api calls */ private function get_cid( $generate_cid = false ) { $cid = ''; /* get client identity via GA cookie and only accepting value if it validated */ if ( isset( $_COOKIE['_ga'] ) ) { $ga_cookie_data = filter_var($_COOKIE['_ga'], FILTER_SANITIZE_STRING); $data = explode('.',$ga_cookie_data); if (is_array($data) && count($data) > 3){ if (strlen($data[2]) > 3 && strlen($data[3]) > 3){ $cid = $data[2].'.'.$data[3]; } } } /* generate custom cid if cookie is not set */ if ( empty( $cid ) ) { $custom_cid = $generate_cid || ( empty($cid) && is_user_logged_in()); if ( $custom_cid ) { $bytes = random_bytes( 16 ); $bytes[6] = chr( ord( $bytes[6] ) & 0x0f | 0x40 ); // set version to 0100 $bytes[8] = chr( ord( $bytes[8] ) & 0x3f | 0x80 ); // set bits 6-7 to 10 return vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split( bin2hex( $bytes ), 4 ) ); } }else { return $cid; } } /* adding some js to footer of website */ public function capture_js($javascript = ''){ if(!empty($javascript) && ($javascript !== '')){ wc_enqueue_js($javascript); //$this->javascript .= $javascript; }else{ if(get_option('print_js')){ $print_js = get_option('print_js'); if(!empty($print_js) && ($print_js !== '')) { wc_enqueue_js($print_js); } } } if(get_option('print_js')){ $print_js = get_option('print_js'); if(!empty($javascript) && ($javascript !== '')){ $javascript .= $print_js; update_option('print_js',$javascript); } }else{ if(!empty($javascript) && ($javascript !== '')){ add_option('print_js',$javascript); } } } /* check for tracking should function or not */ private function disable_tracking() { if($this->get_tracking_id()){ $disable_tracking = false; }else{ $disable_tracking = true; } $user_id = get_current_user_id(); if ( $user_id && user_can( $user_id, 'manage_woocommerce' ) ) { $tracking_options = get_option('awca_track_settings'); if(isset($tracking_options['track_admin']) && $tracking_options['track_admin'] && is_user_logged_in() && $this->get_tracking_id()){ $disable_tracking = false; }else{ $settings = AWCA_Settings::get_instance(); $track_default_settings = $settings->awca_tracking_settings; if(empty($tracking_options) && isset($track_default_settings['track_admin']) && $track_default_settings['track_admin'] && is_user_logged_in() && $this->get_tracking_id()){ $disable_tracking = false; }else{ $disable_tracking = true; } } } return $disable_tracking; } /* Avoid multi trigerring of same event */ private function avoid_multi_trigger() { if(!isset( $_SERVER['HTTP_REFERER'])){ if(isset( $_SERVER['REQUEST_URI'])) { update_option('awca_old_url',parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH )); } return true; } if(get_option('awca_old_url')){ $awca_old_url = get_option('awca_old_url'); if(( $awca_old_url !== parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ))){ update_option('awca_old_url',parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH )); return true; }else{ return false; } }else{ if(( parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_PATH ) !== parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ))){ update_option('awca_old_url',parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH )); return true; } } } /* get product variation attributes */ public function get_product_variation_attributes( $product) { if ( ! $product instanceof \WC_Product ) { $product = wc_get_product( $product ); } if ( ! $product ) { return ''; } $variant = ''; if ( 'variation' === $product->get_type() ) { $variant = implode( ',', array_values( $product->get_variation_attributes() ) ); } elseif( 'variable' === $product->get_type() ) { global $woocommerce; $attributes = $product->get_default_attributes(); $variant = implode( ', ', array_values( $attributes ) ); } return $variant; } /* intiating default params(check for more details) */ private function init_default_params($track_user = true){ if(strpos($this->tracking_id,'UA') !== false){ $this->params['v'] = '1'; $this->params['tid'] = $this->get_tracking_id(); $this->params['cid'] = $this->get_cid(true); if(method_exists('WC_Geolocation','get_ip_address')){ $this->params['uip'] = WC_Geolocation::get_ip_address(); } if($track_user && (is_user_logged_in())){ if(isset($_COOKIE['opt_in'])){ if($_COOKIE['opt_in'] == 'yes'){ $this->params['uid'] = esc_js(get_current_user_id()); } }else{ $this->params['uid'] = esc_js(get_current_user_id()); } } return $this->params; }else{ $this->data['client_id'] = $this->cid; if($track_user && (is_user_logged_in())){ if(isset($_COOKIE['opt_in'])){ if($_COOKIE['opt_in'] == 'yes'){ $this->data['user_id'] = esc_js(get_current_user_id()); } }else{ $this->data['user_id'] = esc_js(get_current_user_id()); } } return $this->data; } } /* setting up request arguments for api request */ protected function get_request_args() { if(function_exists(wc_get_user_agent())){ $user_agent = wc_get_user_agent(); }else{ $user_agent = sprintf( '%s/%s (WooCommerce/%s; WordPress/%s)', 'WoocommerceAnalytics', AWCA_VERSION, WC_VERSION, $GLOBALS['wp_version'] ); } if(strpos($this->tracking_id,'UA') !== false){ $args = array( 'method' => 'POST', 'timeout' => MINUTE_IN_SECONDS, 'redirection' => 0, 'sslverify' => true, 'user-agent' => $user_agent, 'body' => 'payload_data&' . http_build_query( $this->params, '', '&' ), ); return $args; }else{ $args = array( 'method' => 'POST', 'timeout' => MINUTE_IN_SECONDS, 'redirection' => 0, 'sslverify' => true, 'user-agent' => $user_agent, 'body' => json_encode($this->data), ); return $args; } } /* get product details */ private function get_product_details( $product_id, $quantity = 1 ,$i = 1) { global $woocommerce_loop; $product = wc_get_product( $product_id ); if($product instanceof \WC_Product){ $product_identifier = ($sku = $product->get_sku()) ? $sku : $product_id; $categories = wc_get_product_terms( $product_id, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) ); if ( is_array( $categories ) || !empty( $categories ) ) { $product_category = ''; foreach ($categories as $category){ if(isset($category->name) && is_object($category)){ $product_category .= $category->name .'/'; } } $product_category = trim($product_category,'/'); // product category } if(strpos($this->tracking_id,'UA') !== false){ $this->params["pr{$i}id"] = strval($product_identifier); $this->params["pr{$i}nm"] = $product->get_title(); $this->params["pr{$i}qt"] = $quantity; $this->params["pr{$i}ca"] = $product_category; $this->params["pr{$i}va"] = $this->get_product_variation_attributes( $product ); $this->params["pr{$i}pr"] = $product->get_price(); //$this->params["pr{$i}br"] = $product_details['brand']; //$this->params["pal"] = $this->get_list_type(); $this->params["pr{$i}ps"] = isset( $woocommerce_loop['loop'] ) ? $woocommerce_loop['loop'] : ''; foreach ( $this->params as $key => $value ) { if ( empty( $value ) ) { unset( $this->params[ $key ] ); } } return $this->params; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $item['item_name'] = $product->get_title(); $item['quantity'] = $quantity; $item['item_category'] = $product_category; $item['item_variant'] = $this->get_product_variation_attributes( $product ); $item['price'] = $product->get_price(); //$item['item_id'] = isset( $woocommerce_loop['loop'] ) ? $woocommerce_loop['loop'] : ''; foreach ( $item as $key => $value ) { if ( empty( $value ) ) { unset( $item[ $key ] ); } } return $item; } } return $this->params; } /* making remote request */ private function making_remote_request(){ $remote_url = null; if(strpos($this->tracking_id,'UA') !== false){ $remote_url = 'https://ssl.google-analytics.com/collect'; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $remote_url = 'https://www.google-analytics.com/mp/collect?measurement_id='.$this->tracking_id.'&api_secret='.$this->api_secret; } if(!empty($remote_url)){ $args = $this->get_request_args(); $response = null; $i = 0; while(1) { error_log(print_r($args,true),0); $response = wp_safe_remote_request(untrailingslashit($remote_url),$args); if (!empty($response) && is_array($response)){ if(isset($response['response']['code']) && ((int)$response['response']['code'] < 300)){ break; } } if($i > 1) { break; } $i++; } } } /* recording signed in event -completed*/ public function user_login( $user_login, $user ) { if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'User'; $this->params['el'] = $user->ID; $this->params['ea'] = 'user_login'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; /* adding enhanced ecommerce info */ if(is_checkout()){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Checkout'; $this->params['el'] = $user->ID; $this->params['ea'] = 'user_login'; $this->params['pa'] = 'checkout_option'; $this->params['cos'] = 1; $this->params['col'] = 'User Login'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; } }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); if(is_checkout()){ $this->data['events'][0]= array( 'name' => 'login', 'params'=>array( 'method' => 'checkout', ), ); }else{ $this->data['events'][0]= array( 'name' => 'login', 'params'=>array( 'method' => 'myaccount', ), ); } $this->making_remote_request(); $this->params = null; $this->data = null; } } /* recording signed out event -completed*/ public function user_logout() { if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'User'; $this->params['el'] = 'User Logout'; $this->params['ea'] = 'user_logout'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $this->data['events'][0]= array( 'name' => 'logout', ); $this->making_remote_request(); $this->params = null; $this->data = null; } } /* recording viewed signup form event -completed */ public function viewed_signup_form() { if ( !$this->avoid_multi_trigger() ) { return; } if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'User'; $this->params['el'] = 'Viewed Signup Form'; $this->params['ea'] = 'viewed_signup_form'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $this->data['events'][0]= array( 'name' => 'viewed_signup_form', ); $this->making_remote_request(); $this->params = null; $this->data = null; } } /* recording user signup form event -completed*/ public function user_signup() { if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'User'; $this->params['el'] = 'User Signup'; $this->params['ea'] = 'user_signup'; $this->params['z'] = rand(); $this->making_remote_request(); $this->data = null; $this->params = null; /* adding enhanced ecommerce info */ if(is_checkout()){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Checkout'; $this->params['el'] = 'Checkout User Signup'; $this->params['ea'] = 'user_signup'; $this->params['pa'] = 'checkout_option'; $this->params['cos'] = 3; $this->params['col'] = 'User Signup'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; } }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); if(is_checkout()){ $this->data['events'][0]= array( 'name' => 'sign_up', 'params'=>array( 'method' => 'checkout', ), ); }else{ $this->data['events'][0]= array( 'name' => 'sign_up', 'params'=>array( 'method' => 'myaccount', ), ); } $this->making_remote_request(); $this->params = null; $this->data = null; } } /* recording event for writing review for product */ public function wrote_review($comment_ID) { $comment = get_comment( $comment_ID ); $post_ID = $comment->comment_post_ID; $type = get_post_type($post_ID); if ( 'product' == $type ) { if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Products'; $this->params['el'] = get_the_title($post_ID); $this->params['ea'] = 'wrote_review'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $this->data['events'][0]= array( 'name' => 'wrote_review', ); $this->making_remote_request(); $this->params = null; $this->data = null; } } } /* recording event for writing comment for post */ public function commented($comment_ID){ $comment = get_comment( $comment_ID ); $post_ID = $comment->comment_post_ID; $type = get_post_type($post_ID); if ( 'post' == $type ) { if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Post'; $this->params['el'] = get_the_title($post_ID); $this->params['ea'] = 'commented'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $this->data['events'][0]= array( 'name' => 'commented', ); $this->making_remote_request(); $this->params = null; $this->data = null; } } } /* recording event for viewing shop page */ public function viewed_shop() { if ( is_shop()) { if ( !$this->avoid_multi_trigger()) { return; } if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Page View'; $this->params['el'] = 'Viewed Shop Page'; $this->params['ea'] = 'viewed_shop'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $this->data['events'][0]= array( 'name' => 'viewed_shop', ); $this->making_remote_request(); $this->params = null; $this->data = null; } } } /* recording event for viewing cart page -completed */ public function viewed_cart() { if ( is_cart()) { if (!$this->avoid_multi_trigger()) { return; } if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] = htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Cart'; $this->params['el'] = 'Viewed Cart Page'; $this->params['ea'] = 'viewed_cart'; $i = 0; foreach ( WC()->cart->get_cart() as $item ) { $i++; $product_id = ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id']; $this->params = $this->get_product_details($product_id , $item['quantity'], $i ); } $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $i = 0; $items_data = null; foreach ( WC()->cart->get_cart() as $item ) { $product_id = ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id']; $items_data[$i] = $this->get_product_details($product_id , $item['quantity'], $i ); $i++; } $cart_value = floatval( preg_replace( '#[^\d.,]#', '',WC()->cart->get_cart_total())); $this->data['events'][0]= array( 'name' => 'view_cart', 'params' => array( 'currency' => get_woocommerce_currency(), 'items'=> $items_data, 'value' => $cart_value, ), ); $this->making_remote_request(); $this->params = null; $this->data = null; } } } /* recording user viewed my account page */ public function viewed_account() { if ( !$this->avoid_multi_trigger() ) { return; } if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'User'; $this->params['el'] = 'Viewed Account';// get here current user id $this->params['ea'] = 'viewed_account'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $this->data['events'][0]= array( 'name' => 'viewed_account', ); $this->making_remote_request(); $this->params = null; $this->data = null; } } /* recording event for viewing product -completed*/ public function viewed_product() { if (!$this->avoid_multi_trigger()) { return; } $product_id = get_the_ID(); $product = wc_get_product( $product_id ); if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Product View'; $this->params['el'] = esc_js(get_the_title()); $this->params['ea'] = 'view_item'; $this->params['pa'] = 'detail'; $this->params = $this->get_product_details($product_id); $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $item_data[] = $this->get_product_details($product_id); $this->data['events'][0]= array( 'name' => 'view_item', 'params' => array( 'currency' => get_woocommerce_currency(), 'items'=> $item_data, 'value' => $product->get_price(), ), ); $this->making_remote_request(); $this->params = null; $this->data = null; } } /* recording event for initiating checkout -completed */ public function begin_checkout() { if ( !$this->avoid_multi_trigger() ) { return; } if(strpos($this->tracking_id,'UA') !== false){ $i = 0; $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Checkout'; $this->params['el'] = 'Initiated Checkout'; $this->params['ea'] = 'initiated_checkout'; $this->params['pa'] = 'checkout'; foreach ( WC()->cart->get_cart() as $item ) { $i++; $product_id = ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id']; $this->params = $this->get_product_details($product_id , $item['quantity'], $i ); } $this->params['cos'] = 1; $this->params['col'] = 'Begin Checkout'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ foreach ( WC()->cart->get_cart() as $item ) { $i = 0; $i++; $product_id = ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id']; $items_data[] = $this->get_product_details($product_id , $item['quantity'], $i ); } $this->data = $this->init_default_params(); $checkout_value = floatval( preg_replace( '#[^\d.,]#', '',WC()->cart->get_cart_total())); $applied_coupons = WC()->cart->get_applied_coupons(); $coupon_code = null; foreach( $applied_coupons as $coupon){ $coupon_code .= $coupon.'/'; } $coupon_code = trim($coupon_code,'/'); $this->data['events'][0]= array( 'name' => 'begin_checkout', 'params' => array( 'coupon' => $coupon_code, 'currency' => get_woocommerce_currency(), 'items'=> $items_data, 'value' => $checkout_value, ), ); $this->making_remote_request(); $this->params = null; $this->data = null; } } /* recording checkout page events starting with user provided require info */ public function filled_checkout_form() { $live_js = ''; $option_name = is_user_logged_in() ? 'Registered User' : 'Guest'; if(strpos($this->tracking_id,'UA') !== false){ $live_js = "gtag( 'event','checkout_progress',{'checkout_step':'2','checkout_option':'{$option_name}', 'event_category' :'Checkout','non_interaction':true,'event_label' :'Filled Checkout Form'});"; }else{ $live_js = "gtag( 'event','filled_checkout_form');"; } $added_js = " var user_info_fired = false; var all_filled = true; jQuery( 'form.checkout' ).on( 'change', 'input', function() { if(!user_info_fired){ jQuery('input[id|=\'billing\']').each(function(){ if (!all_filled){ return; } if (!jQuery(this).val()){ if(jQuery(this).attr('type')=='email'){ if ( !isEmail( this.value )){ all_filled = false; return; } } if(jQuery(this).attr('type')=='phone'){ if ( !isPhone( this.value )){ all_filled = false; return; } } if(!(jQuery(this).attr('id').includes('company') || jQuery(this).attr('id').includes('address_2'))){ all_filled = false; return; } } }); if(all_filled){ user_info_fired = true; {$live_js} } } }); function isEmail(email) { var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; return regex.test(email); } function isPhone(phone) { var regex = /[0-9\-\(\)\s]+/; return regex.test(phone); } jQuery( 'form.checkout' ).on( 'checkout_place_order', function() { if ( !user_info_fired ) {user_info_fired = true;{$live_js}}});"; if ( ! empty( $added_js ) ) { $this->capture_js( $added_js ); } } /* selected shipping method */ public function added_shipping_method() { if(strpos($this->tracking_id,'UA') !== false){ $live_js = "function get_shipping_event (shipping_method) { return gtag( 'event','checkout_progress',{'checkout_step':'3','checkout_option':shipping_method,'event_category' :'Checkout','non_interaction':true,'event_label' :shipping_method}); }"; }else{ $live_js = "function get_shipping_event (shipping_method) { return gtag( 'event','add_shipping_info',{'shipping_tier' :shipping_method}); }"; } $js = ''; $js = $live_js; $js .= "var selected_shipping_method = jQuery( 'input[name^=\'shipping_method\']:checked' ).val();"; $js .= "var shipping_method_tracked = false; var shipping_method = '';"; $js .= "jQuery( 'form.checkout' ).on( 'click', 'input[name^=\'shipping_method\']', function( e ) { if ( selected_shipping_method !== this.value ) { shipping_method = this.value; shipping_method_tracked = true; if(shipping_method){get_shipping_event(shipping_method);} selected_shipping_method = this.value; } });"; $js .= "jQuery( 'form.checkout' ).on( 'checkout_place_order', function() { if ( !shipping_method_tracked ) {shipping_method = selected_shipping_method ; shipping_method_tracked = true; if(shipping_method){get_shipping_event(shipping_method);} } });"; $this->capture_js( $js ); } /* selected payment method */ public function added_payment_method() { if(strpos($this->tracking_id,'UA') !== false){ $live_js = "function get_paymnet_event (payment_method) { return gtag( 'event','add_payment_info',{'checkout_step':'4','checkout_option':payment_method,'event_category' :'Checkout','non_interaction':true,'event_label' :payment_method}); }"; }else{ $live_js = "function get_paymnet_event (payment_method) { return gtag( 'event','add_payment_info',{'payment_type' :payment_method}); }"; } $js = ''; $js = $live_js; $js .= "var selected_payment_method = jQuery( 'input[name=\'payment_method\']:checked' ).val();"; $js .= "var payment_method_tracked = false; var payment_method = '';"; $js .= "jQuery( 'form.checkout' ).on( 'click', 'input[name=\'payment_method\']', function( e ) { if ( selected_payment_method !== this.value ) { payment_method = this.value; payment_method_tracked = true; if(payment_method){get_paymnet_event(payment_method);} selected_payment_method = this.value; } });"; $js .= "jQuery( 'form.checkout' ).on( 'checkout_place_order', function() { if ( !payment_method_tracked ) {payment_method = selected_payment_method ; payment_method_tracked = true; if(payment_method){get_paymnet_event(payment_method);} } });"; $this->capture_js( $js ); } /* started payment */ public function processing_payment($order_id) { $order = wc_get_order( $order_id ); if($order instanceof WC_Order){ if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Checkout'; $this->params['el'] = 'Payment Processing'; $this->params['ea'] = 'started_payment'; $this->params['pa'] = 'checkout'; $this->params['ni'] = '1'; $this->params['cos'] = '5'; $this->params['col'] = $order->get_payment_method(); $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $this->data['events'][0]= array( 'name' => 'processing_payment', ); $this->making_remote_request(); $this->params = null; $this->data = null; } } } /* order failed */ public function order_failed( $order_id, $order ){ if($order instanceof WC_Order){ if(strpos($this->tracking_id,'UA') !== false){ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ec'] = 'Order'; $this->params['el'] = $order_id.'/'.$order->get_payment_method_title(); $this->params['ea'] = 'order_failed'; $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $this->data = $this->init_default_params(); $this->data['events'][0]= array( 'name' => 'order_failed', ); $this->making_remote_request(); $this->params = null; $this->data = null; } } } /* recording event for completed purchase */ public function completed_purchase( $order_id ) { $order = wc_get_order( $order_id ); if ( ! $order || ( 'yes' === get_post_meta( $order_id, 'awca_already_tracked', true ) ) ) { return; } $coupons_list = ''; if( $order->get_coupon_codes() ) { $i = 1; foreach( $order->get_coupon_codes() as $coupon) { if( $i > 1 ){ $coupons_list .= ','; } $coupons_list .= $coupon; $i++; } $this->params['tcc'] = $coupons_list; } if(strpos($this->tracking_id,'UA') !== false){ /* trigerring event */ $this->params = $this->init_default_params(); $this->params['t'] = 'event'; $this->params['ec'] = 'Checkout'; $this->params['el'] = $order->get_order_number(); $this->params['ea'] = 'completed_purchase'; $this->params['ev'] = floor($order->get_total()); $this->params['pa'] = 'purchase'; $this->params['tr'] = $order->get_total(); $this->params['ti'] = $order->get_order_number(); $this->params['tt'] = $order->get_total_tax(); $this->params['ts'] = $order->get_total_shipping(); $i = 0; $contents = array(); foreach ( $order->get_items() as $item ) { $i++; $product_id = ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id']; $this->params = $this->get_product_details($product_id , $item['qty'], $i ); $contents[] = array( 'id'=>$product_id, 'quantity'=>$item['qty'], ); } $this->params['z'] = rand(); $this->making_remote_request(); $this->params = null; $this->data = null; }elseif((strpos((string)$this->tracking_id,'G') !== false) && !empty($this->api_secret)){ $i = 0; $contents = array(); foreach ( $order->get_items() as $item ) { $i++; $product_id = ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id']; $items_data[] = $this->get_product_details($product_id , $item['qty'], $i ); $contents[] = array( 'id'=>$product_id, 'quantity'=>$item['qty'], ); } $this->data = $this->init_default_params(); $this->data['events'][0]= array( 'name' => 'purchase', 'params' => array( 'coupon' => $coupons_list, 'currency' => get_woocommerce_currency(), 'items'=> $items_data, 'transaction_id'=> $order->get_order_number(), 'value' => $order->get_total(), 'shipping' => $order->get_total_shipping(), 'tax' => $order->get_total_tax(), ), ); $this->making_remote_request(); $this->params = null; $this->data = null; } update_post_meta( $order->get_id(), 'awca_already_tracked', 'yes' ); } /* adding function for GDPR compalience */ public function integration_notification() { if(get_option('awca_track_settings')){ $track_settings = get_option('awca_track_settings'); if($track_settings['gdpr_enable']){ if(isset($_POST['awca_opt_submission']) && (!empty($_POST['awca_opt_submission']))){ $cookie_data = filter_var($_POST['awca_opt_submission'], FILTER_SANITIZE_STRING); if(!empty($cookie_data)){ setcookie('opt_in', $cookie_data, 0,'/'); } unset( $_POST['awca_opt_submission']); }else{ if(isset($_COOKIE['opt_in'])){ if(filter_var($_COOKIE['opt_in'],FILTER_SANITIZE_STRING) == 'no'){ $tracking_id = $this->get_tracking_id(); if(!empty($tracking_id)){ $output = ""; echo $output; } } }else { ?>