Building an Instagram Engagement Rate Calculator in Python

Author : marken owens | Published On : 07 Aug 2026

Building an Instagram Engagement Rate Calculator in Python

Follower count is a vanity metric. Everyone building anything serious in influencer marketing or social analytics learns this within about a week. Two accounts can both have 100k followers, and one gets 200 likes per post while the other gets 8,000. If you're evaluating creators, campaigns, or your own content strategy, engagement rate is the number that actually tells you something.

The formula (and why the "simple" version is wrong)

The textbook engagement rate formula is:

engagement_rate = (likes + comments) / followers * 100

This is fine as a first pass, but it has a well-known flaw: it doesn't account for reach or post frequency, and it treats all accounts as if they post at the same cadence with the same audience overlap. A more useful version averages engagement across a rolling window of recent posts rather than a single post, which smooths out one-off viral spikes or dead posts.

def engagement_rate(posts, follower_count):
    if not posts or follower_count == 0:
        return 0
    total_engagement = sum(p["likes"] + p["comments"] for p in posts)
    avg_engagement = total_engagement / len(posts)
    return round((avg_engagement / follower_count) * 100, 2)

Getting the underlying data

The formula is trivial; the hard part is reliably getting post-level data — likes, comments, and follower counts — for accounts you don't control. Instagram doesn't make this easy through official channels unless you own the account or have business API access, which is a non-starter if you're evaluating creators you have no relationship with yet.

For public data, I've used EnsembleData's Instagram endpoints for this exact use case — pulling a user's recent posts along with engagement fields in one call rather than scraping pages manually. Their documentation lays out the post and profile endpoints with the response schema, which saves you the trial-and-error of guessing field names.

import requests

def get_recent_posts(username, token, count=12):
    url = "https://ensembledata.com/apis/instagram/user/posts"
    params = {"username": username, "depth": 1, "token": token}
    res = requests.get(url, params=params)
    return res.json()["data"]["posts"][:count]

Turning it into a comparison tool

Once you can fetch posts and compute a rate for one account, extending it to compare a shortlist of creators is just a loop and a sorted list. This is genuinely how a lot of influencer vetting tools work under the hood — there's no secret sauce, just consistent data collection and a formula applied uniformly across candidates so the numbers are actually comparable.

A couple of practical notes if you build this yourself: filter out sponsored posts if you can identify them (they often skew engagement lower), and be cautious with accounts that have unusually low post counts — a 20% engagement rate on three posts isn't statistically meaningful in the same way it is on fifty.