array( 'aieo_product_request_created', 'on_product_request', 2 ), 'product_request_item' => array( 'aieo_product_request_item_added', 'on_product_request_item', 2 ), 'waitlist_signup' => array( 'aieo_waitlist_subscribed', 'on_waitlist_subscribed', 4 ), 'contact_message' => array( 'aieo_contact_message_created', 'on_contact_message', 2 ), 'form_submit_aieo' => array( 'aieo_form_entry_recorded', 'on_form_entry', 3 ), ); private static $newsletter_confirmed = false; const DEDUP_OPT = 'aieo_goal_ledger_dedup_v1'; const ORPHANS_OPT = 'aieo_goal_ledger_orphans_v1'; public static function register() { static $done = false; if ( $done ) { return; } $done = true; add_action( 'gform_after_submission', array( __CLASS__, 'on_gravity' ), 11, 2 ); add_action( 'aieo_goal_recorded', array( __CLASS__, 'on_goal' ), 10, 3 ); foreach ( self::ENTRY_WRITTEN_GOALS as $hook ) { add_action( $hook[0], array( __CLASS__, $hook[1] ), 11, $hook[2] ); } add_action( 'aieo_submission', array( __CLASS__, 'on_submission' ), 10, 1 ); if ( ! class_exists( 'AIEO_Form_Provider_Observer', false ) && is_readable( __DIR__ . '/class-aieo-form-provider-observer.php' ) ) { require_once __DIR__ . '/class-aieo-form-provider-observer.php'; } if ( class_exists( 'AIEO_Form_Provider_Observer', false ) ) { AIEO_Form_Provider_Observer::register(); } add_action( 'aieo_newsletter_subscribe', array( __CLASS__, 'on_newsletter_block' ), 11, 4 ); if ( class_exists( 'AIEO_Submission_Context' ) ) { add_filter( 'wp_privacy_personal_data_exporters', array( 'AIEO_Submission_Context', 'register_exporter' ) ); add_filter( 'wp_privacy_personal_data_erasers', array( 'AIEO_Submission_Context', 'register_eraser' ) ); add_action( 'aieo_cleanup_old_sessions', array( 'AIEO_Submission_Context', 'expire_session_ids' ), 20 ); } if ( is_admin() ) { add_action( 'gform_entry_detail_sidebar_middle', array( __CLASS__, 'gravity_entry_box' ), 10, 2 ); } add_action( 'newsletter_user_confirmed', array( __CLASS__, 'on_newsletter_confirmed' ), 5, 1 ); self::maybe_dedup_entry_goals(); self::maybe_forget_deleted_leads(); } public static function maybe_forget_deleted_leads() { if ( ! is_admin() || wp_doing_ajax() || wp_doing_cron() ) { return null; } if ( get_option( self::ORPHANS_OPT ) ) { return null; } return self::forget_deleted_leads(); } public static function forget_deleted_leads() { if ( ! class_exists( 'AIEO_Submission_Context' ) || ! method_exists( 'AIEO_Submission_Context', 'forget_leads' ) ) { return null; } if ( ! self::boot() ) { return null; } global $wpdb; $exists = static function ( $t ) use ( $wpdb ) { return (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $t ) ) ) === $t; }; $tables = array_values( array_filter( array( AIEO_DMM_Conversions_Registry::table(), AIEO_Submission_Context::table() ), $exists ) ); $sources = array( array( $wpdb->prefix . 'aieo_product_requests', array( 'pr:', 'pri:' ) ), array( $wpdb->prefix . 'aieo_waitlist', array( 'wl:' ) ), ); $keys = array(); foreach ( $sources as $src ) { if ( ! $exists( $src[0] ) ) { continue; } foreach ( $tables as $t ) { $like = array(); $args = array( $t, $src[0] ); foreach ( $src[1] as $p ) { $like[] = 'g.source_key LIKE %s'; $args[] = $wpdb->esc_like( $p ) . '%'; } $found = $wpdb->get_col( $wpdb->prepare( "SELECT g.source_key FROM %i AS g LEFT JOIN %i AS r ON r.id = CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(g.source_key, ':', 2), ':', -1) AS UNSIGNED) WHERE (" . implode( ' OR ', $like ) . ') AND r.id IS NULL', $args ) ); if ( '' !== (string) $wpdb->last_error ) { return null; } $keys = array_merge( $keys, (array) $found ); } } $keys = array_values( array_unique( array_map( 'strval', $keys ) ) ); $removed = 0; foreach ( array_chunk( $keys, 200 ) as $chunk ) { $removed += AIEO_Submission_Context::forget_leads( $chunk ); } $done = array( 'ledger_keys' => count( $keys ), 'removed' => $removed ); update_option( self::ORPHANS_OPT, array( 'at' => gmdate( 'Y-m-d H:i:s' ) ) + $done, false ); return $done; } public static function maybe_dedup_entry_goals() { if ( ! is_admin() || wp_doing_ajax() || wp_doing_cron() ) { return; } if ( get_option( self::DEDUP_OPT ) ) { return; } if ( ! self::boot() ) { return; } global $wpdb; $t = AIEO_DMM_Conversions_Registry::table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { update_option( self::DEDUP_OPT, array( 'at' => gmdate( 'Y-m-d H:i:s' ), 'deleted' => 0, 'skipped' => 'no_table' ), true ); return; } $keys = array_keys( self::ENTRY_WRITTEN_GOALS ); $in = implode( ', ', array_fill( 0, count( $keys ), '%s' ) ); $args = $keys; $args[] = $wpdb->esc_like( 'goal:' ) . '%'; $affected = $wpdb->query( $wpdb->prepare( "DELETE g FROM `{$t}` AS g INNER JOIN `{$t}` AS e ON e.conversion_type = g.conversion_type AND e.goal_key = g.goal_key AND e.provider <> '' AND g.occurred_at BETWEEN e.occurred_at - INTERVAL 5 MINUTE AND e.occurred_at + INTERVAL 5 MINUTE AND ( g.session_id = e.session_id OR e.session_id = '' OR g.session_id = '' ) WHERE g.conversion_type = 'goal' AND g.provider = '' AND g.goal_key IN ( {$in} ) AND g.source_key LIKE %s", $args ) ); if ( false === $affected ) { return; } update_option( self::DEDUP_OPT, array( 'at' => gmdate( 'Y-m-d H:i:s' ), 'deleted' => (int) $affected, ), true ); } public static function on_product_request( $request_id, $context = array() ) { if ( ! self::boot() ) { return; } $context = is_array( $context ) ? $context : array(); $a = isset( $context['attribution'] ) && is_array( $context['attribution'] ) ? $context['attribution'] : array(); $when = isset( $context['created_at'] ) && $context['created_at'] ? (string) $context['created_at'] : gmdate( 'Y-m-d H:i:s' ); self::write_lead( array( 'goal_key' => 'product_request', 'provider' => 'product_request', 'entry_id' => (int) $request_id, 'source_key' => 'pr:' . (int) $request_id, 'occurred_at' => $when, 'session_id' => isset( $context['session_id'] ) ? (string) $context['session_id'] : '', 'attribution' => $a, ) ); if ( class_exists( 'AIEO_Submission_Context' ) ) { AIEO_Submission_Context::store( 'pr:' . (int) $request_id, array( 'provider' => 'product_request', 'entry_id' => (int) $request_id, 'goal_key' => 'product_request', 'email' => isset( $context['email'] ) ? (string) $context['email'] : '', 'occurred_at' => $when, ), AIEO_Submission_Context::capture( array( 'surface' => 'product_request', 'page_url' => isset( $context['page_url'] ) ? (string) $context['page_url'] : '', 'attribution' => $a, ) ) ); } } public static function on_product_request_item( $request_id, $product_id = 0 ) { if ( ! self::goal_on( 'product_request_item' ) ) { return; } if ( ! self::boot() ) { return; } self::lead( array( 'goal_key' => 'product_request_item', 'provider' => 'product_request', 'entry_id' => (int) $request_id, 'source_key' => 'pri:' . (int) $request_id . ':' . (int) $product_id, 'occurred_at' => gmdate( 'Y-m-d H:i:s' ), 'surface' => 'product_request', ) ); } public static function on_waitlist_subscribed( $row_id, $email = '', $product_id = 0, $variation_id = 0 ) { if ( ! self::boot() ) { return; } global $wpdb; $created = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT created_at FROM `' . $wpdb->prefix . 'aieo_waitlist` WHERE id = %d', (int) $row_id ) ); if ( '' === $created ) { $created = gmdate( 'Y-m-d H:i:s' ); } self::lead( array( 'goal_key' => 'waitlist_signup', 'provider' => 'waitlist', 'entry_id' => (int) $row_id, 'source_key' => 'wl:' . (int) $row_id . ':' . strtotime( $created . ' UTC' ), 'occurred_at' => $created, 'surface' => 'waitlist', 'email' => (string) $email, ) ); } public static function on_contact_message( $id, $row = array() ) { if ( ! self::live_submit( array( 'aieo_contact_form_submit' ) ) || ! self::boot() ) { return; } $row = is_array( $row ) ? $row : array(); self::lead( array( 'goal_key' => 'contact_message', 'provider' => 'contact_form', 'entry_id' => (int) $id, 'source_key' => 'cf:' . (int) $id, 'occurred_at' => isset( $row['created_at'] ) && '' !== (string) $row['created_at'] ? (string) $row['created_at'] : gmdate( 'Y-m-d H:i:s' ), 'surface' => 'contact_form', 'email' => isset( $row['email'] ) ? (string) $row['email'] : '', 'page_url' => isset( $row['page_url'] ) ? (string) $row['page_url'] : '', ) ); } public static function on_form_entry( $entry_id, $form_id = 0, $values = array() ) { if ( ! self::live_submit( array( 'aieo_form_entry' ) ) || ! self::boot() ) { return; } $email = ''; if ( class_exists( 'AIEO_Form_Submit' ) && class_exists( 'AIEO_Form_CPT' ) && is_array( $values ) ) { $email = (string) AIEO_Form_Submit::first_email( (array) AIEO_Form_CPT::fields( (int) $form_id ), $values ); } self::lead( array( 'goal_key' => 'form_submit_aieo', 'provider' => 'aieo_form', 'form_id' => (int) $form_id, 'entry_id' => (int) $entry_id, 'source_key' => 'af:' . (int) $entry_id, 'occurred_at' => gmdate( 'Y-m-d H:i:s' ), 'surface' => 'aieo_form_' . (int) $form_id, 'email' => $email, ) ); } public static function on_submission( $args ) { if ( ! is_array( $args ) || ! self::boot() ) { return; } $provider = substr( (string) preg_replace( '/[^a-z0-9_]/', '', strtolower( (string) ( $args['provider'] ?? '' ) ) ), 0, 20 ); $goal = substr( (string) preg_replace( '/[^a-z0-9_]/', '', strtolower( (string) ( $args['goal_key'] ?? '' ) ) ), 0, 64 ); if ( '' === $provider || '' === $goal ) { return; } $entry = max( 0, (int) ( $args['entry_id'] ?? 0 ) ); $email = isset( $args['email'] ) ? (string) $args['email'] : ''; $who = '' !== $email ? strtolower( $email ) : ( class_exists( 'AIEO_Submission_Context' ) ? AIEO_Submission_Context::current_session_id() : '' ); if ( '' === $who ) { $who = wp_generate_uuid4(); } if ( isset( $args['source_key'] ) && '' !== (string) $args['source_key'] ) { $key = substr( (string) $args['source_key'], 0, 64 ); } elseif ( $entry > 0 ) { $key = $provider . ':' . $goal . ':' . $entry; if ( strlen( $key ) > 64 ) { $key = $provider . ':' . substr( sha1( $goal . ':' . $entry ), 0, 32 ); } } else { $key = $provider . ':' . substr( sha1( $goal . '|' . $who . '|' . (string) ( $args['surface'] ?? '' ) ), 0, 16 ) . ':' . gmdate( 'YmdHi' ); } self::lead( array( 'goal_key' => $goal, 'provider' => $provider, 'entry_id' => $entry, 'source_key' => $key, 'occurred_at' => isset( $args['occurred_at'] ) && '' !== (string) $args['occurred_at'] ? (string) $args['occurred_at'] : gmdate( 'Y-m-d H:i:s' ), 'surface' => isset( $args['surface'] ) ? (string) $args['surface'] : $provider, 'email' => $email, 'page_url' => isset( $args['page_url'] ) ? (string) $args['page_url'] : '', ) ); } public static function on_newsletter_block( $email, $name = '', $synced = false, $last = '' ) { $email = strtolower( trim( (string) $email ) ); if ( '' === $email || ! self::boot() ) { return; } self::lead( array( 'goal_key' => 'newsletter_signup', 'provider' => 'newsletter', 'entry_id' => 0, 'source_key' => 'nl:' . substr( sha1( $email ), 0, 20 ) . ':' . gmdate( 'YmdHi' ), 'occurred_at' => gmdate( 'Y-m-d H:i:s' ), 'surface' => 'newsletter', 'email' => $email, ) ); } public static function on_newsletter_confirmed( $user ) { $email = is_object( $user ) && isset( $user->email ) ? (string) $user->email : ( is_array( $user ) && isset( $user['email'] ) ? (string) $user['email'] : '' ); if ( '' === $email || ! class_exists( 'AIEO_Submission_Context' ) ) { return; } $key = AIEO_Submission_Context::latest_unconfirmed( 'newsletter', $email, 30 * DAY_IN_SECONDS ); if ( '' === $key ) { return; } AIEO_Submission_Context::confirm( $key ); self::$newsletter_confirmed = true; } private static function live_submit( array $actions ) { foreach ( $actions as $action ) { foreach ( array( 'wp_ajax_', 'wp_ajax_nopriv_', 'admin_post_', 'admin_post_nopriv_' ) as $prefix ) { if ( doing_action( $prefix . $action ) ) { return true; } } } return false; } private static function lead( array $args ) { $ctx = class_exists( 'AIEO_Submission_Context' ) ? AIEO_Submission_Context::capture( array( 'surface' => isset( $args['surface'] ) ? (string) $args['surface'] : '', 'page_url' => isset( $args['page_url'] ) ? (string) $args['page_url'] : '', 'attribution' => isset( $args['attribution'] ) && is_array( $args['attribution'] ) ? $args['attribution'] : null, ) ) : null; $a = isset( $args['attribution'] ) && is_array( $args['attribution'] ) ? $args['attribution'] : ( $ctx ? $ctx['attribution'] : ( class_exists( 'AIEO_Goal_Tracker' ) ? AIEO_Goal_Tracker::attribution() : array() ) ); self::write_lead( array( 'goal_key' => (string) $args['goal_key'], 'provider' => (string) $args['provider'], 'form_id' => isset( $args['form_id'] ) ? (int) $args['form_id'] : 0, 'entry_id' => (int) $args['entry_id'], 'source_key' => (string) $args['source_key'], 'occurred_at' => (string) $args['occurred_at'], 'session_id' => $ctx ? (string) $ctx['session_id'] : '', 'attribution' => $a, ) ); if ( $ctx ) { AIEO_Submission_Context::store( (string) $args['source_key'], array( 'provider' => (string) $args['provider'], 'entry_id' => (int) $args['entry_id'], 'goal_key' => (string) $args['goal_key'], 'email' => isset( $args['email'] ) ? (string) $args['email'] : '', 'occurred_at' => (string) $args['occurred_at'], ), $ctx ); } } private static function write_lead( array $args ) { $a = isset( $args['attribution'] ) && is_array( $args['attribution'] ) ? $args['attribution'] : array(); AIEO_DMM_Conversions_Registry::record( array( 'conversion_type' => AIEO_DMM_Conversions_Registry::TYPE_GOAL, 'source_key' => (string) $args['source_key'], 'occurred_at' => (string) $args['occurred_at'], 'goal_key' => (string) $args['goal_key'], 'provider' => (string) $args['provider'], 'form_id' => ! empty( $args['form_id'] ) ? (int) $args['form_id'] : null, 'entry_id' => (int) $args['entry_id'], 'session_id' => (string) $args['session_id'], 'utm_source' => isset( $a['utm_source'] ) ? $a['utm_source'] : '', 'utm_medium' => isset( $a['utm_medium'] ) ? $a['utm_medium'] : '', 'utm_campaign' => isset( $a['utm_campaign'] ) ? $a['utm_campaign'] : '', 'utm_content' => isset( $a['utm_content'] ) ? $a['utm_content'] : '', 'utm_term' => isset( $a['utm_term'] ) ? $a['utm_term'] : '', 'channel' => isset( $a['channel'] ) ? $a['channel'] : ( class_exists( 'AIEO_Goal_Tracker' ) ? AIEO_Goal_Tracker::channel( $a ) : '' ), 'gclid' => isset( $a['gclid'] ) ? $a['gclid'] : '', 'fbclid' => isset( $a['fbclid'] ) ? $a['fbclid'] : '', 'landing_path' => self::path_of( isset( $a['landing_page'] ) ? $a['landing_page'] : '' ), 'referrer_host' => self::host_of( isset( $a['referrer'] ) ? $a['referrer'] : '' ), 'attr_origin' => 'aieo', 'value_basis' => 'goal_configured', ) ); } public static function counts_as_goal( $goal_key, $form_id = 0 ) { if ( 'form_submit_gravity' === $goal_key ) { return self::form_is_goal( (int) $form_id ); } if ( ! class_exists( 'AIEO_Goal_Tracker' ) ) { return true; } $cfg = AIEO_Goal_Tracker::config(); return isset( $cfg['builtin'][ $goal_key ] ) ? ! empty( $cfg['builtin'][ $goal_key ] ) : true; } private static function goal_on( $key ) { if ( ! class_exists( 'AIEO_Goal_Tracker' ) ) { return false; } $cfg = AIEO_Goal_Tracker::config(); return ! empty( $cfg['builtin'][ $key ] ); } public static function on_gravity( $entry, $form ) { if ( ! class_exists( 'AIEO_Goal_Tracker' ) || ! is_array( $entry ) ) { return; } $form_id = isset( $form['id'] ) ? (int) $form['id'] : 0; if ( $form_id <= 0 ) { return; } self::write_entry( $form, $entry, true ); } public static function on_goal( $goal_key, $meta, $context ) { if ( 'form_submit_gravity' === $goal_key || isset( self::ENTRY_WRITTEN_GOALS[ $goal_key ] ) ) { return; } if ( 'newsletter_signup' === $goal_key && ( self::$newsletter_confirmed || self::live_submit( array( 'aieo_newsletter_subscribe' ) ) ) ) { return; } if ( ! self::boot() ) { return; } $a = is_array( $context ) && isset( $context['attribution'] ) ? (array) $context['attribution'] : array(); $sid = isset( $context['session_id'] ) ? (string) $context['session_id'] : ''; AIEO_DMM_Conversions_Registry::record( array( 'conversion_type' => AIEO_DMM_Conversions_Registry::TYPE_GOAL, 'source_key' => AIEO_DMM_Conversions_Registry::source_key_for_goal( $goal_key, $sid, time() * 1000 ), 'occurred_at' => gmdate( 'Y-m-d H:i:s' ), 'goal_key' => $goal_key, 'provider' => '', 'session_id' => $sid, 'utm_source' => isset( $a['utm_source'] ) ? $a['utm_source'] : '', 'utm_medium' => isset( $a['utm_medium'] ) ? $a['utm_medium'] : '', 'utm_campaign' => isset( $a['utm_campaign'] ) ? $a['utm_campaign'] : '', 'utm_content' => isset( $a['utm_content'] ) ? $a['utm_content'] : '', 'utm_term' => isset( $a['utm_term'] ) ? $a['utm_term'] : '', 'gclid' => isset( $a['gclid'] ) ? $a['gclid'] : '', 'fbclid' => isset( $a['fbclid'] ) ? $a['fbclid'] : '', 'landing_path' => self::path_of( isset( $a['landing_page'] ) ? $a['landing_page'] : '' ), 'referrer_host' => self::host_of( isset( $a['referrer'] ) ? $a['referrer'] : '' ), 'attr_origin' => 'aieo', 'value_basis' => 'goal_configured', ) ); } public static function write_entry( $form, $entry, $live = false ) { if ( ! self::boot() ) { return false; } $form_id = isset( $form['id'] ) ? (int) $form['id'] : 0; $entry_id = isset( $entry['id'] ) ? (int) $entry['id'] : 0; if ( $form_id <= 0 || $entry_id <= 0 ) { return false; } $a = AIEO_Goal_Tracker::entry_attribution( $form, $entry ); $ctx = null; if ( $live && class_exists( 'AIEO_Submission_Context' ) ) { $ctx = AIEO_Submission_Context::capture( array( 'surface' => 'gravity_' . $form_id, 'page_url' => isset( $entry['source_url'] ) ? (string) $entry['source_url'] : '', ) ); $a = is_array( $a ) ? $a : array(); foreach ( $ctx['attribution'] as $k => $v ) { if ( '' !== (string) $v ) { $a[ $k ] = $v; } } } $when = isset( $entry['date_created'] ) && $entry['date_created'] ? (string) $entry['date_created'] : gmdate( 'Y-m-d H:i:s' ); $ok = false !== AIEO_DMM_Conversions_Registry::record( array( 'conversion_type' => AIEO_DMM_Conversions_Registry::TYPE_GOAL, 'source_key' => AIEO_DMM_Conversions_Registry::source_key_for_entry( $form_id, $entry_id ), 'occurred_at' => $when, 'goal_key' => 'form_submit_gravity', 'provider' => 'gravity', 'form_id' => $form_id, 'entry_id' => $entry_id, 'session_id' => '', 'utm_source' => isset( $a['utm_source'] ) ? $a['utm_source'] : '', 'utm_medium' => isset( $a['utm_medium'] ) ? $a['utm_medium'] : '', 'utm_campaign' => isset( $a['utm_campaign'] ) ? $a['utm_campaign'] : '', 'utm_content' => isset( $a['utm_content'] ) ? $a['utm_content'] : '', 'utm_term' => isset( $a['utm_term'] ) ? $a['utm_term'] : '', 'gclid' => isset( $a['gclid'] ) ? $a['gclid'] : '', 'fbclid' => isset( $a['fbclid'] ) ? $a['fbclid'] : '', 'landing_path' => self::path_of( isset( $a['landing_page'] ) ? $a['landing_page'] : '' ), 'referrer_host' => self::host_of( isset( $a['referrer'] ) ? $a['referrer'] : '' ), 'attr_origin' => $ctx ? 'aieo' : 'entry', 'value_basis' => 'goal_configured', ) ); if ( $ok && $ctx ) { AIEO_Submission_Context::store( AIEO_DMM_Conversions_Registry::source_key_for_entry( $form_id, $entry_id ), array( 'provider' => 'gravity', 'entry_id' => $entry_id, 'goal_key' => 'form_submit_gravity', 'email' => self::gravity_email( $form, $entry ), 'occurred_at' => $when, ), $ctx ); } return $ok; } public static function gravity_entry_box( $form, $entry ) { if ( ! current_user_can( 'manage_woocommerce' ) && ! current_user_can( 'manage_options' ) ) { return; } $form_id = is_array( $form ) && isset( $form['id'] ) ? (int) $form['id'] : 0; $entry_id = is_array( $entry ) && isset( $entry['id'] ) ? (int) $entry['id'] : 0; if ( $form_id <= 0 || $entry_id <= 0 || ! self::boot() ) { return; } if ( ! class_exists( 'AIEO_Submission_Panel', false ) && is_readable( __DIR__ . '/class-aieo-submission-panel.php' ) ) { require_once __DIR__ . '/class-aieo-submission-panel.php'; } $key = AIEO_DMM_Conversions_Registry::source_key_for_entry( $form_id, $entry_id ); $lead = class_exists( 'AIEO_Submission_Panel' ) ? AIEO_Submission_Panel::lead( $key ) : null; if ( ! $lead ) { return; } $row = $lead['row']; echo '
' . esc_html( '' !== (string) $row['canon_campaign'] ? (string) $row['canon_campaign'] : __( '(no campaign: direct or untagged)', 'ai-eshop-optimizer' ) ) . '';
$via = trim( (string) $row['canon_source'] . ' / ' . (string) $row['canon_medium'], ' /' );
if ( '' !== $via ) { echo '
' . esc_html( $via ); }
echo '
' . esc_html__( 'Campaign and journey', 'ai-eshop-optimizer' ) . '
'; echo '