-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_time_lag.py
101 lines (86 loc) · 3.05 KB
/
google_time_lag.py
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
import os
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
def main():
# File path
csv_file = 'bquxjob_777a5640_19272ca89e0.csv'
if not os.path.exists(csv_file):
raise FileNotFoundError(f"File '{csv_file}' not found.")
df = pd.read_csv(csv_file)
required_columns = ['time_lag_bucket', 'conversions', 'percentage_of_total']
if not all(col in df.columns for col in required_columns):
missing_cols = [col for col in required_columns if col not in df.columns]
raise ValueError(f"Missing columns in data: {', '.join(missing_cols)}")
fig = create_time_lag_vs_conversions_figure(df)
fig.show()
fig.write_image("google_time_lag.png", scale=5, width=1920, height=1080)
def create_time_lag_vs_conversions_figure(df):
# Define colors and fonts
colors = {
'conversions': 'rgba(99,110,250,0.7)',
'percentage_of_total': 'rgba(239,85,59,0.8)',
'grid': 'lightgray',
'text': '#000000',
'background': 'white'
}
fonts = {
'title': dict(size=24, color=colors['text'], family="Arial Black"),
'axis_title': dict(size=18, color=colors['text'], family="Arial Black"),
'tickfont': dict(size=14, color=colors['text'], family="Arial"),
'legendfont': dict(size=16, color=colors['text'], family="Arial")
}
max_percentage = df['percentage_of_total'].max()
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(
go.Bar(
x=df['time_lag_bucket'],
y=df['conversions'],
name="Conversions",
marker_color=colors['conversions'],
),
secondary_y=False,
)
fig.add_trace(
go.Scatter(
x=df['time_lag_bucket'],
y=df['percentage_of_total'],
name="Percentage of Total",
line=dict(color=colors['percentage_of_total'], width=3),
mode='lines+markers',
),
secondary_y=True,
)
fig.update_layout(
title=dict(text='Time Lag vs Conversions', font=fonts['title']),
xaxis=dict(
title=dict(text='Time Lag (Days)', font=fonts['axis_title']),
tickfont=fonts['tickfont'],
showgrid=True, gridwidth=1, gridcolor=colors['grid'],
),
yaxis=dict(
title=dict(text='Number of Conversions', font=fonts['axis_title']),
tickfont=fonts['tickfont'],
showgrid=True, gridwidth=1, gridcolor=colors['grid'],
),
yaxis2=dict(
title=dict(text='Percentage of Total Conversions', font=fonts['axis_title']),
tickfont=fonts['tickfont'],
range=[0, max_percentage * 1.1],
showgrid=False,
),
legend=dict(
font=fonts['legendfont'],
orientation='h',
yanchor='bottom',
y=1.02,
xanchor='right',
x=1
),
plot_bgcolor=colors['background'],
height=800,
width=1200,
)
return fig
if __name__ == "__main__":
main()