A union of curiosity and data science

Knowledgebase and brain dump of a database engineer


Facebook Insights API : Single Ad Performance Results

The facebook Ad Inisghts API provides access to your ad campaign performance statistics. Below, you'll find some basic calls which may come in handy.

you can find the Graph API Explorer here: https://developers.facebook.com/tools/explorer

API Call for a single ad: 

https://graph.facebook.com/v2.8/<YOUR AD ID>/insights?fields=actions,date_start,date_stop,impressions,spend,account_id,campaign_id,adset_id,ad_id,ad_name,buying_type,action_values,campaign_name,clicks,adset_name&time_range={"since":"2016-10-30","until":"2016-10-30"}&format=json&access_token=<YOUR ACCESS TOKEN>

Result:

{
   "data": [
      {
         "actions": [
            {
               "action_type": "comment",
               "value": "1"
            },
            {
               "action_type": "like",
               "value": "2"
            },
            {
               "action_type": "link_click",
               "value": "100"
            },
            {
               "action_type": "offsite_conversion.fb_pixel_add_to_cart",
               "value": "26"
            },
            {
               "action_type": "offsite_conversion.fb_pixel_purchase",
               "value": "2"
            },
            {
               "action_type": "offsite_conversion.fb_pixel_view_content",
               "value": "231"
            },
            {
               "action_type": "offsite_conversion.other",
               "value": "4"
            },
            {
               "action_type": "post",
               "value": "1"
            },
            {
               "action_type": "post_reaction",
               "value": "23"
            },
            {
               "action_type": "page_engagement",
               "value": "127"
            },
            {
               "action_type": "post_engagement",
               "value": "125"
            },
            {
               "action_type": "offsite_conversion",
               "value": "263"
            }
         ],
         "date_start": "2016-10-30",
         "date_stop": "2016-10-30",
         "impressions": "15568",
         "spend": "113.73",
         "account_id": "our ids",
         "campaign_id": "our ids",
         "adset_id": "our ids",
         "ad_id": "our ids",
         "ad_name": "our ad name",
         "action_values": [
            {
               "action_type": "offsite_conversion.fb_pixel_add_to_cart",
               "value": "724.53"
            },
            {
               "action_type": "offsite_conversion.fb_pixel_purchase",
               "value": "13990"
            },
            {
               "action_type": "offsite_conversion.other",
               "value": "432.4"
            },
            {
               "action_type": "offsite_conversion",
               "value": "15146.93"
            }
         ],
         "campaign_name": "m6-Single Product Styles 7/29/2016",
         "clicks": "154",
         "adset_name": "Retention Remarketing"
      }
   ],
   "paging": {
      "cursors": {
         "before": "MAZDZD",
         "after": "MAZDZD"
      }
   }
}

 

API Call for Ad Account : 

https://graph.facebook.com/v2.8/act_<account_id>/insights?fields=website_clicks,unique_social_clicks,cpc,cpm&access_token=<your access token>

Result: 

{
  "data": [
    {
      "website_clicks": "552008",
      "unique_social_clicks": "227833",
      "cpc": "0.439427",
      "cpm": "6.523525",
      "date_start": "2016-09-28",
      "date_stop": "2016-10-27"
    }
  ],
  "paging": {
    "cursors": {
      "before": "MAZDZD",
      "after": "MAZDZD"
    }
  }
}

 

C# Query Facebook Insights API

using System.Net;
 
string request_url = "https://graph.facebook.com/v2.8/<account_id>/insights?fields=actions,account_name,ad_name,ad_id&time_range={'since':'2016-10-20','until':'2016-10-20'}&format=json&access_token=<your token>
WebClient req = new WebClient();
Console.WriteLine(req.DownloadString(request_url));
Console.ReadKey();

 

Python 3 Connection to SQL Server with PYPY

Download pypi and install it on your server with python.

https://pypi.python.org/pypi/pypyodbc/

Once the install is complete, run the following code. It will:
   1. create a table
   2. create a few inserts
   3. select the records 
   4. loop through and print the records

 

#!/usr/bin/python3
import pypyodbc
 
#connect to the database. 
conn = pypyodbc.connect(driver='{SQL Server}', server='myserver', database='mydb', uid='user', pwd='pass') 
cur = conn.cursor()

table_create = """
if not exists(select * from information_schema.tables where table_name = 'Hello_World') 
        Create Table Hello_World (id int identity, val varchar(32))
"""
cur.execute(table_create) #create the table 
cur.execute("insert into hello_world values ('Hello'), ('to'), ('you'), ('too')") #insert some rows
cur.commit() #commit the above sql statements. 

cur.execute("select * from hello_world")
results = cur.fetchone() 

while results:
    print ("values :  id=" +  str(results[0]) + " and val='" + results["val"] + "'")
    results = cur.fetchone() 
conn.close()