Carbon Intelligence™
Meta Ads Export Guide — Python Script
GMSF v1.2
Meta Ads
10 min install
Objective
This Python script exports Meta Ads (Facebook, Instagram, Audience Network, Messenger) campaign data with GMSF v1.2 granularity for Carbon Intelligence™. Meta offers unique granularity on placements (Feed, Stories, Reels) and actual impression devices, essential for precise carbon calculation.
Installation
3 steps · 10 minutes
Prerequisites
- Python 3.8+ installed on your machine
- Install dependencies:
pip install requests pandas - Create a Meta App on developers.facebook.com
- Obtain a long-lived Access Token with
ads_readandads_managementpermissions - Note your Ad Account ID (format:
act_XXXXXXXXX)
Configure the script
- Open the file:
carbon-intelligence-meta-ads-export.py - Fill in the variables:
ACCESS_TOKEN,AD_ACCOUNT_ID - Configure
OUTPUT_DIR(CSV destination folder) - Optional: Set environment variables
CI_META_ACCESS_TOKENandCI_META_AD_ACCOUNT_IDinstead of hardcoding
Run
- Run the script:
python carbon-intelligence-meta-ads-export.py - CSV files are generated in
OUTPUT_DIR - Schedule execution via
cron(weekly recommended)
What is exported
10 tabs created automatically
| Tab | Content | Usage Carbon Intelligence |
|---|---|---|
| CI_Campaigns | By campaign + week: impressions, reach, frequency, spend, ROAS | Campaign overview |
| CI_Device | By device (mobile, desktop, tablet) | Energy factor per device |
| CI_Geo | By country | GMSF v1.2 country energy mix |
| CI_Placements | By platform x position: Feed, Stories, Reels, Right Column | 🎯 Meta Specific — Feed/Stories/Reels granularity |
| CI_CreativeSize | By ad with creative dimensions | Data transfer weight |
| CI_AdFormat | By publisher_platform | Ad format factor |
| CI_Video | Video quartiles (p25, p50, p75, p100), average duration | Video carbon calculation |
| CI_ImpDevice | By real device: iPhone, iPad, Android, Desktop | 🔴 Carbon Critical — actual device used |
| CI_Metadata | Account and export info (timestamp, API version) | Traceability and audit |
| CI_Export_CSV | All dimensions consolidated flat | Direct CI platform import |
Export CSV
Two options available
The CI_Export_CSV tab contains all dimensions flattened, ready for direct import into the Carbon Intelligence™ platform. CSV files are automatically generated in
OUTPUT_DIR with each script execution.Sharing with Carbon Intelligence™
Advanced Configuration
Script CONFIG section
# In the script, CONFIG section:
LOOKBACK_DAYS: 90, # Days of history (default: 90)
MIN_IMPRESSIONS: 1, # Minimum impression threshold per row
API_VERSION: 'v19.0' # Meta API version (updated regularly)
LOOKBACK_DAYS: 90, # Days of history (default: 90)
MIN_IMPRESSIONS: 1, # Minimum impression threshold per row
API_VERSION: 'v19.0' # Meta API version (updated regularly)
Troubleshooting
OAuthException
Access Token expired. Generate a new long-lived token on developers.facebook.com with the required permissions.
"Error validating access token"
Check that the Access Token has
ads_read and ads_management permissions on your Meta App."Ad Account not found"
Check the format of your Ad Account ID:
act_XXXXXXXXX (with the act_ prefix)."Rate limit exceeded"
The script automatically handles exponential retries. If persistent, reduce
LOOKBACK_DAYS to 30 or 60 days.Empty video data
Normal if no video campaign is active for the period. The CI_Video tab only fills when there are video impressions.
Support
Email
support@carbonintelligence.green
Website
carbonintelligence.green
Script version
1.0.0
Methodology
GMSF v1.2 (Ad Net Zero)