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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
| # ROI计算工具
class ROICalculator:
"""ROI计算器"""
@staticmethod
def calculate_basic_roi(revenue, cost):
"""
计算基础ROI
revenue: 收入
cost: 成本
"""
if cost == 0:
return float('inf')
roi = ((revenue - cost) / cost) * 100
return round(roi, 2)
@staticmethod
def calculate_roas(revenue, ad_spend):
"""
计算ROAS (Return on Ad Spend)
revenue: 收入
ad_spend: 广告花费
"""
if ad_spend == 0:
return float('inf')
roas = revenue / ad_spend
return round(roas, 2)
@staticmethod
def calculate_break_even_roas(profit_margin):
"""
计算盈亏平衡ROAS
profit_margin: 利润率 (0-1)
"""
if profit_margin == 0:
return float('inf')
break_even_roas = 1 / profit_margin
return round(break_even_roas, 2)
@staticmethod
def calculate_ltv_cac_ratio(ltv, cac):
"""
计算LTV/CAC比率
ltv: 用户生命周期价值
cac: 获客成本
"""
if cac == 0:
return float('inf')
ratio = ltv / cac
return round(ratio, 2)
@staticmethod
def calculate_payback_period(cac, monthly_arpu):
"""
计算回本周期
cac: 获客成本
monthly_arpu: 月均每用户收入
"""
if monthly_arpu == 0:
return float('inf')
payback_months = cac / monthly_arpu
return round(payback_months, 1)
def calculate_channel_roi(self, channel_data):
"""
计算各渠道ROI
channel_data: {
'channel_name': {
'spend': 广告花费,
'impressions': 展示次数,
'clicks': 点击次数,
'conversions': 转化次数,
'revenue': 收入
}
}
"""
results = []
for channel, data in channel_data.items():
spend = data['spend']
revenue = data['revenue']
clicks = data['clicks']
conversions = data['conversions']
# 基础指标
roi = self.calculate_basic_roi(revenue, spend)
roas = self.calculate_roas(revenue, spend)
# 衍生指标
cpc = spend / clicks if clicks > 0 else 0
cpa = spend / conversions if conversions > 0 else 0
ctr = (clicks / data['impressions'] * 100) if data['impressions'] > 0 else 0
cvr = (conversions / clicks * 100) if clicks > 0 else 0
results.append({
'channel': channel,
'spend': round(spend, 2),
'revenue': round(revenue, 2),
'profit': round(revenue - spend, 2),
'roi': f"{roi}%",
'roas': f"{roas}x",
'cpc': round(cpc, 2),
'cpa': round(cpa, 2),
'ctr': round(ctr, 2),
'cvr': round(cvr, 2)
})
return pd.DataFrame(results)
def calculate_marketing_mix_roi(self, marketing_data):
"""
计算营销组合ROI
marketing_data: {
'channels': 渠道数据,
'total_revenue': 总收入,
'baseline_revenue': 基线收入(无营销投入)
}
"""
total_spend = sum(ch['spend'] for ch in marketing_data['channels'].values())
incremental_revenue = marketing_data['total_revenue'] - marketing_data['baseline_revenue']
marketing_roi = self.calculate_basic_roi(incremental_revenue, total_spend)
marketing_roas = self.calculate_roas(incremental_revenue, total_spend)
return {
'total_spend': round(total_spend, 2),
'total_revenue': round(marketing_data['total_revenue'], 2),
'baseline_revenue': round(marketing_data['baseline_revenue'], 2),
'incremental_revenue': round(incremental_revenue, 2),
'marketing_roi': f"{marketing_roi}%",
'marketing_roas': f"{marketing_roas}x"
}
|