Spin Wheel

⌘K
  1. Home
  2. Spin Wheel
  3. Hooks & Filters (Deve...
  4. Before random selection algorithm (uspw_before_prize_selection)

Before random selection algorithm (uspw_before_prize_selection)

uspw_before_prize_selection

Description

Fires before the prize selection algorithm runs, allowing developers to track analytics, implement A/B testing, or modify the prize pool before selection.

Hook Type: Action
Since: 1.0.0

Parameters

ParameterTypeDescription
$campaign_idintThe ID of the spin wheel campaign
$weighted_prizesarrayAvailable prizes with their weights
$user_dataarrayUser submitted data (email, name, phone, etc.)

Usage

add_action( 'uspw_before_prize_selection', 'my_prize_tracking', 10, 3 );
function my_prize_tracking( $campaign_id, $weighted_prizes, $user_data ) {
    // Your custom logic here
}

Real-World Examples

Example 1: Track Prize Pool Analytics

Log the available prizes and their probabilities for analytics and reporting.

add_action( 'uspw_before_prize_selection', 'track_prize_analytics', 10, 3 );
function track_prize_analytics( $campaign_id, $weighted_prizes, $user_data ) {
    $analytics_data = [
        'campaign_id' => $campaign_id,
        'timestamp' => current_time( 'mysql' ),
        'user_email' => $user_data['email'] ?? 'anonymous',
        'available_prizes' => count( $weighted_prizes ),
        'total_weight' => array_sum( wp_list_pluck( $weighted_prizes, 'weight' ) ),
        'prize_distribution' => array_map( function( $prize ) {
            return [
                'label' => $prize['data']['label'] ?? '',
                'weight' => $prize['weight']
            ];
        }, $weighted_prizes )
    ];
    
    // Log to custom table or external service
    error_log( 'Prize Selection Analytics: ' . wp_json_encode( $analytics_data ) );
    
    // Or send to Google Analytics
    if ( function_exists( 'gtag' ) ) {
        gtag( 'event', 'prize_pool_ready', [
            'event_category' => 'Spin Wheel',
            'event_label' => 'Campaign ' . $campaign_id,
            'value' => count( $weighted_prizes )
        ] );
    }
}

Example 2: Implement A/B Testing

Test different prize distributions for different user segments.

add_action( 'uspw_before_prize_selection', 'ab_test_prize_distribution', 10, 3 );
function ab_test_prize_distribution( $campaign_id, $weighted_prizes, $user_data ) {
    // Only apply to test campaign
    if ( $campaign_id !== 555 ) {
        return;
    }
    
    // Determine user segment (e.g., based on email domain)
    $email_domain = '';
    if ( ! empty( $user_data['email'] ) ) {
        $email_domain = substr( strrchr( $user_data['email'], '@' ), 1 );
    }
    
    $is_business_email = in_array( $email_domain, [ 'company.com', 'business.org' ], true );
    
    // Log which variant the user is seeing
    $variant = $is_business_email ? 'B2B' : 'B2C';
    
    update_option( 'ab_test_spin_' . md5( $user_data['email'] ?? '' ), [
        'variant' => $variant,
        'timestamp' => time(),
        'campaign_id' => $campaign_id
    ] );
    
    // Track in analytics
    if ( function_exists( 'gtag' ) ) {
        gtag( 'event', 'ab_test_assignment', [
            'event_category' => 'Spin Wheel AB Test',
            'event_label' => $variant,
            'campaign_id' => $campaign_id
        ] );
    }
}

Example 3: Send Real-Time Notifications

Notify admins when high-value prizes are about to be selected.

add_action( 'uspw_before_prize_selection', 'notify_high_value_spin', 10, 3 );
function notify_high_value_spin( $campaign_id, $weighted_prizes, $user_data ) {
    // Check if any high-value prizes are in the pool
    $high_value_threshold = 50; // $50 or more
    
    foreach ( $weighted_prizes as $prize ) {
        $prize_value = floatval( $prize['data']['value'] ?? 0 );
        
        if ( $prize_value >= $high_value_threshold ) {
            // Send Slack notification
            $slack_webhook = get_option( 'spin_wheel_slack_webhook' );
            
            if ( $slack_webhook ) {
                wp_remote_post( $slack_webhook, [
                    'body' => wp_json_encode( [
                        'text' => sprintf(
                            '🎰 High-value spin in progress!\nCampaign: %d\nUser: %s\nPotential Prize: $%s',
                            $campaign_id,
                            $user_data['email'] ?? 'Unknown',
                            number_format( $prize_value, 2 )
                        )
                    ] ),
                    'headers' => [ 'Content-Type' => 'application/json' ]
                ] );
            }
            
            break; // Only notify once
        }
    }
}

Example 4: Log User Behavior Patterns

Track user behavior and spin patterns for machine learning or fraud detection.

add_action( 'uspw_before_prize_selection', 'log_user_behavior', 10, 3 );
function log_user_behavior( $campaign_id, $weighted_prizes, $user_data ) {
    global $wpdb;
    
    $behavior_data = [
        'campaign_id' => $campaign_id,
        'user_email' => $user_data['email'] ?? '',
        'user_ip' => $_SERVER['REMOTE_ADDR'] ?? '',
        'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
        'referer' => $_SERVER['HTTP_REFERER'] ?? '',
        'timestamp' => current_time( 'mysql' ),
        'available_prizes' => count( $weighted_prizes ),
        'session_id' => session_id(),
        'utm_source' => $_GET['utm_source'] ?? '',
        'utm_campaign' => $_GET['utm_campaign'] ?? ''
    ];
    
    // Store in custom table for analysis
    $table_name = $wpdb->prefix . 'spin_behavior_log';
    $wpdb->insert( $table_name, $behavior_data );
    
    // Check for suspicious patterns
    $recent_spins = $wpdb->get_var( $wpdb->prepare(
        "SELECT COUNT(*) FROM {$table_name} 
        WHERE user_ip = %s 
        AND timestamp > DATE_SUB(NOW(), INTERVAL 1 HOUR)",
        $behavior_data['user_ip']
    ) );
    
    if ( $recent_spins > 10 ) {
        // Flag for review
        wp_mail(
            get_option( 'admin_email' ),
            'Suspicious Spin Activity Detected',
            sprintf( 'IP %s has made %d spins in the last hour.', $behavior_data['user_ip'], $recent_spins )
        );
    }
}

Example 5: Integrate with External CRM

Sync spin attempts with your CRM system before prize selection.

add_action( 'uspw_before_prize_selection', 'sync_with_crm', 10, 3 );
function sync_with_crm( $campaign_id, $weighted_prizes, $user_data ) {
    if ( empty( $user_data['email'] ) ) {
        return;
    }
    
    // HubSpot integration example
    $hubspot_api_key = get_option( 'hubspot_api_key' );
    
    if ( $hubspot_api_key ) {
        $endpoint = 'https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/' . urlencode( $user_data['email'] );
        
        $contact_data = [
            'properties' => [
                [
                    'property' => 'email',
                    'value' => $user_data['email']
                ],
                [
                    'property' => 'firstname',
                    'value' => $user_data['name'] ?? ''
                ],
                [
                    'property' => 'phone',
                    'value' => $user_data['phone'] ?? ''
                ],
                [
                    'property' => 'spin_wheel_campaign',
                    'value' => $campaign_id
                ],
                [
                    'property' => 'last_spin_date',
                    'value' => current_time( 'timestamp' ) * 1000 // HubSpot uses milliseconds
                ],
                [
                    'property' => 'spin_wheel_status',
                    'value' => 'In Progress'
                ]
            ]
        ];
        
        wp_remote_post( $endpoint, [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => 'Bearer ' . $hubspot_api_key
            ],
            'body' => wp_json_encode( $contact_data )
        ] );
    }
}

Notes

  • This hook fires after validation but before the random prize selection algorithm runs.
  • The $weighted_prizes array contains only eligible prizes (inventory checked, probabilities > 0).
  • Perfect for analytics, tracking, and external integrations that don’t need to modify the selection process.
  • Does not allow modification of the prize pool (use filters for that purpose).

How can we help?