Comment on page
Follow content engagements weekly
Tags: #linkedin #html #plotly #csv #image #content #analytics #dependency
Last update: 2023-05-29 (Created: 2022-06-30)
Description: This notebook helps you track and analyze your weekly content engagements on LinkedIn. To run this notebook, you must have already run LinkedIn_Get_profile_posts_stats.ipynb or LinkedIn_Get_company_posts_stats.ipynb to get your post stats in CSV.
Disclaimer:
This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by Linkedin or any of its affiliates or subsidiaries. It uses an independent and unofficial API. Use at your own risk.
This project violates Linkedin's User Agreement Section 8.2, and because of this, Linkedin may (and will) temporarily or permanently ban your account. We are not responsible for your account being banned.
import naas
import pandas as pd
from datetime import datetime
import plotly.graph_objects as go
# Input
csv_input = f"LINKEDIN_PROFILE_POSTS.csv" # CSV path with your posts stats generated with 'LinkedIn_Get_profile_posts_stats.ipynb' or 'LinkedIn_Get_company_posts_stats.ipynb'
TITLE = "Engagements" # Chart title
COL_VALUE = ["LIKES", "COMMENTS"] # Column to sum
# Outputs
name_output = "LINKEDIN_FOLLOW_CONTENT_ENGAGEMENTS_WEEKLY"
csv_output = f"{name_output}.csv"
html_output = f"{name_output}.html"
image_output = f"{name_output}.png"
naas.dependency.add()
# -> Uncomment the line below to remove your dependency
# naas.dependency.delete()
Get posts feed from CSV stored in your local (Returns empty if CSV does not exist)
def read_csv(file_path):
try:
df = pd.read_csv(file_path)
except FileNotFoundError as e:
# Empty dataframe returned
return pd.DataFrame()
return df
df_posts = read_csv(csv_input)
print("✅ Posts fetched:", len(df_posts))
df_posts.head(1)
DATE_FORMAT = "%Y-%m-%d"
PERIOD = "%Y%U"
PERIOD_D = "%Y-W%U"
PERIOD_TEXT = "This week"
def get_trend(df_init, col_date, col_value, agg_value, period_rolling=12):
# Init variable
df = df_init.copy()
# Groupby period
if isinstance(col_value, list):
df["VALUE"] = 0
for c in col_value:
df[c] = df[c].astype(float)
df["VALUE"] = df["VALUE"] + df[c]
col_value = "VALUE"
elif agg_value == "sum":
df[col_value] = df[col_value].astype(float)
df[col_date] = pd.to_datetime(df[col_date].str[:-6]).dt.strftime(DATE_FORMAT)
df = df.groupby(col_date, as_index=False).agg({col_value: agg_value})