dylanplummer commited on
Commit
f8fbbae
·
1 Parent(s): 76a8a28

init commit

Browse files
Files changed (2) hide show
  1. app.py +90 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from google.analytics.data_v1beta import BetaAnalyticsDataClient
2
+ from google.analytics.data_v1beta.types import (
3
+ DateRange,
4
+ Dimension,
5
+ Metric,
6
+ RunReportRequest,
7
+ RunRealtimeReportRequest
8
+ )
9
+
10
+ import gradio as gr
11
+ import os
12
+ import json
13
+ import pandas as pd
14
+ import seaborn as sns
15
+ import matplotlib.pyplot as plt
16
+
17
+ plt.style.use('fivethirtyeight')
18
+
19
+ FINISHED_EXERCISE = 'finished_exercise'
20
+ PROPERTY_ID = "384068977"
21
+
22
+ credentials_json = os.environ['GOOGLE_APPLICATION_CREDENTIALS_JSON']
23
+ credentials_dict = json.loads(credentials_json)
24
+ # write json to file
25
+ with open('credentials.json', 'w') as f:
26
+ json.dump(credentials_dict, f)
27
+ # set env var to filename
28
+ os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.join(os.path.dirname(__file__), 'credentials.json')
29
+
30
+
31
+ def full_report():
32
+ client = BetaAnalyticsDataClient()
33
+
34
+ request = RunReportRequest(
35
+ property=f"properties/{PROPERTY_ID}",
36
+ dimensions=[Dimension(name="hour"),
37
+ Dimension(name="nthDay"),
38
+ Dimension(name='eventName'),
39
+ Dimension(name="continent"),
40
+ Dimension(name="country")],
41
+ metrics=[Metric(name="eventValue")],
42
+ #return_property_quota=True,
43
+ date_ranges=[DateRange(start_date="2023-06-30", end_date="today")],
44
+ )
45
+ response = client.run_report(request)
46
+
47
+ print(response)
48
+
49
+
50
+
51
+ total_jumps = 0
52
+ res = {'day': [], 'jumps': [], 'continent': [], 'country': []}
53
+
54
+ for row in response.rows:
55
+ date = row.dimension_values[0].value # YYYMMDD (e.g 20230715)
56
+ day = int(row.dimension_values[1].value)
57
+ event_name = row.dimension_values[2].value
58
+ continent = row.dimension_values[3].value
59
+ country = row.dimension_values[4].value
60
+ if event_name == FINISHED_EXERCISE:
61
+ event_value = float(row.metric_values[0].value)
62
+ total_jumps += int(event_value)
63
+ res['day'].append(day)
64
+ res['jumps'].append(event_value)
65
+ res['continent'].append(continent)
66
+ res['country'].append(country)
67
+
68
+ print(f"Total jumps: {total_jumps}")
69
+
70
+ df = pd.DataFrame.from_dict(res)
71
+ country_df = df.groupby(['country']).sum().reset_index()
72
+ country_df = country_df.sort_values(by=['jumps'], ascending=False)
73
+ print(country_df)
74
+
75
+ country_avg = df.groupby(['country']).mean().reset_index()
76
+ country_avg = country_avg.sort_values(by=['jumps'], ascending=False)
77
+ print(country_avg)
78
+
79
+ return f"Total Jumps: {total_jumps}"
80
+
81
+
82
+ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
83
+ with gr.Row():
84
+ total_jumps_label = gr.Markdown("Total Jumps: 0")
85
+ out_plot = gr.Plot(label="Total Jumps")
86
+
87
+ dep = demo.load(full_report, None, total_jumps_label, every=1)
88
+
89
+ if __name__ == "__main__":
90
+ demo.queue(api_open=True, max_size=15).launch(share=False)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ google-analytics-data
2
+ pandas<2.0.0
3
+ seaborn
4
+ matplotlib
5
+ plotly