{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "FB_Ads_Review.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python2",
      "display_name": "Python 2"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/filtr/marc-jupyter-notebooks/blob/google-colab/FB_Ads_Review.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ReHYncvRy1Lg",
        "colab_type": "text"
      },
      "source": [
        "# 1. Run Initial Ad Insights Query for User"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qkvX8JFUIX0E",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import json\n",
        "import urllib2\n",
        "url = \"https://graph.facebook.com/v4.0/act_512042582544198/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&breakdowns=impression_device&access_token=\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DcDTMblvzCLu",
        "colab_type": "text"
      },
      "source": [
        "# 2. Save Response to a Python Dataframe"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7Onx_NE4teuV",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import pandas as pd\n",
        "next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GcyE7zlGrwMS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "df=pd.DataFrame.from_dict(values['data'])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cT8Z9hbtzJw3",
        "colab_type": "text"
      },
      "source": [
        "# 3. Cycle through next tokens and append each response to the Dataframe"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xmRbjW6uIjlj",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for x in range(10000):\n",
        "  response = urllib2.urlopen(next)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  df=pd.concat([df,pd.DataFrame.from_dict(values['data'])])\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JC2s1PgAzWd5",
        "colab_type": "text"
      },
      "source": [
        "# 4. Analyze Campaign Effectiveness"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DjB1vpUAJavU",
        "colab_type": "code",
        "outputId": "5afc56c7-f9a5-46f9-df35-fc5b50f8b284",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 187
        }
      },
      "source": [
        "df.count()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "campaign_name        300\n",
              "cpm                  300\n",
              "date_start           300\n",
              "date_stop            300\n",
              "impression_device    300\n",
              "impressions          300\n",
              "objective            300\n",
              "reach                300\n",
              "spend                300\n",
              "dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 83
        }
      ]
    }
  ]
}