{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "FB_Ads.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.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "096QxlK4ycS0",
        "colab_type": "text"
      },
      "source": [
        "# 1. Run Initial Ad Insights Query for User"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qkvX8JFUIX0E",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Error Count 138+1+1\n",
        "#Call Count 891+142\n",
        "import time\n",
        "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&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-K-kopNAykg7",
        "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": "code",
      "metadata": {
        "id": "YgQAUV1EbuVI",
        "colab_type": "code",
        "outputId": "0a5bdd93-ad9f-449c-b6ce-7e5f1bccacfa",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "source": [
        "df.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>campaign_name</th>\n",
              "      <th>cpm</th>\n",
              "      <th>date_start</th>\n",
              "      <th>date_stop</th>\n",
              "      <th>device_platform</th>\n",
              "      <th>impression_device</th>\n",
              "      <th>impressions</th>\n",
              "      <th>objective</th>\n",
              "      <th>platform_position</th>\n",
              "      <th>publisher_platform</th>\n",
              "      <th>reach</th>\n",
              "      <th>spend</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>Khalid - AU (Free Spirit)</td>\n",
              "      <td>1.954277</td>\n",
              "      <td>2019-07-15</td>\n",
              "      <td>2019-08-13</td>\n",
              "      <td>mobile_app</td>\n",
              "      <td>android_smartphone</td>\n",
              "      <td>4068</td>\n",
              "      <td>LINK_CLICKS</td>\n",
              "      <td>instagram_stories</td>\n",
              "      <td>instagram</td>\n",
              "      <td>3960</td>\n",
              "      <td>7.95</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>Khalid - AU (Free Spirit)</td>\n",
              "      <td>0</td>\n",
              "      <td>2019-07-15</td>\n",
              "      <td>2019-08-13</td>\n",
              "      <td>mobile_app</td>\n",
              "      <td>android_tablet</td>\n",
              "      <td>32</td>\n",
              "      <td>LINK_CLICKS</td>\n",
              "      <td>instagram_stories</td>\n",
              "      <td>instagram</td>\n",
              "      <td>16</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>Khalid - AU (Free Spirit)</td>\n",
              "      <td>2.322275</td>\n",
              "      <td>2019-07-15</td>\n",
              "      <td>2019-08-13</td>\n",
              "      <td>mobile_app</td>\n",
              "      <td>ipad</td>\n",
              "      <td>211</td>\n",
              "      <td>LINK_CLICKS</td>\n",
              "      <td>instagram_stories</td>\n",
              "      <td>instagram</td>\n",
              "      <td>168</td>\n",
              "      <td>0.49</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>Khalid - AU (Free Spirit)</td>\n",
              "      <td>1.507301</td>\n",
              "      <td>2019-07-15</td>\n",
              "      <td>2019-08-13</td>\n",
              "      <td>mobile_app</td>\n",
              "      <td>iphone</td>\n",
              "      <td>31573</td>\n",
              "      <td>LINK_CLICKS</td>\n",
              "      <td>instagram_stories</td>\n",
              "      <td>instagram</td>\n",
              "      <td>29576</td>\n",
              "      <td>47.59</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>Khalid - AU (Free Spirit)</td>\n",
              "      <td>0</td>\n",
              "      <td>2019-07-15</td>\n",
              "      <td>2019-08-13</td>\n",
              "      <td>mobile_app</td>\n",
              "      <td>ipod</td>\n",
              "      <td>35</td>\n",
              "      <td>LINK_CLICKS</td>\n",
              "      <td>instagram_stories</td>\n",
              "      <td>instagram</td>\n",
              "      <td>35</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "               campaign_name       cpm  ...  reach  spend\n",
              "0  Khalid - AU (Free Spirit)  1.954277  ...   3960   7.95\n",
              "1  Khalid - AU (Free Spirit)         0  ...     16      0\n",
              "2  Khalid - AU (Free Spirit)  2.322275  ...    168   0.49\n",
              "3  Khalid - AU (Free Spirit)  1.507301  ...  29576  47.59\n",
              "4  Khalid - AU (Free Spirit)         0  ...     35      0\n",
              "\n",
              "[5 rows x 12 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DpUS7fzXIfnX",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import time\n",
        "import json\n",
        "import urllib2\n",
        "import pandas as pd\n",
        "url = \"https://graph.facebook.com/v4.0/act_10156308372288586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)\n",
        "next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "df_interim=pd.DataFrame.from_dict(values['data'])\n",
        "df_interim['account']='Myles'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ocTAADZlInkd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "url = \"https://graph.facebook.com/v4.0/act_512042582544198/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)\n",
        "next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "df=pd.DataFrame.from_dict(values['data'])\n",
        "df['account']='Jenn'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Yn6JJ_XgIyAC",
        "colab_type": "code",
        "outputId": "50233e19-f7fb-4b4e-d410-738b161824a8",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 511
        }
      },
      "source": [
        "url = \"https://graph.facebook.com/v4.0/act_10156162965163586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)\n",
        "next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "df=pd.DataFrame.from_dict(values['data'])\n",
        "df['account']='Sam'"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "error",
          "ename": "KeyError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m\u001b[0m",
            "\u001b[0;31mKeyError\u001b[0mTraceback (most recent call last)",
            "\u001b[0;32m<ipython-input-5-cca734dd1741>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mvalues\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_dict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'paging'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'next'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'after'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'utf-8'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_dict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'data'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'account'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'Sam'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/pandas/core/frame.pyc\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m   2925\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnlevels\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2926\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_multilevel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2927\u001b[0;31m             \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2928\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mis_integer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2929\u001b[0m                 \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/base.pyc\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m   2657\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2658\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2659\u001b[0;31m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_maybe_cast_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2660\u001b[0m         \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtolerance\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2661\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;31mKeyError\u001b[0m: 'next'"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sE_GxapSJEGU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "url = \"https://graph.facebook.com/v4.0/act_1376605895888910/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)\n",
        "#next=pd.DataFrame.from_dict(values['paging'])['next']['before'].encode('utf-8')\n",
        "df=pd.DataFrame.from_dict(values['data'])\n",
        "df['account']='Joe'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FUfgxvWhJdFW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "url = \"https://graph.facebook.com/v4.0/act_2123931584507408/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)\n",
        "#next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "df=pd.DataFrame.from_dict(values['data'])\n",
        "df['account']='Joe 2'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jKWTL9IzJ8_7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "url = \"https://graph.facebook.com/v4.0/act_10156352789338586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)\n",
        "next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "df=pd.DataFrame.from_dict(values['data'])\n",
        "df['account']='Devin 3'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ptbHqWmaKYJK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "url = \"https://graph.facebook.com/v4.0/act_10154205820958586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)\n",
        "#next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "df=pd.DataFrame.from_dict(values['data'])\n",
        "df['account']='Devin 2'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "z1e_SE_8Kjuc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "url = \"https://graph.facebook.com/v4.0/act_108170885940775/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)\n",
        "#next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "df=pd.DataFrame.from_dict(values['data'])\n",
        "df['account']='Epic Retarget'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UenedxWAJRcY",
        "colab_type": "code",
        "outputId": "0f88378f-1804-4101-9295-16425028c5fe",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 187
        }
      },
      "source": [
        "df_interim.count()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "campaign_name    25\n",
              "cpm              25\n",
              "date_start       25\n",
              "date_stop        25\n",
              "impressions      25\n",
              "objective        25\n",
              "reach            25\n",
              "spend            25\n",
              "account          25\n",
              "dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "w6J76czrv5Fb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jzamGUH6yzha",
        "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": [
        "#Myles\n",
        "import time\n",
        "import json\n",
        "import urllib2\n",
        "import pandas as pd\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_10156308372288586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df_interim=pd.DataFrame.from_dict(values['data'])\n",
        "  df_interim['account']='Myles'\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Myles'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_512042582544198/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Jenn'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Jenn'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_10156162965163586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Sam'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Sam'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_1376605895888910/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Joe'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Joe'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_2123931584507408/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Joe 2'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Joe 2'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_10156352789338586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Devin 3'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Devin 3'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_10154205820958586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Devin 2'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Devin 2'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_108170885940775/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Epic Retarget'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Epic Retarget'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Y-jNhGNrghwk",
        "colab_type": "code",
        "outputId": "caee4a19-e5a4-4c48-99e4-d07a5d9aa15f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 187
        }
      },
      "source": [
        "df_interim.count()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "campaign_name    1074\n",
              "cpm              1074\n",
              "date_start       1074\n",
              "date_stop        1074\n",
              "impressions      1074\n",
              "objective        1074\n",
              "reach            1074\n",
              "spend            1074\n",
              "account          1074\n",
              "dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3VYUvVu7IXFn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import time\n",
        "import json\n",
        "import urllib2\n",
        "import pandas as pd\n",
        "for y in range(1):\n",
        "  #time.sleep(3600)\n",
        "  count=0\n",
        "  #Myles\n",
        "  for x in range(10000):\n",
        "    time.sleep(20)\n",
        "    url = \"https://graph.facebook.com/v4.0/act_10156308372288586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&breakdowns=device_platform%2Cpublisher_platform%2Cplatform_position%2Cimpression_device&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "    response = urllib2.urlopen(url)\n",
        "    #data = response.read()\n",
        "    count+=1\n",
        "    #Sam\n",
        "    time.sleep(20)\n",
        "    url = \"https://graph.facebook.com/v4.0/act_10156162965163586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&breakdowns=device_platform%2Cpublisher_platform%2Cplatform_position%2Cimpression_device&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "    response = urllib2.urlopen(url)\n",
        "    #data = response.read()\n",
        "    count+=1\n",
        "    #Jenn\n",
        "    #time.sleep(23)\n",
        "    #url = \"https://graph.facebook.com/v4.0/act_512042582544198/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&breakdowns=device_platform%2Cpublisher_platform%2Cplatform_position%2Cimpression_device&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "    #response = urllib2.urlopen(url)\n",
        "    #data = response.read()\n",
        "    #count+=1\n",
        "    #Devin 3\n",
        "    #time.sleep(10)\n",
        "    #url = \"https://graph.facebook.com/v4.0/act_10156352789338586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&breakdowns=device_platform%2Cpublisher_platform%2Cplatform_position%2Cimpression_device&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "    #response = urllib2.urlopen(url)\n",
        "    #data = response.read()\n",
        "    #count+=1\n",
        "  #time.sleep(3600)\n",
        "    #values = json.loads(data)\n",
        "    #next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "    #df=pd.DataFrame.from_dict(values['data'])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HdNoE4a1ZG15",
        "colab_type": "code",
        "outputId": "4a51c9b5-a12f-4d5e-dbab-7a84682796b3",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 169
        }
      },
      "source": [
        "count"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "error",
          "ename": "NameError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m\u001b[0m",
            "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)",
            "\u001b[0;32m<ipython-input-1-26780ddf2751>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcount\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mNameError\u001b[0m: name 'count' is not defined"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4hzjqwbYyJ8h",
        "colab_type": "code",
        "outputId": "ffc37f2b-3944-4103-9ffe-4065d431a8e4",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 54
        }
      },
      "source": [
        "next"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'https://graph.facebook.com/v4.0/act_512042582544198/insights?access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&breakdowns=device_platform%2Cpublisher_platform%2Cplatform_position%2Cimpression_device&level=ad&limit=25&after=MzU0OQZDZD'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kck_drTyzBim",
        "colab_type": "text"
      },
      "source": [
        "# 4. Analyze Campaign Effectiveness"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DjB1vpUAJavU",
        "colab_type": "code",
        "outputId": "bb8ab205-1fef-4553-f12e-bbb414f7645e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 187
        }
      },
      "source": [
        "df_interim.count()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "campaign_name    1074\n",
              "cpm              1074\n",
              "date_start       1074\n",
              "date_stop        1074\n",
              "impressions      1074\n",
              "objective        1074\n",
              "reach            1074\n",
              "spend            1074\n",
              "account          1074\n",
              "dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0HXPB10iI0w_",
        "colab_type": "code",
        "outputId": "9d4840b0-c933-470a-c0c9-6cd6ef526280",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "from fastparquet import write\n",
        "write('ads.parq', df_interim)\n",
        "!gsutil cp ads.parq gs://epic-social/ads.parq"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Copying file://ads.parq [Content-Type=application/octet-stream]...\n",
            "-\n",
            "Operation completed over 1 objects/1.1 MiB.                                      \n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ffbSI0L27OgD",
        "colab_type": "code",
        "outputId": "bb5fb44b-8bf4-41a9-f6db-c83e0cdfc338",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 324
        }
      },
      "source": [
        "df_interim.to_json('ads.json')\n",
        "!gsutil cp ads.json gs://epic-social/ads.json"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m\u001b[0m",
            "\u001b[0;31mValueError\u001b[0mTraceback (most recent call last)",
            "\u001b[0;32m<ipython-input-12-8875746ffeaf>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdf_interim\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto_json\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ads.json'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mu'gsutil cp ads.json gs://epic-social/ads.json'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/pandas/core/generic.pyc\u001b[0m in \u001b[0;36mto_json\u001b[0;34m(self, path_or_buf, orient, date_format, double_precision, force_ascii, date_unit, default_handler, lines, compression, index)\u001b[0m\n\u001b[1;32m   2271\u001b[0m                             \u001b[0mdefault_handler\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdefault_handler\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2272\u001b[0m                             \u001b[0mlines\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlines\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcompression\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcompression\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2273\u001b[0;31m                             index=index)\n\u001b[0m\u001b[1;32m   2274\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2275\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mto_hdf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpath_or_buf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/pandas/io/json/json.pyc\u001b[0m in \u001b[0;36mto_json\u001b[0;34m(path_or_buf, obj, orient, date_format, double_precision, force_ascii, date_unit, default_handler, lines, compression, index)\u001b[0m\n\u001b[1;32m     60\u001b[0m         \u001b[0mdouble_precision\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdouble_precision\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mensure_ascii\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mforce_ascii\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     61\u001b[0m         \u001b[0mdate_unit\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdate_unit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdefault_handler\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdefault_handler\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 62\u001b[0;31m         index=index).write()\n\u001b[0m\u001b[1;32m     63\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     64\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mlines\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/pandas/io/json/json.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, obj, orient, date_format, double_precision, ensure_ascii, date_unit, index, default_handler)\u001b[0m\n\u001b[1;32m     94\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     95\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_copy\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 96\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_format_axes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     97\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     98\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_format_axes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/pandas/io/json/json.pyc\u001b[0m in \u001b[0;36m_format_axes\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    145\u001b[0m                 'index', 'columns'):\n\u001b[1;32m    146\u001b[0m             raise ValueError(\"DataFrame index must be unique for orient=\"\n\u001b[0;32m--> 147\u001b[0;31m                              \"'{orient}'.\".format(orient=self.orient))\n\u001b[0m\u001b[1;32m    148\u001b[0m         if not self.obj.columns.is_unique and self.orient in (\n\u001b[1;32m    149\u001b[0m                 'index', 'columns', 'records'):\n",
            "\u001b[0;31mValueError\u001b[0m: DataFrame index must be unique for orient='columns'."
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JIIRNe1cKcSX",
        "colab_type": "code",
        "outputId": "ea189e9f-0eda-4918-ff4b-87daa0dd4664",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        }
      },
      "source": [
        "from google.colab import auth\n",
        "auth.authenticate_user()\n",
        "\n",
        "# https://cloud.google.com/resource-manager/docs/creating-managing-projects\n",
        "project_id = 'alien-iterator-227622'\n",
        "!gcloud config set project {project_id}"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Updated property [core/project].\n",
            "\n",
            "\n",
            "To take a quick anonymous survey, run:\n",
            "  $ gcloud alpha survey\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9PF9NULeamMT",
        "colab_type": "code",
        "outputId": "33b5a1f0-69bd-4de6-d5a4-f877b5f6f8af",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 479
        }
      },
      "source": [
        "!pip install fastparquet"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting fastparquet\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/58/49/dccb790fa17ab3fbf84a6b848050083c7a1899e9586000e34e3e4fbf5538/fastparquet-0.3.2.tar.gz (151kB)\n",
            "\u001b[K     |████████████████████████████████| 153kB 8.8MB/s \n",
            "\u001b[?25hRequirement already satisfied: pandas>=0.19 in /usr/local/lib/python2.7/dist-packages (from fastparquet) (0.24.2)\n",
            "Requirement already satisfied: numba>=0.28 in /usr/local/lib/python2.7/dist-packages (from fastparquet) (0.40.1)\n",
            "Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python2.7/dist-packages (from fastparquet) (1.16.4)\n",
            "Collecting thrift>=0.11.0 (from fastparquet)\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/c6/b4/510617906f8e0c5660e7d96fbc5585113f83ad547a3989b80297ac72a74c/thrift-0.11.0.tar.gz (52kB)\n",
            "\u001b[K     |████████████████████████████████| 61kB 18.0MB/s \n",
            "\u001b[?25hRequirement already satisfied: six in /usr/local/lib/python2.7/dist-packages (from fastparquet) (1.12.0)\n",
            "Requirement already satisfied: pytz>=2011k in /usr/local/lib/python2.7/dist-packages (from pandas>=0.19->fastparquet) (2018.9)\n",
            "Requirement already satisfied: python-dateutil>=2.5.0 in /usr/local/lib/python2.7/dist-packages (from pandas>=0.19->fastparquet) (2.5.3)\n",
            "Requirement already satisfied: funcsigs in /usr/local/lib/python2.7/dist-packages (from numba>=0.28->fastparquet) (1.0.2)\n",
            "Requirement already satisfied: enum34 in /usr/local/lib/python2.7/dist-packages (from numba>=0.28->fastparquet) (1.1.6)\n",
            "Requirement already satisfied: llvmlite>=0.25.0dev0 in /usr/local/lib/python2.7/dist-packages (from numba>=0.28->fastparquet) (0.29.0)\n",
            "Requirement already satisfied: singledispatch in /usr/local/lib/python2.7/dist-packages (from numba>=0.28->fastparquet) (3.4.0.3)\n",
            "Building wheels for collected packages: fastparquet, thrift\n",
            "  Building wheel for fastparquet (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for fastparquet: filename=fastparquet-0.3.2-cp27-cp27mu-linux_x86_64.whl size=263369 sha256=07416532b202799c3d683e851a99cb6fd6692d20429f22812039c6ce2c551d51\n",
            "  Stored in directory: /root/.cache/pip/wheels/b9/36/13/01416a760ddcab0eb8281ec9c9ffcbed945c9b831647c8b904\n",
            "  Building wheel for thrift (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for thrift: filename=thrift-0.11.0-cp27-cp27mu-linux_x86_64.whl size=284098 sha256=ddff53103095c360cfcd4724adc77fedc7d655c5003bbe666e9081b0fbf11854\n",
            "  Stored in directory: /root/.cache/pip/wheels/be/36/81/0f93ba89a1cb7887c91937948519840a72c0ffdd57cac0ae8f\n",
            "Successfully built fastparquet thrift\n",
            "Installing collected packages: thrift, fastparquet\n",
            "Successfully installed fastparquet-0.3.2 thrift-0.11.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VO0qFXZe5dZS",
        "colab_type": "code",
        "outputId": "063f9cb8-3ff9-46c9-a368-52e4fbd9016e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        }
      },
      "source": [
        "!pip install pyarrow"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: pyarrow in /usr/local/lib/python2.7/dist-packages (0.13.0)\n",
            "Requirement already satisfied: six>=1.0.0 in /usr/local/lib/python2.7/dist-packages (from pyarrow) (1.12.0)\n",
            "Requirement already satisfied: enum34>=1.1.6; python_version < \"3.4\" in /usr/local/lib/python2.7/dist-packages (from pyarrow) (1.1.6)\n",
            "Requirement already satisfied: futures; python_version < \"3.2\" in /usr/local/lib/python2.7/dist-packages (from pyarrow) (3.2.0)\n",
            "Requirement already satisfied: numpy>=1.14 in /usr/local/lib/python2.7/dist-packages (from pyarrow) (1.16.3)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8FkCgbfbHeVh",
        "colab_type": "code",
        "outputId": "56a3e756-52c9-49ec-ef90-54c95fb49245",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 309
        }
      },
      "source": [
        "!pip install pandavro"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting pandavro\n",
            "  Downloading https://files.pythonhosted.org/packages/ca/c2/307ee588db6497289a3119e98577b4a71c291eb888ecb765b034acc177bc/pandavro-1.5.0.tar.gz\n",
            "Collecting fastavro>=0.14.11 (from pandavro)\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/15/8b/5d9736fec68e94ce4f98e98407b500c21d2faffcbdec52d09a96a0f68ddc/fastavro-0.21.23-cp27-cp27mu-manylinux1_x86_64.whl (1.0MB)\n",
            "\u001b[K     |████████████████████████████████| 1.0MB 2.9MB/s \n",
            "\u001b[?25hRequirement already satisfied: numpy>=1.7.0 in /usr/local/lib/python2.7/dist-packages (from pandavro) (1.16.3)\n",
            "Requirement already satisfied: pandas in /usr/local/lib/python2.7/dist-packages (from pandavro) (0.24.2)\n",
            "Requirement already satisfied: six>=1.9 in /usr/local/lib/python2.7/dist-packages (from pandavro) (1.12.0)\n",
            "Requirement already satisfied: pytz>=2011k in /usr/local/lib/python2.7/dist-packages (from pandas->pandavro) (2018.9)\n",
            "Requirement already satisfied: python-dateutil>=2.5.0 in /usr/local/lib/python2.7/dist-packages (from pandas->pandavro) (2.5.3)\n",
            "Building wheels for collected packages: pandavro\n",
            "  Building wheel for pandavro (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Stored in directory: /root/.cache/pip/wheels/a8/7e/0d/5e6a27d47e4c74a6e4dcdb73d07ea7b9fc957b1453162832ff\n",
            "Successfully built pandavro\n",
            "Installing collected packages: fastavro, pandavro\n",
            "Successfully installed fastavro-0.21.23 pandavro-1.5.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "s_3Y0Ox5KpnL",
        "colab_type": "code",
        "outputId": "31c7eb39-cdbb-4f0c-8e9c-9507f2e21b93",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 394
        }
      },
      "source": [
        "df2.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>account</th>\n",
              "      <th>date</th>\n",
              "      <th>description</th>\n",
              "      <th>expandedLinks</th>\n",
              "      <th>id</th>\n",
              "      <th>link</th>\n",
              "      <th>media</th>\n",
              "      <th>message</th>\n",
              "      <th>platform</th>\n",
              "      <th>platformId</th>\n",
              "      <th>postUrl</th>\n",
              "      <th>score</th>\n",
              "      <th>statistics</th>\n",
              "      <th>subscriberCount</th>\n",
              "      <th>type</th>\n",
              "      <th>updated</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>{u'subscriberCount': 46527589, u'profileImage'...</td>\n",
              "      <td>2019-06-25 23:52:14</td>\n",
              "      <td>Scream therapy @camila_cabello</td>\n",
              "      <td>NaN</td>\n",
              "      <td>63027061462</td>\n",
              "      <td>NaN</td>\n",
              "      <td>[{u'url': u'https://scontent.cdninstagram.com/...</td>\n",
              "      <td>NaN</td>\n",
              "      <td>Instagram</td>\n",
              "      <td>2074390522188857733_212742998</td>\n",
              "      <td>https://www.instagram.com/p/BzJtzB4gQGF/</td>\n",
              "      <td>14482525.0</td>\n",
              "      <td>{u'expected': {u'commentCount': 18209, u'viewC...</td>\n",
              "      <td>45463841.0</td>\n",
              "      <td>video</td>\n",
              "      <td>2019-07-04 19:10:33</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>{u'subscriberCount': 46527589, u'profileImage'...</td>\n",
              "      <td>2019-06-30 05:22:23</td>\n",
              "      <td>Thank you for all the love on Señorita ♥️ @cam...</td>\n",
              "      <td>NaN</td>\n",
              "      <td>63449743873</td>\n",
              "      <td>NaN</td>\n",
              "      <td>[{u'url': u'https://scontent.cdninstagram.com/...</td>\n",
              "      <td>NaN</td>\n",
              "      <td>Instagram</td>\n",
              "      <td>2077455948334515185_212742998</td>\n",
              "      <td>https://www.instagram.com/p/BzUmy2dAEPx/</td>\n",
              "      <td>7011901.0</td>\n",
              "      <td>{u'expected': {u'commentCount': 17756, u'viewC...</td>\n",
              "      <td>46129592.0</td>\n",
              "      <td>video</td>\n",
              "      <td>2019-07-04 19:10:33</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>{u'subscriberCount': 46527589, u'profileImage'...</td>\n",
              "      <td>2019-06-21 17:39:48</td>\n",
              "      <td>Señorita...@camila_cabello 🖤🌹</td>\n",
              "      <td>NaN</td>\n",
              "      <td>62681869480</td>\n",
              "      <td>NaN</td>\n",
              "      <td>[{u'url': u'https://scontent.xx.fbcdn.net/v/t5...</td>\n",
              "      <td>NaN</td>\n",
              "      <td>Instagram</td>\n",
              "      <td>2071304744961541723_212742998</td>\n",
              "      <td>https://www.instagram.com/p/By-wLD5A4Zb/</td>\n",
              "      <td>6992520.0</td>\n",
              "      <td>{u'expected': {u'commentCount': 29663, u'favor...</td>\n",
              "      <td>45007944.0</td>\n",
              "      <td>album</td>\n",
              "      <td>2019-07-04 19:10:33</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>{u'subscriberCount': 3899124, u'profileImage':...</td>\n",
              "      <td>2019-06-27 20:10:46</td>\n",
              "      <td>How to create Havana by Camila Cabello 😂 @imal...</td>\n",
              "      <td>NaN</td>\n",
              "      <td>63242103054</td>\n",
              "      <td>NaN</td>\n",
              "      <td>[{u'url': u'https://scontent.xx.fbcdn.net/v/t5...</td>\n",
              "      <td>NaN</td>\n",
              "      <td>Instagram</td>\n",
              "      <td>2075728995598371345_4686316577</td>\n",
              "      <td>https://www.instagram.com/p/BzOeIYYnooR/</td>\n",
              "      <td>6934390.0</td>\n",
              "      <td>{u'expected': {u'commentCount': 3219, u'viewCo...</td>\n",
              "      <td>3789428.0</td>\n",
              "      <td>video</td>\n",
              "      <td>2019-07-04 19:16:59</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>{u'subscriberCount': 46527589, u'profileImage'...</td>\n",
              "      <td>2019-06-20 18:01:40</td>\n",
              "      <td>#Señorita video with @camila_cabello midnight ...</td>\n",
              "      <td>NaN</td>\n",
              "      <td>62578870210</td>\n",
              "      <td>NaN</td>\n",
              "      <td>[{u'url': u'https://scontent.xx.fbcdn.net/v/t5...</td>\n",
              "      <td>NaN</td>\n",
              "      <td>Instagram</td>\n",
              "      <td>2070590974857563499_212742998</td>\n",
              "      <td>https://www.instagram.com/p/By8N4Vrg81r/</td>\n",
              "      <td>4070097.0</td>\n",
              "      <td>{u'expected': {u'commentCount': 26915, u'favor...</td>\n",
              "      <td>44973020.0</td>\n",
              "      <td>photo</td>\n",
              "      <td>2019-07-04 19:10:33</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                             account  ...              updated\n",
              "0  {u'subscriberCount': 46527589, u'profileImage'...  ...  2019-07-04 19:10:33\n",
              "1  {u'subscriberCount': 46527589, u'profileImage'...  ...  2019-07-04 19:10:33\n",
              "2  {u'subscriberCount': 46527589, u'profileImage'...  ...  2019-07-04 19:10:33\n",
              "3  {u'subscriberCount': 3899124, u'profileImage':...  ...  2019-07-04 19:16:59\n",
              "4  {u'subscriberCount': 46527589, u'profileImage'...  ...  2019-07-04 19:10:33\n",
              "\n",
              "[5 rows x 16 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QvaHA5hzATmQ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import json\n",
        "import urllib2\n",
        "url=''\n",
        "response=''\n",
        "data=''\n",
        "values=''\n",
        "df=''\n",
        "df2=''\n",
        "page=''\n",
        "url = \"https://api.crowdtangle.com/lists?token=07PCsFU8FOkUKLxmThNSHfLK7UP7KUWnHK0E7kCz\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7z4GyRq8BEog",
        "colab_type": "code",
        "outputId": "2818b03c-bb26-433e-ee9b-77f964a7ba40",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "source": [
        "values"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{u'result': {u'lists': [{u'id': 1124537,\n",
              "    u'title': u'Camila Cabello',\n",
              "    u'type': u'LIST'},\n",
              "   {u'id': 1124559, u'title': u'French Montana', u'type': u'LIST'},\n",
              "   {u'id': 1124561, u'title': u'AJ Mitchell', u'type': u'LIST'},\n",
              "   {u'id': 1124562, u'title': u'Zara Larsson', u'type': u'LIST'},\n",
              "   {u'id': 1124563, u'title': u'Tyla Yaweh', u'type': u'LIST'},\n",
              "   {u'id': 1157446, u'title': u'Meghan Trainor', u'type': u'LIST'},\n",
              "   {u'id': 1161953, u'title': u'DJ Khaled', u'type': u'LIST'},\n",
              "   {u'id': 1183368, u'title': u'Senorita', u'type': u'LIST'}]},\n",
              " u'status': 200}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ovHcp46RBG1x",
        "colab_type": "code",
        "outputId": "bd7d8895-677e-42b1-ed56-d165b39653dd",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 156
        }
      },
      "source": [
        "!curl -G \\\n",
        "!-d \"level=ad\" \\\n",
        "!-d \"fields=impressions,ad_id\" \\\n",
        "!-d \"access_token=EAAGTXZCl1AJUBAC3xy1WMOBTEyP3vbVGxalpoZCZAoVyJhJb3qPsWF7DEOljrMdfirZA7JOQPwp2IvbwGGMrjVjl6zgXnZB2ZCFS1ZBWqRFM0ZAJ2WlqSzyaIlbVdNwylgcWBbPi64yK9DrZBjHWZCDMZBENNrtoH7HYne8IEXDPcndBOU73ENMt6tQ4CeiKKb50jsZD\" \\\n",
        "!\"https://graph.facebook.com/v4.0/act_512042582544198/insights\""
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "curl: (6) Could not resolve host: !-d\n",
            "curl: (6) Could not resolve host: level=ad\n",
            "curl: (6) Could not resolve host: !-d\n",
            "curl: (6) Could not resolve host: fields=impressions,ad_id\n",
            "curl: (6) Could not resolve host: !-d\n",
            "curl: (6) Could not resolve host: access_token=EAAGTXZCl1AJUBAC3xy1WMOBTEyP3vbVGxalpoZCZAoVyJhJb3qPsWF7DEOljrMdfirZA7JOQPwp2IvbwGGMrjVjl6zgXnZB2ZCFS1ZBWqRFM0ZAJ2WlqSzyaIlbVdNwylgcWBbPi64yK9DrZBjHWZCDMZBENNrtoH7HYne8IEXDPcndBOU73ENMt6tQ4CeiKKb50jsZD\n",
            "curl: (1) Protocol \"!https\" not supported or disabled in libcurl\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gonS4Xc_P-lI",
        "colab_type": "code",
        "outputId": "1f0cfb14-c683-4ffe-e963-38845568c31e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 477
        }
      },
      "source": [
        "url = \"https://graph.facebook.com/v4.0/act_10156308372288586/insights?level=ad&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "response = urllib2.urlopen(url)\n",
        "data = response.read()\n",
        "values = json.loads(data)\n",
        "next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "df_interim=pd.DataFrame.from_dict(values['data'])\n",
        "df_interim['account']='Myles'\n",
        "for x in range(10000):\n",
        "  response = urllib2.urlopen(next)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Myles'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "error",
          "ename": "KeyError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m\u001b[0m",
            "\u001b[0;31mKeyError\u001b[0mTraceback (most recent call last)",
            "\u001b[0;32m<ipython-input-11-7b5b09221af5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     13\u001b[0m   \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'account'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'Myles'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m   \u001b[0mdf_interim\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconcat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mdf_interim\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m   \u001b[0mnext\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_dict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'paging'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'next'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'after'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'utf-8'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/pandas/core/frame.pyc\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m   2925\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnlevels\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2926\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_multilevel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2927\u001b[0;31m             \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2928\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mis_integer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2929\u001b[0m                 \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/base.pyc\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m   2657\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2658\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2659\u001b[0;31m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_maybe_cast_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2660\u001b[0m         \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtolerance\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2661\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;31mKeyError\u001b[0m: 'next'"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "j2AfjFWg3tV6",
        "colab_type": "code",
        "outputId": "8a8345a4-e516-4157-c807-849ce03cda29",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 187
        }
      },
      "source": [
        "df_interim.count()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "campaign_name    303\n",
              "cpm              303\n",
              "date_start       303\n",
              "date_stop        303\n",
              "impressions      303\n",
              "objective        303\n",
              "reach            303\n",
              "spend            303\n",
              "account          303\n",
              "dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QGYcn2yGqqrM",
        "colab_type": "code",
        "outputId": "e0f6042d-8b4c-48c2-cb7d-09aef99cebc0",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 111
        }
      },
      "source": [
        "pd.DataFrame.from_dict(values['paging'])"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>cursors</th>\n",
              "      <th>next</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>after</th>\n",
              "      <td>MjQZD</td>\n",
              "      <td>https://graph.facebook.com/v4.0/act_5120425825...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>before</th>\n",
              "      <td>MAZDZD</td>\n",
              "      <td>https://graph.facebook.com/v4.0/act_5120425825...</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "       cursors                                               next\n",
              "after    MjQZD  https://graph.facebook.com/v4.0/act_5120425825...\n",
              "before  MAZDZD  https://graph.facebook.com/v4.0/act_5120425825..."
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 51
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tI8HUjbfQmG7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "https://graph.facebook.com/v4.0/act_512042582544198/insights?access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&breakdowns=device_platform%2Cpublisher_platform%2Cplatform_position%2Cimpression_device&level=ad&limit=25&after=MjQZD'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oOMt0fhSQwBS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "https://graph.facebook.com/v4.0/act_512042582544198/insights?access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cobjective&breakdowns=device_platform%2Cpublisher_platform%2Cplatform_position%2Cimpression_device&level=ad&limit=25&after=MjQZD"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JHKi6ZdpD886",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Myles\n",
        "import time\n",
        "import json\n",
        "import urllib2\n",
        "import pandas as pd\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_10156308372288586/insights?level=campaign&time_range={'since':'2010-01-01','until':'2019-09-27'}&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cclicks%2Ccpc%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df_interim=pd.DataFrame.from_dict(values['data'])\n",
        "  df_interim['account']='Myles'\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Myles'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_512042582544198/insights?level=campaign&time_range={'since':'2010-01-01','until':'2019-09-27'}&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cclicks%2Ccpc%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Jenn'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Jenn'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_10156162965163586/insights?level=campaign&time_range={'since':'2010-01-01','until':'2019-09-27'}&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cclicks%2Ccpc%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Sam'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Sam'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_1376605895888910/insights?level=campaign&time_range={'since':'2010-01-01','until':'2019-09-27'}&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cclicks%2Ccpc%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Joe'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Joe'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_2123931584507408/insights?level=campaign&time_range={'since':'2010-01-01','until':'2019-09-27'}&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cclicks%2Ccpc%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Joe 2'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Joe 2'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_10156352789338586/insights?level=campaign&time_range={'since':'2010-01-01','until':'2019-09-27'}&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cclicks%2Ccpc%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Devin 3'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Devin 3'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_10154205820958586/insights?level=campaign&time_range={'since':'2010-01-01','until':'2019-09-27'}&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cclicks%2Ccpc%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Devin 2'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Devin 2'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass\n",
        "\n",
        "try:\n",
        "  url = \"https://graph.facebook.com/v4.0/act_108170885940775/insights?level=campaign&time_range={'since':'2010-01-01','until':'2019-09-27'}&fields=campaign_name%2Cspend%2Creach%2Cimpressions%2Ccpm%2Cclicks%2Ccpc%2Cobjective&access_token=EAAGTXZCl1AJUBABQZBXNtjUJt160w7s0YltBMTSEvpuowRSsp90MqgnM7jHHT4qYMIESqEUgFYpxXwtQN5vMzQdZAFRKTAStPVP1SMGspIIZCeUAZBHmH900HwLGdd5JZBDxdk7ZC0DdkdxH3aoCKu9ExaMltMz9djOXe8YYE4W0gZDZD\"\n",
        "  response = urllib2.urlopen(url)\n",
        "  data = response.read()\n",
        "  values = json.loads(data)\n",
        "  next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "  df=pd.DataFrame.from_dict(values['data'])\n",
        "  df['account']='Epic Retarget'\n",
        "  df_interim=pd.concat([df_interim,df])\n",
        "  for x in range(10000):\n",
        "    response = urllib2.urlopen(next)\n",
        "    data = response.read()\n",
        "    values = json.loads(data)\n",
        "    df=pd.DataFrame.from_dict(values['data'])\n",
        "    df['account']='Epic Retarget'\n",
        "    df_interim=pd.concat([df_interim,df])\n",
        "    next=pd.DataFrame.from_dict(values['paging'])['next']['after'].encode('utf-8')\n",
        "except:\n",
        "    pass"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8exo3lzND9n3",
        "colab_type": "code",
        "outputId": "37ac24d2-a813-4c91-81cf-4fa8ccccbcce",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 221
        }
      },
      "source": [
        "df_interim.count()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "campaign_name    7023\n",
              "clicks           7023\n",
              "cpc              7006\n",
              "cpm              7023\n",
              "date_start       7023\n",
              "date_stop        7023\n",
              "impressions      7023\n",
              "objective        7023\n",
              "reach            7023\n",
              "spend            7023\n",
              "account          7023\n",
              "dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SlBSvMhFHgtU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}