Skip to content

Instantly share code, notes, and snippets.

@pauladam
Created December 4, 2015 01:35
Show Gist options
  • Save pauladam/ab5948f653499da95aa1 to your computer and use it in GitHub Desktop.
Save pauladam/ab5948f653499da95aa1 to your computer and use it in GitHub Desktop.
Basic KPI calcs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"api https://graph.facebook.com/v2.5/%s/insights\n",
"docs https://developers.facebook.com/docs/marketing-api/reference/adgroup/insights/\n",
"adgroup 6037771945864\n",
"spend 1432.4, impressions 383685, unique impressions 271574\n",
"CPM 3.73327078202, uCPM 5.27443716998\n"
]
}
],
"source": [
"import requests\n",
"\n",
"api = 'https://graph.facebook.com/v2.5/%s/insights'\n",
"api_doc = 'https://developers.facebook.com/docs/marketing-api/reference/adgroup/insights/'\n",
"access_token = file('token').read()\n",
"adgroups = ['6011518651144']\n",
"\n",
"resp = requests.get(api % adgroup, params={'access_token': access_token})\n",
"\n",
"spend = resp.json()['data'][0]['spend']\n",
"imps = int(resp.json()['data'][0]['impressions'])\n",
"unique_imps = resp.json()['data'][0]['unique_impressions']\n",
"\n",
"uCPM = (spend * 1000) / unique_imps\n",
"CPM = (spend * 1000) / imps\n",
"\n",
"print 'api {}'.format(api)\n",
"print 'docs {}'.format(api_doc)\n",
"print 'adgroup', adgroup_id\n",
"print 'spend {}, impressions {}, unique impressions {}'.format(*[spend, imps, unique_imps])\n",
"print 'CPM {}, uCPM {}'.format(CPM, uCPM)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment