File size: 16,190 Bytes
7e61a73
 
4d2fd83
 
43e0c2d
9b96a20
b91e3df
7e61a73
4d2fd83
 
7e61a73
48407e5
97e32dc
bbf108c
8e299ad
 
 
 
7e61a73
4d2fd83
7e61a73
92fd5a6
 
 
 
 
4d2fd83
92fd5a6
4d2fd83
 
92fd5a6
 
 
4d2fd83
 
92fd5a6
 
4d2fd83
92fd5a6
4d2fd83
 
 
92fd5a6
 
4d2fd83
92fd5a6
4d2fd83
 
92fd5a6
 
4d2fd83
 
92fd5a6
 
fc9f8af
92fd5a6
 
4d2fd83
92fd5a6
4d2fd83
 
 
92fd5a6
 
 
4d2fd83
92fd5a6
 
 
4d2fd83
 
92fd5a6
4d2fd83
92fd5a6
 
4d2fd83
 
 
92fd5a6
4d2fd83
92fd5a6
4d2fd83
 
92fd5a6
4d2fd83
92fd5a6
4d2fd83
 
 
92fd5a6
 
4d2fd83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fbf909
4d2fd83
 
 
4fbf909
 
4d2fd83
 
4fbf909
4d2fd83
 
 
 
 
4fbf909
4d2fd83
4fbf909
4d2fd83
 
 
 
 
 
 
4fbf909
bbf108c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83111cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f6691f
83111cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c35c495
 
83111cb
c35c495
 
 
83111cb
c35c495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83111cb
c73b5ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97e32dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbf108c
4d2fd83
 
 
92fd5a6
4d2fd83
 
 
92fd5a6
 
4d2fd83
bbf108c
 
83111cb
c73b5ee
974e00e
97e32dc
 
c73b5ee
bbf108c
4d2fd83
 
 
 
 
 
 
92fd5a6
 
97e32dc
92fd5a6
 
4d2fd83
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import json
import requests
from datetime import datetime, timezone, timedelta
import matplotlib.pyplot as plt
import gradio as gr
import traceback
import html

from sessions import create_session
from error_handling import display_error

from Data_Fetching_and_Rendering import fetch_posts_and_stats
from mentions_dashboard import generate_mentions_dashboard

import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

API_V2_BASE = 'https://api.linkedin.com/v2'
API_REST_BASE = 'https://api.linkedin.com/rest'

def extract_follower_gains(data):
    elements = data.get("elements", [])
    if not elements:
        return []

    results = []
    for item in elements:
        start_timestamp = item.get("timeRange", {}).get("start")
        if not start_timestamp:
            continue

        try:
            date_str = datetime.fromtimestamp(start_timestamp / 1000, tz=timezone.utc).strftime('%Y-%m')
        except Exception:
            continue

        gains = item.get("followerGains", {})
        results.append({
            "date": date_str,
            "organic": gains.get("organicFollowerGain", 0) or 0,
            "paid": gains.get("paidFollowerGain", 0) or 0
        })

    return sorted(results, key=lambda x: x['date'])

def fetch_analytics_data(client_id, token):
    if not token:
        raise ValueError("comm_token is missing.")

    token_dict = token if isinstance(token, dict) else {'access_token': token, 'token_type': 'Bearer'}
    session = create_session(client_id, token=token_dict)

    try:
        org_urn, org_name = "urn:li:organization:19010008", "GRLS"

        count_url = f"{API_V2_BASE}/networkSizes/{org_urn}?edgeType=CompanyFollowedByMember"
        follower_count = session.get(count_url).json().get("firstDegreeSize", 0)

        start = datetime.now(timezone.utc) - timedelta(days=365)
        start = start.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
        start_ms = int(start.timestamp() * 1000)

        gains_url = (
            f"{API_REST_BASE}/organizationalEntityFollowerStatistics"
            f"?q=organizationalEntity&organizationalEntity={org_urn}"
            f"&timeIntervals.timeGranularityType=MONTH"
            f"&timeIntervals.timeRange.start={start_ms}"
        )
        gains_data = session.get(gains_url).json()
        gains = extract_follower_gains(gains_data)

        return org_name, follower_count, gains

    except requests.exceptions.RequestException as e:
        status = getattr(e.response, 'status_code', 'N/A')
        msg = f"Failed to fetch LinkedIn analytics (Status: {status})."
        raise ValueError(msg) from e
    except Exception as e:
        raise ValueError("Unexpected error during LinkedIn analytics fetch.") from e

def plot_follower_gains(data):
    plt.style.use('seaborn-v0_8-whitegrid')

    if not data:
        fig, ax = plt.subplots(figsize=(10, 5))
        ax.text(0.5, 0.5, 'No follower gains data.', ha='center', va='center', transform=ax.transAxes)
        ax.set_title('Monthly Follower Gains')
        ax.set_xticks([]); ax.set_yticks([])
        return fig

    dates = [d['date'] for d in data]
    organic = [d['organic'] for d in data]
    paid = [d['paid'] for d in data]

    fig, ax = plt.subplots(figsize=(12, 6))
    ax.plot(dates, organic, label='Organic', marker='o', color='#0073b1')
    ax.plot(dates, paid, label='Paid', marker='x', linestyle='--', color='#d9534f')
    ax.set(title='Monthly Follower Gains', xlabel='Month', ylabel='New Followers')
    ax.tick_params(axis='x', rotation=45)
    ax.legend()
    plt.tight_layout()
    return fig

def plot_growth_rate(data, total):
    if not data:
        fig, ax = plt.subplots(figsize=(10, 5))
        ax.text(0.5, 0.5, 'No data for growth rate.', ha='center', va='center', transform=ax.transAxes)
        ax.set_title('Growth Rate (%)')
        ax.set_xticks([]); ax.set_yticks([])
        return fig

    dates = [d['date'] for d in data]
    gains = [d['organic'] + d['paid'] for d in data]

    history = []
    current = total
    for g in reversed(gains):
        history.insert(0, current)
        current -= g

    rates = [((history[i] - history[i-1]) / history[i-1] * 100 if history[i-1] else 0) for i in range(1, len(history))]

    fig, ax = plt.subplots(figsize=(12, 6))
    ax.plot(dates[1:], rates, label='Growth Rate (%)', marker='o', color='green')
    ax.set(title='Monthly Growth Rate (%)', xlabel='Month', ylabel='Growth %')
    ax.tick_params(axis='x', rotation=45)
    ax.legend()
    plt.tight_layout()
    return fig

def compute_monthly_avg_engagement_rate(posts):
    from collections import defaultdict
    import statistics

    if not posts:
        return []

    monthly_data = defaultdict(lambda: {"engagement_sum": 0, "post_count": 0, "impression_total": 0})

    for post in posts:
        try:
            month = post["when"][:7]  # Format: YYYY-MM
            likes = post.get("likes", 0)
            comments = post.get("comments", 0)
            shares = post.get("shares", 0)
            clicks = post.get("clicks", 0)
            impressions = post.get("impressions", 0)

            engagement = likes + comments + shares + clicks
            monthly_data[month]["engagement_sum"] += engagement
            monthly_data[month]["post_count"] += 1
            monthly_data[month]["impression_total"] += impressions
        except Exception:
            continue

    results = []
    for month in sorted(monthly_data.keys()):
        data = monthly_data[month]
        if data["post_count"] == 0 or data["impression_total"] == 0:
            rate = 0
        else:
            avg_impressions = data["impression_total"] / data["post_count"]
            rate = (data["engagement_sum"] / (data["post_count"] * avg_impressions)) * 100
        results.append({"month": month, "engagement_rate": round(rate, 2)})

    return results

def plot_avg_engagement_rate(data):
    import matplotlib.pyplot as plt

    if not data:
        fig, ax = plt.subplots(figsize=(10, 5))
        ax.text(0.5, 0.5, 'No engagement data.', ha='center', va='center', transform=ax.transAxes)
        ax.set_title('Average Post Engagement Rate (%)')
        ax.set_xticks([]); ax.set_yticks([])
        return fig

    months = [d["month"] for d in data]
    rates = [d["engagement_rate"] for d in data]

    fig, ax = plt.subplots(figsize=(12, 6))
    ax.plot(months, rates, label="Engagement Rate (%)", marker="s", color="#ff7f0e")
    ax.set(title="Average Post Engagement Rate (%)", xlabel="Month", ylabel="Engagement Rate %")
    ax.tick_params(axis='x', rotation=45)
    ax.legend()
    plt.tight_layout()
    return fig

def compute_post_interaction_metrics(posts):
    from collections import defaultdict

    if not posts:
        return []

    monthly_stats = defaultdict(lambda: {
        "comments": 0,
        "shares": 0,
        "clicks": 0,
        "likes": 0,
        "posts": 0
    })

    for post in posts:
        try:
            month = post["when"][:7]  # YYYY-MM
            monthly_stats[month]["comments"] += post.get("comments", 0)
            monthly_stats[month]["shares"] += post.get("shares", 0)
            monthly_stats[month]["clicks"] += post.get("clicks", 0)
            monthly_stats[month]["likes"] += post.get("likes", 0)
            monthly_stats[month]["posts"] += 1
        except Exception:
            continue

    results = []
    for month in sorted(monthly_stats.keys()):
        stats = monthly_stats[month]
        total_engagement = stats["comments"] + stats["shares"] + stats["clicks"] + stats["likes"]
        posts_count = stats["posts"] or 1  # Avoid division by zero

        results.append({
            "month": month,
            "comments_per_post": round(stats["comments"] / posts_count, 2),
            "shares_per_post": round(stats["shares"] / posts_count, 2),
            "clicks_per_post": round(stats["clicks"] / posts_count, 2),
            "comment_share_of_engagement": round((stats["comments"] / total_engagement) * 100 if total_engagement else 0, 2)
        })
        logging.info(f"this are the inter<ction results {results}")
    return results

def plot_interaction_metrics(data):
    if not data:
        fig, ax = plt.subplots(figsize=(10, 5))
        ax.text(0.5, 0.5, 'No interaction data.', ha='center', va='center', transform=ax.transAxes)
        ax.set_title('Post Interaction Metrics')
        ax.set_xticks([]); ax.set_yticks([])
        return fig

    months = [d["month"] for d in data]
    comments_pp = [d["comments_per_post"] for d in data]
    shares_pp = [d["shares_per_post"] for d in data]
    clicks_pp = [d["clicks_per_post"] for d in data]
    comment_share = [d["comment_share_of_engagement"] for d in data]

    fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(12, 10), sharex=True)
    fig.suptitle("Post Interaction Metrics", fontsize=16)

    axes[0].plot(months, comments_pp, marker="o", color="#1f77b4")
    axes[0].set_ylabel("Comments/Post")
    axes[0].grid(True)

    axes[1].plot(months, shares_pp, marker="s", color="#ff7f0e")
    axes[1].set_ylabel("Shares/Post")
    axes[1].grid(True)

    axes[2].plot(months, clicks_pp, marker="^", color="#2ca02c")
    axes[2].set_ylabel("Clicks/Post")
    axes[2].grid(True)

    axes[3].plot(months, comment_share, marker="x", linestyle="--", color="#d62728")
    axes[3].set_ylabel("Comment Share (%)")
    axes[3].set_xlabel("Month")
    axes[3].grid(True)

    plt.xticks(rotation=45)
    plt.tight_layout(rect=[0, 0, 1, 0.96])  # Leave space for suptitle
    return fig

from collections import defaultdict
import matplotlib.pyplot as plt

def compute_eb_content_ratio(posts):
    if not posts:
        return []

    monthly_counts = defaultdict(lambda: {"eb_count": 0, "total": 0})

    for post in posts:
        try:
            month = post["when"][:7]  # YYYY-MM
            category = post.get("category", "None")
            monthly_counts[month]["total"] += 1
            if category and category.strip() != "None":
                monthly_counts[month]["eb_count"] += 1
        except Exception:
            continue

    results = []
    for month in sorted(monthly_counts.keys()):
        data = monthly_counts[month]
        ratio = (data["eb_count"] / data["total"]) * 100 if data["total"] else 0
        results.append({"month": month, "eb_ratio": round(ratio, 2)})

    return results

def plot_eb_content_ratio(data):
    if not data:
        fig, ax = plt.subplots(figsize=(10, 5))
        ax.text(0.5, 0.5, 'No EB content data.', ha='center', va='center', transform=ax.transAxes)
        ax.set_title('EB Content Ratio (%)')
        ax.set_xticks([]); ax.set_yticks([])
        return fig

    months = [d["month"] for d in data]
    ratios = [d["eb_ratio"] for d in data]

    fig, ax = plt.subplots(figsize=(12, 6))
    ax.plot(months, ratios, label="EB Content Ratio (%)", marker="o", color="#2ca02c")
    ax.set(title="Monthly EB Content Ratio (%)", xlabel="Month", ylabel="EB Content %")
    ax.tick_params(axis='x', rotation=45)
    ax.legend()
    plt.tight_layout()
    return fig

def compute_mention_metrics(mention_data):
    if not mention_data:
        return [], []

    monthly_stats = defaultdict(lambda: {"positive": 0, "negative": 0, "neutral": 0, "total": 0})

    for m in mention_data:
        month = m["date"].strftime("%Y-%m")
        sentiment = m["sentiment"]
        monthly_stats[month]["total"] += 1
        if "Positive" in sentiment:
            monthly_stats[month]["positive"] += 1
        elif "Negative" in sentiment:
            monthly_stats[month]["negative"] += 1
        elif "Neutral" in sentiment:
            monthly_stats[month]["neutral"] += 1

    volume_data = []
    sentiment_data = []
    sorted_months = sorted(monthly_stats.keys())

    for i, month in enumerate(sorted_months):
        stats = monthly_stats[month]
        positive = stats["positive"]
        negative = stats["negative"]
        total = stats["total"]

        sentiment_score = ((positive / total) * 100 - (negative / total) * 100) if total else 0
        sentiment_ratio = (positive / negative) if negative else float('inf')

        sentiment_data.append({
            "month": month,
            "score": round(sentiment_score, 2),
            "ratio": round(sentiment_ratio, 2) if sentiment_ratio != float('inf') else None
        })

        prev_total = monthly_stats[sorted_months[i - 1]]["total"] if i > 0 else 0
        change = (((total - prev_total) / prev_total) * 100) if prev_total else None
        volume_data.append({"month": month, "count": total, "change": round(change, 2) if change is not None else None})

    return volume_data, sentiment_data

def plot_mention_volume_trend(volume_data):
    fig, ax = plt.subplots(figsize=(12, 6))
    if not volume_data:
        ax.text(0.5, 0.5, 'No Mention Volume Data.', ha='center', va='center', transform=ax.transAxes)
        ax.set_title('Mention Volume Over Time')
        return fig

    months = [d["month"] for d in volume_data]
    counts = [d["count"] for d in volume_data]
    ax.plot(months, counts, marker='o', linestyle='-', color="#1f77b4")
    ax.set(title="Monthly Mention Volume", xlabel="Month", ylabel="Mentions")
    ax.tick_params(axis='x', rotation=45)
    plt.tight_layout()
    return fig

def plot_mention_sentiment_score(sentiment_data):
    fig, ax = plt.subplots(figsize=(12, 6))
    if not sentiment_data:
        ax.text(0.5, 0.5, 'No Sentiment Score Data.', ha='center', va='center', transform=ax.transAxes)
        ax.set_title('Mention Sentiment Score')
        return fig

    months = [d["month"] for d in sentiment_data]
    scores = [d["score"] for d in sentiment_data]
    ax.plot(months, scores, marker='o', linestyle='-', color="#ff7f0e")
    ax.set(title="Monthly Sentiment Score (% Positive - % Negative)", xlabel="Month", ylabel="Score")
    ax.axhline(0, color='gray', linestyle='--', linewidth=1)
    ax.tick_params(axis='x', rotation=45)
    plt.tight_layout()
    return fig


def fetch_and_render_analytics(client_id, token):
    loading = gr.update(value="<p>Loading follower count...</p>", visible=True)
    hidden = gr.update(value=None, visible=False)

    if not token:
        error = "<p style='color:red;'>❌ Missing token. Please log in.</p>"
        return gr.update(value=error, visible=True), hidden, hidden

    try:
        name, count, gains = fetch_analytics_data(client_id, token)
        posts, org_name, sentiments = fetch_posts_and_stats(client_id, token, count=30)
        engagement_data = compute_monthly_avg_engagement_rate(posts)
        interaction_data = compute_post_interaction_metrics(posts)
        eb_data = compute_eb_content_ratio(posts)
        html_mentions, fig, mention_data = generate_mentions_dashboard(client_id, token)
        volume_data, sentiment_data = compute_mention_metrics(mention_data)


 

        count_html = f"""
        <div style='text-align:center; padding:20px; background:#e7f3ff; border:1px solid #bce8f1; border-radius:8px;'>
            <p style='font-size:1.1em; color:#31708f;'>Total Followers for</p>
            <p style='font-size:1.4em; font-weight:bold; color:#005a9e;'>{html.escape(name)}</p>
            <p style='font-size:2.8em; font-weight:bold; color:#0073b1;'>{count:,}</p>
            <p style='font-size:0.9em; color:#777;'>(As of latest data)</p>
        </div>
        """
        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), gr.update(value=plot_interaction_metrics(interaction_data), visible=True), gr.update(value=plot_eb_content_ratio(eb_data), visible=True), gr.update(value=plot_mention_volume_trend(volume_data), visible=True), gr.update(value=plot_mention_sentiment_score(sentiment_data), visible=True)

    except Exception as e:
        error = display_error("Analytics load failed.", e).get('value', "<p style='color:red;'>Error loading data.</p>")
        return gr.update(value=error, visible=True), hidden, hidden