Spaces:
Running
Running
Update analytics_fetch_and_rendering.py
Browse files
analytics_fetch_and_rendering.py
CHANGED
@@ -9,6 +9,8 @@ import html
|
|
9 |
from sessions import create_session
|
10 |
from error_handling import display_error
|
11 |
|
|
|
|
|
12 |
API_V2_BASE = 'https://api.linkedin.com/v2'
|
13 |
API_REST_BASE = 'https://api.linkedin.com/rest'
|
14 |
|
@@ -122,6 +124,65 @@ def plot_growth_rate(data, total):
|
|
122 |
plt.tight_layout()
|
123 |
return fig
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
def fetch_and_render_analytics(client_id, token):
|
126 |
loading = gr.update(value="<p>Loading follower count...</p>", visible=True)
|
127 |
hidden = gr.update(value=None, visible=False)
|
@@ -132,6 +193,9 @@ def fetch_and_render_analytics(client_id, token):
|
|
132 |
|
133 |
try:
|
134 |
name, count, gains = fetch_analytics_data(client_id, token)
|
|
|
|
|
|
|
135 |
|
136 |
count_html = f"""
|
137 |
<div style='text-align:center; padding:20px; background:#e7f3ff; border:1px solid #bce8f1; border-radius:8px;'>
|
@@ -141,7 +205,7 @@ def fetch_and_render_analytics(client_id, token):
|
|
141 |
<p style='font-size:0.9em; color:#777;'>(As of latest data)</p>
|
142 |
</div>
|
143 |
"""
|
144 |
-
return gr.update(value=count_html, visible=True), gr.update(value=plot_follower_gains(gains), visible=True), gr.update(value=plot_growth_rate(gains, count), visible=True)
|
145 |
|
146 |
except Exception as e:
|
147 |
error = display_error("Analytics load failed.", e).get('value', "<p style='color:red;'>Error loading data.</p>")
|
|
|
9 |
from sessions import create_session
|
10 |
from error_handling import display_error
|
11 |
|
12 |
+
from Update Data_Fetching_and_Rendering import fetch_posts_and_stats
|
13 |
+
|
14 |
API_V2_BASE = 'https://api.linkedin.com/v2'
|
15 |
API_REST_BASE = 'https://api.linkedin.com/rest'
|
16 |
|
|
|
124 |
plt.tight_layout()
|
125 |
return fig
|
126 |
|
127 |
+
def compute_monthly_avg_engagement_rate(posts):
|
128 |
+
from collections import defaultdict
|
129 |
+
import statistics
|
130 |
+
|
131 |
+
if not posts:
|
132 |
+
return []
|
133 |
+
|
134 |
+
monthly_data = defaultdict(lambda: {"engagement_sum": 0, "post_count": 0, "impression_total": 0})
|
135 |
+
|
136 |
+
for post in posts:
|
137 |
+
try:
|
138 |
+
month = post["when"][:7] # Format: YYYY-MM
|
139 |
+
likes = post.get("likes", 0)
|
140 |
+
comments = post.get("comments", 0)
|
141 |
+
shares = post.get("shares", 0)
|
142 |
+
clicks = post.get("clicks", 0)
|
143 |
+
impressions = post.get("impressions", 0)
|
144 |
+
|
145 |
+
engagement = likes + comments + shares + clicks
|
146 |
+
monthly_data[month]["engagement_sum"] += engagement
|
147 |
+
monthly_data[month]["post_count"] += 1
|
148 |
+
monthly_data[month]["impression_total"] += impressions
|
149 |
+
except Exception:
|
150 |
+
continue
|
151 |
+
|
152 |
+
results = []
|
153 |
+
for month in sorted(monthly_data.keys()):
|
154 |
+
data = monthly_data[month]
|
155 |
+
if data["post_count"] == 0 or data["impression_total"] == 0:
|
156 |
+
rate = 0
|
157 |
+
else:
|
158 |
+
avg_impressions = data["impression_total"] / data["post_count"]
|
159 |
+
rate = (data["engagement_sum"] / (data["post_count"] * avg_impressions)) * 100
|
160 |
+
results.append({"month": month, "engagement_rate": round(rate, 2)})
|
161 |
+
|
162 |
+
return results
|
163 |
+
|
164 |
+
def plot_avg_engagement_rate(data):
|
165 |
+
import matplotlib.pyplot as plt
|
166 |
+
|
167 |
+
if not data:
|
168 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
169 |
+
ax.text(0.5, 0.5, 'No engagement data.', ha='center', va='center', transform=ax.transAxes)
|
170 |
+
ax.set_title('Average Post Engagement Rate (%)')
|
171 |
+
ax.set_xticks([]); ax.set_yticks([])
|
172 |
+
return fig
|
173 |
+
|
174 |
+
months = [d["month"] for d in data]
|
175 |
+
rates = [d["engagement_rate"] for d in data]
|
176 |
+
|
177 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
178 |
+
ax.plot(months, rates, label="Engagement Rate (%)", marker="s", color="#ff7f0e")
|
179 |
+
ax.set(title="Average Post Engagement Rate (%)", xlabel="Month", ylabel="Engagement Rate %")
|
180 |
+
ax.tick_params(axis='x', rotation=45)
|
181 |
+
ax.legend()
|
182 |
+
plt.tight_layout()
|
183 |
+
return fig
|
184 |
+
|
185 |
+
|
186 |
def fetch_and_render_analytics(client_id, token):
|
187 |
loading = gr.update(value="<p>Loading follower count...</p>", visible=True)
|
188 |
hidden = gr.update(value=None, visible=False)
|
|
|
193 |
|
194 |
try:
|
195 |
name, count, gains = fetch_analytics_data(client_id, token)
|
196 |
+
posts, org_name, sentiments = fetch_posts_and_stats(client_id, token, count=30)
|
197 |
+
engagement_data = compute_monthly_avg_engagement_rate(posts)
|
198 |
+
|
199 |
|
200 |
count_html = f"""
|
201 |
<div style='text-align:center; padding:20px; background:#e7f3ff; border:1px solid #bce8f1; border-radius:8px;'>
|
|
|
205 |
<p style='font-size:0.9em; color:#777;'>(As of latest data)</p>
|
206 |
</div>
|
207 |
"""
|
208 |
+
return gr.update(value=count_html, visible=True), gr.update(value=plot_follower_gains(gains), visible=True), gr.update(value=plot_growth_rate(gains, count), visible=True), gr.update(value=plot_avg_engagement_rate(engagement_data), visible=True)
|
209 |
|
210 |
except Exception as e:
|
211 |
error = display_error("Analytics load failed.", e).get('value', "<p style='color:red;'>Error loading data.</p>")
|