true, 'strategy' => 'defer', ) ); wp_enqueue_script( $track_handle ); $track_data = array( 'trackurl' => TrackingLocal::instance()->local_tracking_url(), 'actions' => $actions, 'methods' => $tracking_methods, 'user_events' => $this->events_to_track(), 'debugTracking' => defined( 'ADCMDR_DEBUG_TRACKING' ) && ADCMDR_DEBUG_TRACKING === true, ); if ( $ga_config ) { $track_data['ga'] = $ga_config; } Util::enqueue_script_data( $track_handle, $track_data ); return $track_handle; } /** * Get the enabled tracking methods. * * @return array */ public function get_tracking_methods() { $methods = array(); if ( $this->is_local_tracking_enabled() ) { $methods[] = 'local'; } if ( ProBridge::instance()->is_pro_loaded() && Options::instance()->get( 'enable_ga_tracking', 'tracking', true ) ) { $methods[] = 'ga'; } if ( ProBridge::instance()->is_pro_loaded() && Options::instance()->get( 'enable_3p_tracking', 'tracking', true ) ) { $methods[] = '3p'; } return $methods; } /** * Determine if we have any tracking methods. * * @return bool */ public function has_tracking_methods() { return ( ! empty( $this->get_tracking_methods() ) ); } /** * Determine if we have impression or click tracking disabled. * * @param string $type String for impressions or clicks. * * @return bool */ public function is_tracking_disabled_for( $type ) { /** * This does not check settings of individual ads for performance reasons. * We're relying on javascript not binding click and impressions to ads that are individually disabled. */ $type = trim( strtolower( $type ) ); if ( $type !== 'impressions' && $type !== 'clicks' ) { return; } return Options::instance()->get( 'disable_track_' . $type, 'tracking', true ); } /** * Determine which events we should track, if any. * * @return array */ public function events_to_track() { $actions = array(); if ( ! $this->is_tracking_disabled_for( 'impressions' ) ) { $actions[] = 'impressions'; } if ( ! $this->is_tracking_disabled_for( 'clicks' ) ) { $actions[] = 'clicks'; } return $actions; } /** * Determine if local tracking is possible. * * @return bool */ public static function can_track_local() { return class_exists( 'DateTime' ); } /** * Determine if we should track local for a specific type of tracking. * Also checks for bots. * * @param string $type String for impressions or clicks tracking. * @param bool $bot_check Whether to check for bots or not. * * @return [type] */ public function should_track_local( $type, $bot_check = true ) { if ( $this->is_local_tracking_enabled() && ! $this->is_tracking_disabled_for( $type ) ) { if ( $bot_check ) { return ! $this->should_block_bot(); } return true; } return false; } /** * Checks for local tracking. * Takes into account both the option, and if we CAN track local. * * @return bool */ public function is_local_tracking_enabled() { return self::can_track_local() && Options::instance()->get( 'enable_local_tracking', 'tracking', true ); } /** * Checks for GA tracking. * * @return bool */ public function is_ga_tracking_enabled() { return ProBridge::instance()->is_pro_loaded() === true && Options::instance()->get( 'enable_ga_tracking', 'tracking', true ) === true; } /** * Determines if we should block a bot or not. * * @return bool */ private function should_block_bot() { if ( ! Options::instance()->get( 'bots_disable_tracking', 'tracking', true ) ) { return false; } return Bots::is_bot(); } /** * Check for compatible database for local tracking and return an error message if not compatible. * * @param bool $return_message Whether to return an error message or just a boolean for compatibility. Default true. * @return bool|string True if compatible, false if not compatible and $return_message is */ public static function check_database_compatibility( $return_message = true ) { global $wpdb; $is_sqlite = ( defined( 'DB_ENGINE' ) && DB_ENGINE === 'sqlite' ) || ( isset( $wpdb->dbh ) && is_object( $wpdb->dbh ) && stripos( get_class( $wpdb->dbh ), 'sqlite' ) !== false ); if ( $is_sqlite ) { if ( ! $return_message ) { return false; } return esc_html__( 'Your site appears to be using a SQLite database. Local tracking may not work as expected with SQLite.', 'ad-commander' ); } $db_version = $wpdb->db_version(); $is_mariadb = false; if ( ! empty( $wpdb->db_server_info ) ) { $is_mariadb = stripos( $wpdb->db_server_info, 'mariadb' ) !== false; } elseif ( method_exists( $wpdb, 'db_server_info' ) ) { $is_mariadb = stripos( $wpdb->db_server_info(), 'mariadb' ) !== false; } if ( $is_mariadb ) { if ( version_compare( $db_version, '10.3', '<' ) ) { if ( ! $return_message ) { return false; } return sprintf( /* translators: %s: the current MariaDB version */ esc_html__( 'Your site is running MariaDB %s. Local tracking requires MariaDB 10.3 or later.', 'ad-commander' ), esc_html( $db_version ) ); } } elseif ( version_compare( $db_version, '5.7', '<' ) ) { if ( ! $return_message ) { return false; } return sprintf( /* translators: %s: the current MySQL version */ esc_html__( 'Your site is running MySQL %s. Local tracking requires MySQL 5.7 or later.', 'ad-commander' ), esc_html( $db_version ) ); } return true; } }