'page_viewed', 'view_content' => 'contents_viewed', 'add_to_cart' => 'items_added', 'initiate_checkout' => 'checkout_started', 'purchase' => 'order_created', 'complete_registration' => 'registration_completed', 'lead' => 'lead_created', 'subscribe' => 'subscription_created', 'start_trial' => 'trial_started', 'schedule' => 'appointment_scheduled', ); private static $instance = null; public static function instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } public static function track( $event, $args = array() ) { return self::instance()->send( $event, $args ); } public function register_hooks() { add_action( 'aieo_openai_track', array( $this, 'send' ), 10, 2 ); } private function settings() { $all = get_option( AIEO_DMM_Conversions::OPTION_KEY, array() ); return is_array( $all ) ? $all : array(); } private function get( $key, $default = '' ) { $s = $this->settings(); return isset( $s[ $key ] ) && '' !== $s[ $key ] ? $s[ $key ] : $default; } public function is_enabled() { $mode = (string) $this->get( 'openai_mode' ); return in_array( $mode, array( 'server', 'both' ), true ) && '' !== (string) $this->get( 'openai_pixel_id' ) && '' !== (string) $this->get( 'openai_access_token' ); } private function hash( $value ) { $value = strtolower( trim( (string) $value ) ); return '' === $value ? '' : hash( 'sha256', $value ); } private function build_user( $args ) { $user = array(); if ( ! empty( $args['email'] ) ) { $user['email_sha256'] = $this->hash( $args['email'] ); } if ( ! empty( $args['external_id'] ) ) { $user['external_id_sha256'] = $this->hash( $args['external_id'] ); } foreach ( array( 'country' => 'country', 'city' => 'city', 'zip' => 'zip_code' ) as $in => $out ) { if ( ! empty( $args[ $in ] ) ) { $user[ $out ] = strtolower( trim( (string) $args[ $in ] ) ); } } return $user; } private function build_data( $event, $args ) { $type = in_array( $event, array( 'subscription_created', 'trial_started' ), true ) ? 'plan_enrollment' : ( ! empty( $args['contents'] ) ? 'contents' : 'customer_action' ); $data = array( 'type' => $type ); $currency = strtoupper( (string) ( isset( $args['currency'] ) ? $args['currency'] : '' ) ); if ( isset( $args['value'] ) && '' !== $args['value'] && '' !== $currency ) { $data['amount'] = AIEO_OpenAI_Money::to_minor( $args['value'], $currency ); $data['currency'] = $currency; } if ( ! empty( $args['contents'] ) && is_array( $args['contents'] ) ) { $items = array(); foreach ( $args['contents'] as $item ) { $row = array(); if ( isset( $item['id'] ) ) { $row['id'] = (string) $item['id']; } if ( isset( $item['name'] ) ) { $row['name'] = (string) $item['name']; } $row['content_type'] = isset( $item['content_type'] ) ? (string) $item['content_type'] : 'product'; if ( isset( $item['quantity'] ) ) { $row['quantity'] = (int) $item['quantity']; } if ( isset( $item['amount'] ) && '' !== $currency ) { $row['amount'] = AIEO_OpenAI_Money::to_minor( $item['amount'], $currency ); $row['currency'] = $currency; } $items[] = $row; } $data['contents'] = $items; } return $data; } private function resolve_click_id( $args ) { if ( ! empty( $args['oppref'] ) ) { return (string) $args['oppref']; } if ( ! empty( $_GET[ self::CLICK_ID_PARAM ] ) ) { return sanitize_text_field( wp_unslash( $_GET[ self::CLICK_ID_PARAM ] ) ); } if ( ! empty( $_COOKIE[ self::CLICK_ID_COOKIE ] ) ) { return sanitize_text_field( wp_unslash( $_COOKIE[ self::CLICK_ID_COOKIE ] ) ); } return ''; } public function send( $event, $args = array() ) { if ( ! $this->is_enabled() ) { return new WP_Error( 'aieo_openai_disabled', 'OpenAI server events are not configured.' ); } $name = isset( self::EVENT_MAP[ $event ] ) ? self::EVENT_MAP[ $event ] : (string) $event; if ( ! in_array( $name, self::SUPPORTED_EVENTS, true ) ) { return new WP_Error( 'aieo_openai_bad_event', sprintf( 'Unsupported OpenAI event: %s', $name ) ); } if ( 'custom' === $name && empty( $args['custom_event_name'] ) ) { return new WP_Error( 'aieo_openai_custom_name', 'A custom event needs custom_event_name.' ); } $event_obj = array( 'id' => ! empty( $args['event_id'] ) ? (string) $args['event_id'] : wp_generate_uuid4(), 'type' => $name, 'timestamp_ms' => ! empty( $args['timestamp_ms'] ) ? (int) $args['timestamp_ms'] : (int) round( microtime( true ) * 1000 ), 'action_source' => 'web', 'source_url' => ! empty( $args['url'] ) ? esc_url_raw( $args['url'] ) : home_url( '/' ), 'data' => $this->build_data( $name, $args ), ); if ( 'custom' === $name ) { $event_obj['custom_event_name'] = sanitize_text_field( (string) $args['custom_event_name'] ); } $click = $this->resolve_click_id( $args ); if ( '' !== $click ) { $event_obj['oppref'] = $click; } $user = $this->build_user( $args ); if ( ! empty( $user ) ) { $event_obj['user'] = $user; } $body = array( 'integration_source' => self::INTEGRATION_SOURCE, 'validate_only' => (bool) $this->get( 'openai_validate_only', false ), 'events' => array( $event_obj ), ); $url = add_query_arg( 'pid', rawurlencode( (string) $this->get( 'openai_pixel_id' ) ), self::ENDPOINT ); $resp = wp_remote_post( $url, array( 'timeout' => 10, 'headers' => array( 'Authorization' => 'Bearer ' . (string) $this->get( 'openai_access_token' ), 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $resp ) ) { return $resp; } return array( 'code' => (int) wp_remote_retrieve_response_code( $resp ), 'body' => wp_remote_retrieve_body( $resp ), 'event_id' => $event_obj['id'], 'sent' => $body, ); } }