get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE source_key = %s AND conversion_type = %s', AIEO_DMM_Conversions_Registry::table(), (string) $source_key, 'goal' ), ARRAY_A ); if ( ! $row ) { return null; } return array( 'row' => $row, 'context' => class_exists( 'AIEO_Submission_Context' ) ? AIEO_Submission_Context::get( (string) $source_key ) : null, ); } public static function for_entry( $provider, $entry_id ) { global $wpdb; if ( ! class_exists( 'AIEO_DMM_Conversions_Registry' ) || (int) $entry_id <= 0 ) { return null; } $key = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT source_key FROM %i WHERE conversion_type = %s AND provider = %s AND entry_id = %d ORDER BY occurred_at DESC, id DESC LIMIT 1', AIEO_DMM_Conversions_Registry::table(), 'goal', (string) $provider, (int) $entry_id ) ); return '' !== $key ? self::lead( $key ) : null; } public static function url( $source_key ) { return add_query_arg( array( 'page' => class_exists( 'AIEO_DMM' ) ? AIEO_DMM::MENU_SLUG : 'ai-eshop-dmm', 'dmm_tab' => 'attribution', 'attr_lead' => rawurlencode( (string) $source_key ), ), admin_url( 'admin.php' ) ); } public static function keys_for( $provider, array $entry_ids ) { global $wpdb; $ids = array_values( array_unique( array_filter( array_map( 'intval', $entry_ids ) ) ) ); if ( ! $ids || ! class_exists( 'AIEO_DMM_Conversions_Registry' ) ) { return array(); } $in = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT entry_id, source_key FROM %i WHERE conversion_type = 'goal' AND provider = %s AND entry_id IN ({$in}) ORDER BY occurred_at ASC, id ASC", array_merge( array( AIEO_DMM_Conversions_Registry::table(), (string) $provider ), $ids ) ), ARRAY_A ); $out = array(); foreach ( $rows as $r ) { $out[ (int) $r['entry_id'] ] = (string) $r['source_key']; } return $out; } public static function link_for( $provider, $entry_id ) { global $wpdb; if ( ! class_exists( 'AIEO_DMM_Conversions_Registry' ) || (int) $entry_id <= 0 ) { return ''; } $key = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT source_key FROM %i WHERE conversion_type = %s AND provider = %s AND entry_id = %d ORDER BY occurred_at DESC, id DESC LIMIT 1', AIEO_DMM_Conversions_Registry::table(), 'goal', (string) $provider, (int) $entry_id ) ); if ( '' === $key ) { return ''; } return '' . esc_html__( 'Campaign and journey', 'ai-eshop-optimizer' ) . ''; } public static function surface_label( $surface, $provider = '' ) { $surface = (string) $surface; $fixed = array( 'contact_form' => __( 'Contact form', 'ai-eshop-optimizer' ), 'newsletter' => __( 'Newsletter sign-up', 'ai-eshop-optimizer' ), 'waitlist' => __( 'Back-in-stock waitlist', 'ai-eshop-optimizer' ), 'product_request' => __( 'Product request', 'ai-eshop-optimizer' ), ); if ( isset( $fixed[ $surface ] ) ) { return $fixed[ $surface ]; } if ( preg_match( '/^aieo_form_(\d+)$/', $surface, $m ) ) { return sprintf( __( 'Site form: %s', 'ai-eshop-optimizer' ), wp_strip_all_tags( get_the_title( (int) $m[1] ) ) ); } if ( preg_match( '/^event_(\d+)$/', $surface, $m ) ) { return sprintf( __( 'Event: %s', 'ai-eshop-optimizer' ), wp_strip_all_tags( get_the_title( (int) $m[1] ) ) ); } if ( preg_match( '/^cf7_(\d+)$/', $surface, $m ) ) { return sprintf( __( 'Contact Form 7: %s', 'ai-eshop-optimizer' ), wp_strip_all_tags( get_the_title( (int) $m[1] ) ) ); } if ( preg_match( '/^gravity_(\d+)$/', $surface, $m ) ) { $title = ''; if ( class_exists( 'GFAPI' ) ) { $form = GFAPI::get_form( (int) $m[1] ); $title = is_array( $form ) && isset( $form['title'] ) ? (string) $form['title'] : ''; } return sprintf( __( 'Gravity Forms: %s', 'ai-eshop-optimizer' ), '' !== $title ? $title : '#' . (int) $m[1] ); } if ( 'aieo_survey' === $provider && class_exists( 'AIEO_Survey_Blueprint' ) && 0 === strpos( $surface, 'aieo_' ) ) { $bp = AIEO_Survey_Blueprint::get( substr( $surface, 5 ) ); return sprintf( __( 'Survey: %s', 'ai-eshop-optimizer' ), $bp && ! empty( $bp['label'] ) ? (string) $bp['label'] : substr( $surface, 5 ) ); } $names = array( 'wpforms' => 'WPForms', 'fluent' => 'Fluent Forms', 'ninja' => 'Ninja Forms', 'elementor' => 'Elementor' ); foreach ( $names as $p => $label ) { if ( 0 === strpos( $surface, $p . '_' ) ) { return sprintf( __( '%1$s form %2$s', 'ai-eshop-optimizer' ), $label, substr( $surface, strlen( $p ) + 1 ) ); } } return '' !== $surface ? $surface : (string) $provider; } private static function local( $gmt ) { $ts = strtotime( (string) $gmt . ' UTC' ); return $ts ? wp_date( 'Y-m-d H:i', $ts ) : ''; } public static function render( array $lead ) { if ( empty( $lead['row'] ) || ! class_exists( 'AIEO_DMM_UI' ) || ! class_exists( '\Rooster\Admin\Ui' ) ) { return ''; } $row = $lead['row']; $ctx = isset( $lead['context'] ) && is_array( $lead['context'] ) ? $lead['context'] : null; $attr = $ctx && ! empty( $ctx['attribution'] ) ? $ctx['attribution'] : array( 'utm_source' => (string) $row['utm_source'], 'utm_medium' => (string) $row['utm_medium'], 'utm_campaign' => (string) $row['utm_campaign'], 'utm_content' => (string) $row['utm_content'], 'utm_term' => (string) $row['utm_term'], 'gclid' => (string) $row['gclid'], 'fbclid' => (string) $row['fbclid'], 'channel' => (string) $row['channel'], 'landing_page' => (string) $row['landing_path'], 'referrer' => (string) $row['referrer_host'], ); ob_start(); list( $open, $close ) = AIEO_DMM_UI::card( array( 'anchor' => 'lead-campaign', 'icon' => 'megaphone', 'title' => __( 'Campaign attribution', 'ai-eshop-optimizer' ), 'description' => __( 'Read when the form was sent and kept with the lead, so it holds even after the visitor\'s cookies are gone. The same values count this lead against its campaign in the report.', 'ai-eshop-optimizer' ), ) ); echo $open; $facts = array( array( 'label' => __( 'Form', 'ai-eshop-optimizer' ), 'value' => self::surface_label( $ctx ? (string) $ctx['surface'] : '', (string) $row['provider'] ) ), array( 'label' => __( 'Received', 'ai-eshop-optimizer' ), 'value' => self::local( (string) $row['occurred_at'] ) ), ); if ( $ctx && ! empty( $ctx['confirmed_at'] ) ) { $facts[] = array( 'label' => __( 'Confirmed', 'ai-eshop-optimizer' ), 'value' => self::local( (string) $ctx['confirmed_at'] ) ); } foreach ( array( 'utm_source' => __( 'Source', 'ai-eshop-optimizer' ), 'utm_medium' => _x( 'Medium', 'UTM parameter', 'ai-eshop-optimizer' ), 'utm_campaign' => __( 'Campaign', 'ai-eshop-optimizer' ), 'utm_content' => __( 'Content', 'ai-eshop-optimizer' ), 'utm_term' => __( 'Term', 'ai-eshop-optimizer' ), 'gclid' => __( 'Google click id', 'ai-eshop-optimizer' ), 'fbclid' => __( 'Meta click id', 'ai-eshop-optimizer' ), 'channel' => __( 'Channel', 'ai-eshop-optimizer' ), 'landing_page' => __( 'Landing page', 'ai-eshop-optimizer' ), 'referrer' => __( 'Referrer', 'ai-eshop-optimizer' ), ) as $k => $label ) { $facts[] = array( 'label' => $label, 'value' => isset( $attr[ $k ] ) ? trim( (string) $attr[ $k ] ) : '' ); } $facts[] = array( 'label' => __( 'Sent from', 'ai-eshop-optimizer' ), 'value' => $ctx ? (string) $ctx['page_url'] : '' ); echo \Rooster\Admin\Ui::facts( $facts ); echo $close; if ( ! class_exists( 'AIEO_Submission_Trail', false ) && defined( 'AIEO_DIR' ) && is_readable( AIEO_DIR . 'includes/class-aieo-submission-trail.php' ) ) { require_once AIEO_DIR . 'includes/class-aieo-submission-trail.php'; } $trail = ( $ctx && class_exists( 'AIEO_Submission_Trail' ) ) ? AIEO_Submission_Trail::for_submission( array( 'created' => (string) $row['occurred_at'], 'session_ids' => $ctx['session_ids'] ) ) : null; list( $open, $close ) = AIEO_DMM_UI::card( array( 'anchor' => 'lead-journey', 'icon' => 'eye', 'title' => __( 'What they looked at', 'ai-eshop-optimizer' ), 'description' => __( 'The pages this visitor read before sending the form and after it, from the session stream.', 'ai-eshop-optimizer' ), ) ); echo $open; if ( null === $trail ) { echo \Rooster\Admin\Ui::notice( esc_html__( 'This lead was recorded before journeys were kept with leads, or by an import: only its campaign is known.', 'ai-eshop-optimizer' ), 'info', array( 'inline' => true ) ); } else { $tz = wp_timezone_string(); $dash = ''; foreach ( array( 'before' => __( 'Before sending', 'ai-eshop-optimizer' ), 'after' => __( 'After sending', 'ai-eshop-optimizer' ), ) as $key => $heading ) { echo '
' . esc_html__( 'Nothing recorded.', 'ai-eshop-optimizer' ) . '
'; continue; } $rows = array(); foreach ( $trail[ $key ] as $p ) { $rows[] = array( 'page' => array( 'html' => '' . esc_html( $p['title'] ) . '' ), 'when' => self::local( (string) $p['at'] ), 'dwell' => (int) $p['dwell'] > 0 ? sprintf( _n( '%d second', '%d seconds', (int) $p['dwell'], 'ai-eshop-optimizer' ), (int) $p['dwell'] ) : array( 'html' => $dash ), '_s' => array( 'page' => (string) $p['title'], 'when' => (string) $p['at'], 'dwell' => (int) $p['dwell'] ), ); } echo AIEO_Table::local( 'aieo-lead-' . $key, array( array( 'key' => 'page', 'label' => __( 'Page', 'ai-eshop-optimizer' ) ), array( 'key' => 'when', 'label' => sprintf( __( 'When (%s)', 'ai-eshop-optimizer' ), $tz ), 'type' => 'date' ), array( 'key' => 'dwell', 'label' => __( 'Dwell', 'ai-eshop-optimizer' ), 'sort' => 'number' ), ), $rows, array( 'sortable' => true, 'prefix' => 'aieo-lead-' . $key . '_', 'sizes' => false ) ); } $f = $trail['source_flags']; if ( ! empty( $f['sessions'] ) ) { echo \Rooster\Admin\Ui::notice( '' . esc_html__( 'Where the journey came from:', 'ai-eshop-optimizer' ) . ' ' . esc_html__( 'the session stream', 'ai-eshop-optimizer' ), 'mono', array( 'inline' => true ) ); } elseif ( ! empty( $f['tracking_on'] ) ) { $quiet = ! empty( $f['consent_gated'] ) ? __( 'Session tracking is on, but no activity was recorded for this visitor around the time of the form. Tracking on this site waits for consent, so they may have declined it.', 'ai-eshop-optimizer' ) : __( 'Session tracking is on, but no activity was recorded for this visitor around the time of the form.', 'ai-eshop-optimizer' ); echo \Rooster\Admin\Ui::notice( esc_html( $quiet ), 'info', array( 'inline' => true ) ); } if ( empty( $f['tracking_on'] ) ) { $url = add_query_arg( array( 'page' => class_exists( 'AIEO_DMM' ) ? AIEO_DMM::MENU_SLUG : 'ai-eshop-dmm', 'dmm_tab' => 'attribution' ), admin_url( 'admin.php' ) ); echo \Rooster\Admin\Ui::notice( wp_kses_post( sprintf( __( 'Session tracking is OFF on this site, so this lead carries its campaign but no journey. %1$sTurn tracking on%2$s to see the pages read before and after a form.', 'ai-eshop-optimizer' ), '', '' ) ), 'warning', array( 'inline' => true ) ); } } echo $close; return (string) ob_get_clean(); } } }