{
    "version": "https:\/\/jsonfeed.org\/version\/1",
    "title": "LEFT JOIN: blog on analytics, visualisation & data science, posts tagged: facebook",
    "home_page_url": "https:\/\/en.leftjoin.ru\/tags\/facebook\/",
    "feed_url": "https:\/\/en.leftjoin.ru\/tags\/facebook\/json\/",
    "icon": "https:\/\/en.leftjoin.ru\/user\/userpic@2x.jpg",
    "author": {
        "name": "Nikolay Valiotti",
        "url": "https:\/\/en.leftjoin.ru\/",
        "avatar": "https:\/\/en.leftjoin.ru\/user\/userpic@2x.jpg"
    },
    "items": [
        {
            "id": "23",
            "url": "https:\/\/en.leftjoin.ru\/all\/collecting-data-on-facebook-ad-campaigns\/",
            "title": "Collecting Data on Facebook Ad Campaigns",
            "content_html": "<p>Let’s find out how to obtain data on costs, ad clicks, and impressions on Facebook.<\/p>\n<pre class=\"e2-text-code\"><code>from facebook_business.api import FacebookAdsApi\r\nfrom facebook_business.exceptions import FacebookRequestError\r\nfrom facebook_business.adobjects.adaccount import AdAccount\r\nfrom facebook_business.adobjects.adreportrun import AdReportRun\r\nfrom facebook_business.adobjects.adsinsights import AdsInsights\r\nfrom facebook_business.adobjects.campaign import Campaign\r\nfrom facebook_business.adobjects.adset import AdSet\r\nfrom facebook_business.adobjects.adaccountuser import AdAccountUser as AdUser\r\nfrom facebook_business import adobjects\r\nfrom matplotlib import pyplot as plt\r\nfrom pandas import DataFrame\r\nimport time<\/code><\/pre><p><b>Getting the API Key<\/b><br \/>\nFirst thing we need to do before working with Facebook API is to create a Facebook App.  Go to <a href=\"https:\/\/developers.facebook.com\">https:\/\/developers.facebook.com<\/a> — My Apps — Create App. Type in your app name, your email, and click «Create App ID».<\/p>\n<div class=\"e2-text-picture\">\n<img src=\"https:\/\/en.leftjoin.ru\/pictures\/1.png\" width=\"318\" height=\"428\" alt=\"\" \/>\n<\/div>\n<p>After all the steps have done right you will see the App Dashboard. Choose Settings — Basic, copy your App ID and your App Secret. We will need this data for authentication process later.<\/p>\n<div class=\"e2-text-picture\">\n<img src=\"https:\/\/en.leftjoin.ru\/pictures\/2.png\" width=\"217\" height=\"135\" alt=\"\" \/>\n<\/div>\n<p>Now click Tools — Graph API Explorer and we will find ourselves in the token creation menu.<\/p>\n<div class=\"e2-text-picture\">\n<img src=\"https:\/\/en.leftjoin.ru\/pictures\/3.png\" width=\"480\" height=\"320\" alt=\"\" \/>\n<\/div>\n<p>These tokens can be generated for different needs, we need to set access rights for our token. We’ll need ads_management – this right allows to get information about ad campaigns for your Facebook account. Add it and click «Generate Access Token».<\/p>\n<p class=\"note\">Be careful – the user access token  provides only a temporary access and expires after 1-2 hours.  If you want to get a long-lived token click  the blue button to open Access Token Info -- Open in Access Token Tool. You will see a new page with the «Extend Access Token»  button, click it and a new long-lived Access Token will be generated for a period of 60 days.<\/p>\n<div class=\"e2-text-picture\">\n<img src=\"https:\/\/en.leftjoin.ru\/pictures\/4.png\" width=\"385\" height=\"221\" alt=\"\" \/>\n<\/div>\n<div class=\"e2-text-picture\">\n<img src=\"https:\/\/en.leftjoin.ru\/pictures\/9.png\" width=\"389\" height=\"123\" alt=\"\" \/>\n<\/div>\n<p><b>Writing the Script<\/b><\/p>\n<p>Now we need to create 3 variables, assign them to our Access Token, App ID and App Secret respectively.  Log in using the init() method of FacebooksAdsApi Class and add a user. The get_ad_accounts() method returns data on all our ad accounts as a dictionary. We can get the same data using the get_campaigns() method instead.<\/p>\n<pre class=\"e2-text-code\"><code>my_access_token = ''\r\nmy_app_id = ''\r\nmy_app_secret = ''\r\nFacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)\r\n\r\nme = AdUser(fbid='me')\r\nmy_accounts = list(me.get_ad_accounts())\r\nmy_accounts\r\n\r\nmy_account = my_accounts[0]\r\ncampaigns = my_account.get_campaigns()\r\nprint(campaigns)<\/code><\/pre><p>Let’s retrieve the amount spent using my_account. We’ll use the api_get() method and pass AdAccount.Field.amount_spent in the fields parameter. Now, to receive the data we need some math:<br \/>\ndivide my_account by 100  to see a whole number. The expenses will be displayed in your account currency, in our case it’s RUB. The main purpose of our actions – obtaining  data on the costs of ad campaigns for further analysis:<\/p>\n<pre class=\"e2-text-code\"><code>my_account.api_get(fields=[AdAccount.Field.amount_spent])\r\nprint(int(my_account[AdAccount.Field.amount_spent])\/100)<\/code><\/pre><p>The next step will be creating two functions.  The first one will send asynchronous queries to Facebook and return results. The second function forms these queries and passes them in the first function. As a result, we will get a list of dictionaries.<\/p>\n<pre class=\"e2-text-code\"><code>count = 0\r\n\r\ndef wait_for_async_job(async_job):\r\n    global count\r\n    async_job = async_job.api_get()\r\n    while async_job[AdReportRun.Field.async_status] != 'Job Completed' or async_job[\r\n        AdReportRun.Field.async_percent_completion] &lt; 100:\r\n        time.sleep(2)\r\n        async_job = async_job.api_get()\r\n    else:\r\n        print(&quot;Job &quot; + str(count) + &quot; completed&quot;)\r\n        count += 1\r\n    return async_job.get_result(params={&quot;limit&quot;: 1000})\r\n\r\ndef get_insights(account, date_preset='last_3d'):\r\n    account = AdAccount(account[&quot;id&quot;])\r\n    i_async_job = account.get_insights(\r\n        params={\r\n            'level': 'ad',\r\n            'date_preset': date_preset,\r\n            'time_increment': 1},\r\n            fields=fields,\r\n            is_async=True)\r\n    results = [dict(item) for item in wait_for_async_job(i_async_job)]\r\n    return results<\/code><\/pre><p>The following step is to get data on costs.  We need the data for all time, therefore create the variable date_preset, set its value to lifetime. Call the get_insights() function for every account, and assign the output to insights_lists.<br \/>\nCreate a DataFrame and extract the relevant data from the insights_lists, that’s campaign’s id, number of clicks, costs and impressions.<\/p>\n<pre class=\"e2-text-code\"><code>elem_insights = []\r\ninsights_lists = []\r\ndate_preset = 'last_year'\r\nfor elem in my_accounts:\r\n            elem_insights = get_insights(elem, date_preset)\r\n            insights_lists.append(elem_insights)\r\n\r\ninsight_campaign_id_list = []\r\ninsight_clicks_list = []\r\ninsight_spend_list = []\r\ninsight_impressions_list = []\r\ninsight_date_start_list = []\r\ninsight_date_stop_list = []\r\nfor elem1 in insights_lists:\r\n    for elem2 in elem1:\r\n        insight_campaign_id_list.append(int(elem2['campaign_id']))\r\n        insight_clicks_list.append(int(elem2['clicks']))\r\n        insight_spend_list.append(float(elem2['spend']))\r\n        insight_impressions_list.append(int(elem2['impressions']))\r\n        insight_date_start_list.append(elem2['date_start'])\r\n        insight_date_stop_list.append(elem2['date_stop'])\r\n\r\ndf = DataFrame()\r\ndf['campaign_id'] = insight_campaign_id_list\r\ndf['clicks'] = insight_clicks_list\r\ndf['spend'] = insight_spend_list\r\ndf['impressions'] = insight_impressions_list\r\ndf['date_start'] = insight_date_start_list\r\ndf['date_stop'] = insight_date_stop_list<\/code><\/pre><p>We’ll get the following DataFrame:<\/p>\n<div class=\"e2-text-picture\">\n<img src=\"https:\/\/en.leftjoin.ru\/pictures\/5.png\" width=\"465\" height=\"226\" alt=\"\" \/>\n<\/div>\n<p>Let’s summarize this data – group it by campaigns and calculate the sum for each group. Pandas have build-in group() and sum() methods for it,  just keep in mind which column to group by.<\/p>\n<pre class=\"e2-text-code\"><code>df.groupby(['campaign_id']).sum()<\/code><\/pre><div class=\"e2-text-picture\">\n<img src=\"https:\/\/en.leftjoin.ru\/pictures\/6.png\" width=\"294\" height=\"117\" alt=\"\" \/>\n<\/div>\n<p>Now, plot two graphs  – by the number of impressions and clicks relative to the dates. We’ll use the rcParams attribute  to set parameters for the graphs.<\/p>\n<pre class=\"e2-text-code\"><code>plt.rcParams['figure.figsize'] = [20, 5]\r\nplt.plot(df.date_start.str.replace('2019-', ''), df.clicks)<\/code><\/pre><div class=\"e2-text-picture\">\n<img src=\"https:\/\/en.leftjoin.ru\/pictures\/7.png\" width=\"992\" height=\"269\" alt=\"\" \/>\n<\/div>\n<pre class=\"e2-text-code\"><code>plt.rcParams['figure.figsize'] = [20, 5]\r\nplt.plot(df.date_start.str.replace('2019-', ''), df.impressions)<\/code><\/pre><div class=\"e2-text-picture\">\n<img src=\"https:\/\/en.leftjoin.ru\/pictures\/8.png\" width=\"992\" height=\"269\" alt=\"\" \/>\n<\/div>\n<p>That’s it! Now we have the data on costs, ad impressions and clicks. In the next article,  we will explain how to upload it in JSON format and pass it to Redash for further analysis and visualization.<\/p>\n",
            "date_published": "2020-05-12T14:09:02+03:00",
            "date_modified": "2020-05-13T13:28:00+03:00",
            "image": "https:\/\/en.leftjoin.ru\/pictures\/1.png",
            "_date_published_rfc2822": "Tue, 12 May 2020 14:09:02 +0300",
            "_rss_guid_is_permalink": "false",
            "_rss_guid": "23",
            "_e2_data": {
                "is_favourite": false,
                "links_required": [
                    "system\/library\/highlight\/highlight.js",
                    "system\/library\/highlight\/highlight.css",
                    "system\/library\/highlight\/highlight.js",
                    "system\/library\/highlight\/highlight.css",
                    "system\/library\/highlight\/highlight.js",
                    "system\/library\/highlight\/highlight.css",
                    "system\/library\/highlight\/highlight.js",
                    "system\/library\/highlight\/highlight.css",
                    "system\/library\/highlight\/highlight.js",
                    "system\/library\/highlight\/highlight.css",
                    "system\/library\/highlight\/highlight.js",
                    "system\/library\/highlight\/highlight.css",
                    "system\/library\/highlight\/highlight.js",
                    "system\/library\/highlight\/highlight.css",
                    "system\/library\/highlight\/highlight.js",
                    "system\/library\/highlight\/highlight.css"
                ],
                "og_images": [
                    "https:\/\/en.leftjoin.ru\/pictures\/1.png",
                    "https:\/\/en.leftjoin.ru\/pictures\/2.png",
                    "https:\/\/en.leftjoin.ru\/pictures\/3.png",
                    "https:\/\/en.leftjoin.ru\/pictures\/4.png",
                    "https:\/\/en.leftjoin.ru\/pictures\/9.png",
                    "https:\/\/en.leftjoin.ru\/pictures\/5.png",
                    "https:\/\/en.leftjoin.ru\/pictures\/6.png",
                    "https:\/\/en.leftjoin.ru\/pictures\/7.png",
                    "https:\/\/en.leftjoin.ru\/pictures\/8.png"
                ]
            }
        }
    ],
    "_e2_version": 3386,
    "_e2_ua_string": "E2 (v3386; Aegea)"
}