An end-to-end pipeline that pulls Instagram data via the Meta Graph API, analyzes it with Google Gemini (free tier), and generates ready-to-publish content ideas β all automated in Python. No paid API required.
This proof of concept demonstrates how Generative AI can turn raw Instagram data into actionable marketing intelligence. It runs four steps automatically:
- Fetches posts, engagement metrics, and comments from the Instagram Graph API
- Analyzes audience sentiment and emotional themes across all comments
- Identifies what makes your top-performing posts work (vs. the ones that flop)
- Generates 10 ready-to-publish post ideas in Spanish, grounded in real data
No dashboards, no manual tagging β just run the script and get results.
sm-analytics-vert/
β
βββ instagram_poc_free.py # Main pipeline script (Gemini)
βββ .env # Your API keys (never commit this!)
βββ .env.example # Template for .env
βββ requirements.txt # Python dependencies
βββ README.md # This file
β
βββ output/ # Auto-generated on first run
βββ posts_metrics.csv # All posts with engagement rates
βββ content_ideas.csv # 10 AI-generated post ideas
βββ full_report.json # Complete analysis report
- Python 3.9+
- An Instagram Business or Creator account connected to a Facebook Page
- A Google Gemini API key (free, no credit card needed)
- A Meta Developer App with Instagram Graph API access
git clone https://github.com/mseglar/sm-analytics-vert.git
cd sm-analytics-vertpip install google-generativeai requests python-dotenvCopy the example and fill in your keys:
cp .env.example .envYour .env should look like this:
GEMINI_API_KEY=AIzaSyxxxxxxxxxxxxxxxxxx
INSTAGRAM_ACCESS_TOKEN=EAAxxxxxxxxxxxxxxxxxx
INSTAGRAM_BUSINESS_ACCOUNT_ID=17841xxxxxxxxxx
β οΈ Never commit your.envfile. Add it to.gitignore.
python instagram_poc_free.pyResults will appear in the /output folder.
- Go to aistudio.google.com/app/apikey
- Sign in with any Google account
- Click Create API Key β done, no credit card required
- Copy the key and paste it into
.envasGEMINI_API_KEY
Free tier limits: 1,500 requests/day and 1M tokens/minute β this PoC uses roughly 15β20 requests per full run, so you're well within limits.
This is the most involved part. Follow these steps carefully.
- Go to developers.facebook.com
- Click My Apps β Create App
- Choose Business as the app type
- Give it a name (e.g.
instagram-poc) and click Create
- Inside your app dashboard, click Add Product
- Find Instagram Graph API and click Set Up
- Go to Instagram Graph API β Getting Started
- Connect your Facebook Page (which must be linked to your Instagram Business account)
- Grant the following permissions:
instagram_basicinstagram_manage_insightspages_read_engagement
- Open the Graph API Explorer
- Select your app from the dropdown (top right)
- Click Generate Access Token
- Select the permissions listed above
- Copy the token β this is a short-lived token (valid ~1 hour)
Run this in your terminal, replacing the placeholders:
curl -i -X GET "https://graph.facebook.com/v19.0/oauth/access_token
?grant_type=fb_exchange_token
&client_id=YOUR_APP_ID
&client_secret=YOUR_APP_SECRET
&fb_exchange_token=YOUR_SHORT_LIVED_TOKEN"You'll get back a long-lived token. Paste it into .env as INSTAGRAM_ACCESS_TOKEN.
Your App ID and App Secret are found in your Meta App Dashboard under Settings β Basic.
Run this in your terminal (replace the token):
curl -i -X GET "https://graph.facebook.com/v19.0/me/accounts?access_token=YOUR_LONG_LIVED_TOKEN"This returns your Facebook Pages. Then use the page ID to get the Instagram account ID:
curl -i -X GET "https://graph.facebook.com/v19.0/PAGE_ID?fields=instagram_business_account&access_token=YOUR_LONG_LIVED_TOKEN"Copy the id value from instagram_business_account and paste it into .env as INSTAGRAM_BUSINESS_ACCOUNT_ID.
| File | What's inside |
|---|---|
posts_metrics.csv |
Every post with likes, reach, saves, comments count, and calculated engagement rate |
content_ideas.csv |
10 post ideas with format, caption (in Spanish), visual brief, and rationale |
full_report.json |
Full structured JSON: sentiment breakdown, top themes, unanswered audience questions, performance patterns, and all content ideas |
You can adjust these constants at the top of instagram_poc_free.py:
| Variable | Default | Description |
|---|---|---|
MAX_POSTS |
50 |
How many recent posts to fetch |
MAX_COMMENTS |
30 |
Max comments to fetch per post |
GEMINI_MODEL |
gemini-1.5-flash |
Gemini model to use |
============================================================
Instagram Intelligence PoC
Run date: 2026-05-06 10:32
============================================================
π₯ Fetching posts from Instagram Graph API...
β Fetched 50 posts
π¬ Fetching comments for post 1/50...
...
π€ Analyzing sentiment & themes with Claude...
β Sentiment & theme analysis complete
π Analyzing performance patterns with Claude...
β Performance pattern analysis complete
β¨ Generating content ideas with Claude...
β Generated 10 content ideas
πΎ Exporting results...
π Posts CSV saved β output/posts_metrics.csv
π Content ideas CSV saved β output/content_ideas.csv
π Full report saved β output/full_report.json
============================================================
SUMMARY
============================================================
π£ Sentiment: 87% positive | 10% neutral | 3% negative
Followers respond most emotionally to fear-to-achievement posts and scenic nature content.
π Top performing media type: REEL β 3.2x higher engagement than images
π Top themes in comments:
β’ Overcoming fear (high) β inspired
β’ Community & belonging (high) β grateful
β’ Curiosity about routes (medium) β curious
π‘ Top recommendation:
β Post more Reels showing the emotional journey of a climb, not just the summit.
β
Done! Check the /output folder for all files.
============================================================
- The Meta long-lived token expires after 60 days. For a production version, implement the token refresh flow via the API.
- Instagram Graph API only returns data for your own account's posts β not competitors.
- Comments are capped at
MAX_COMMENTSper post to stay within API rate limits. - This PoC does not store historical data between runs β each run is independent.
- Schedule the script to run weekly with
cronor GitHub Actions - Store results in a database (SQLite or PostgreSQL) to track trends over time
- Build a simple dashboard with Streamlit or Looker Studio
- Add competitor analysis via third-party tools (e.g. Apify)
- Automate posting of approved content ideas via the Instagram Graph API
| Tool | Purpose |
|---|---|
| Python 3.9+ | Core language |
| Meta Graph API v19 | Instagram data source |
| Google Gemini API | AI analysis & content generation |
requests |
HTTP calls to Meta API |
python-dotenv |
Environment variable management |
This project is a proof of concept for internal use. Not for redistribution.