get_tracking_id() ) { if($this->disable_tracking()){ return; } $this->get_event_hooks(); foreach($this->event_hooks as $key=>$value){ if (is_array($value)){ foreach($value as $single_hook){ if (array_key_exists($key, $this->event_settings)){ 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 ); } } /* getting tracking id */ public function get_tracking_id() { if(get_option('awca_auth_settings')){ $auth_settings = get_option('awca_auth_settings'); if(isset($auth_settings['property_id'])){ $property = $auth_settings['property_id'] ; $pieces = explode( '|', $property ); return $pieces[1]; }else{ if( isset($auth_settings['tracking_id']) ){ 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()); ?> 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; } } /* 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; } } } if ( is_ajax() && is_admin() ) { $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; } } } /* intiating default params(check for more details) */ private function init_default_params($track_user = true){ $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; } /* 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'] ); } $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; } /* 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 ) { $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(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); } } /* 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 ) { $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(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); } } /* recording event for viewing shop page */ public function viewed_shop() { if ( is_shop()) { if ( !$this->avoid_multi_trigger()) { return; } $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(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); } } /* recording event for viewing cart page */ public function viewed_cart() { if ( is_cart()) { if (!$this->avoid_multi_trigger()) { return; } $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'; $this->params['z'] = rand(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); } } /* recording event for viewing product */ public function viewed_product() { if (!$this->avoid_multi_trigger()) { return; } $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'] = 'viewed_product'; $this->params['z'] = rand(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); } /* recording event for product added to cart */ public function added_product ( $cart_item_key ) { $item = WC()->cart->cart_contents[ $cart_item_key ]; $product_id = ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id']; if ( ! $product_id ) { return; } $product = wc_get_product( $product_id ); $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'] = htmlentities( $product->get_title(), ENT_QUOTES, 'UTF-8' ); $this->params['ea'] = 'added_product'; $this->params['z'] = rand(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); } /* recording event for product removed from cart */ public function removed_product( $cart_item_key ) { if ( isset( WC()->cart->cart_contents[ $cart_item_key ] ) ) { $item = WC()->cart->cart_contents[ $cart_item_key ]; $product_id = ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id']; if ( ! $product_id ) { return; } $product = wc_get_product( $product_id ); $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'] = htmlentities( $product->get_title(), ENT_QUOTES, 'UTF-8' ); $this->params['ea'] = 'removed_product'; $this->params['z'] = rand(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); } } /* recording event for initiating checkout */ public function started_checkout() { if ( !$this->avoid_multi_trigger() ) { return; } $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['z'] = rand(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); } /* 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; } /* 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['z'] = rand(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); $this->params = null; /* trigerring transaction */ $this->params = $this->init_default_params(); $this->params['t'] = 'transaction'; $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $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(); $this->params['cu'] = $order->get_currency(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); $this->params = null; /* add the purchased products */ foreach ( $order->get_items() as $item ) { $product = wc_get_product( ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'] ); $product_id = ($parent_id = $product->get_parent_id()) ? $parent_id : $product->get_id(); $product_identifier = ($sku = $product->get_sku()) ? $sku : $product_id; $this->params = $this->init_default_params(); $this->params['t'] = 'item'; $this->params['ti'] = $order->get_order_number(); $this->params['ua'] =htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); $this->params['ic'] = '#'.$product_identifier; // product ID $this->params['in'] = $item['name']; // product name $categories = wc_get_product_terms( $product_id, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) ); if ( is_array( $categories ) || !empty( $categories ) ) { $this->params['iv'] = ''; foreach ($categories as $category){ if(isset($category->name) && is_object($category)){ $this->params['iv'] .= $category->name .'/'; } } $this->params['iv'] = trim($this->params['iv'],'/'); // product category } $this->params['ip'] = $order->get_item_total( $item ); // product price $this->params['iq'] = $item['qty']; // product quantity $this->params['cu'] = $order->get_currency(); // product variant $this->params['z'] = rand(); $args = $this->get_request_args(); $response = null; $response = wp_safe_remote_request(untrailingslashit('https://ssl.google-analytics.com/collect'),$args); $this->params = 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 { ?>