File size: 2,710 Bytes
727abcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from flask import Flask, render_template, request, jsonify
import logging

app = Flask(__name__, static_folder='static', template_folder='templates')

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/api/simulate', methods=['POST'])
def simulate():
    try:
        data = request.get_json(silent=True)
        if not data:
            return jsonify({'error': 'Invalid JSON data or empty body'}), 400

        traffic = float(data.get('traffic', 10000))
        cpc = float(data.get('cpc', 1.5))
        product_price = float(data.get('product_price', 99))
        steps = data.get('steps', [])

        if not steps or not isinstance(steps, list):
            return jsonify({'error': 'No steps provided or invalid format'}), 400

        # Calculate funnel flow
        current_users = traffic
        total_spend = traffic * cpc
        
        results = []
        
        # Add Initial Step (Traffic Source)
        results.append({
            'step_name': 'Traffic Source',
            'users': int(current_users),
            'conversion_rate': 100,
            'dropoff': 0
        })

        for step in steps:
            name = step.get('name', 'Unknown Step')
            rate = float(step.get('rate', 50)) / 100.0 # Convert percentage to decimal
            
            next_users = current_users * rate
            dropoff = current_users - next_users
            
            results.append({
                'step_name': name,
                'users': int(next_users),
                'conversion_rate': step.get('rate', 50),
                'dropoff': int(dropoff)
            })
            
            current_users = next_users

        final_users = current_users
        total_revenue = final_users * product_price
        roi = ((total_revenue - total_spend) / total_spend) * 100 if total_spend > 0 else 0
        cac = total_spend / final_users if final_users > 0 else 0

        response = {
            'metrics': {
                'total_spend': round(total_spend, 2),
                'total_revenue': round(total_revenue, 2),
                'roi': round(roi, 2),
                'cac': round(cac, 2),
                'final_conversions': int(final_users)
            },
            'funnel_data': results
        }
        
        return jsonify(response)

    except Exception as e:
        logger.error(f"Error in simulation: {e}")
        return jsonify({'error': str(e)}), 500

@app.route('/health')
def health():
    return jsonify({'status': 'healthy'}), 200

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=True)