예제 #1
0
    def get_report(self, fields, params, file_dir):
        """
            Uses asynchronous calls to avoid time out. Pins every 15 seconds to check status. Once done, pull 
            the data, and dump data into a local file
            :param fields: all valid fields we can retrieve from an Ad account
            :param params: specifies report level, report dates, breakdowns, filters
            :param file_dir: specifies the downloading location
            :return: None
        """
        with gzip.open(file_dir, 'wb') as outfile:
            for account_id in self.account_ids:
                account = AdAccount('act_' + account_id)

                async_job = account.get_insights(fields=fields,
                                                 params=params,
                                                 async=True)
                async_job.remote_read()
                while async_job[
                        AdReportRun.Field.async_status] != 'Job Completed':
                    time.sleep(5)
                    async_job.remote_read()
                time.sleep(1)

                for result in async_job.get_result():
                    json.dump(result.export_all_data(), outfile)
                    outfile.write('\n')
        outfile.close()
    def get_ads_insight(self, report_date,account_id):

        ad_account = AdAccount(fbid=account_id)
        limit = 5000
        fields = ['campaign_name',
                'ad_id',
                'reach',
                'date_start',
                'date_stop',
                'impressions',
                'actions',
                'ctr',
                'clicks',
                'objective',
                'spend'
            ]
        params = {
            'time_range': {
                'since': report_date,
                'until': report_date
            },
#            'date_preset':'last_7d',
            'action_attribution_windows': ['28d_click','28d_view'],
            'breakdowns': ['gender', 'age'],
            'time_increment':'1',             
            'level': 'ad',
            'limit': limit if limit > 0 else None
        }
        insights = ad_account.get_insights(fields, params)
        return insights
def adset_comment_fetcher(account, params, fields):
    """
    fetches ad ids and corresponding adset name from active campaigns for the selected ad account
    """
    account = AdAccount(account)

    adset_insights = account.get_insights(fields=fields, params=params)
    for insight in adset_insights:
        adset_name_ad_id[insight['adset_name']] = insight['ad_id']
    return adset_name_ad_id
def ad_ids_fetcher(account, params, fields):
    """
    fetches ad ids from active campaigns for the selected ad account

    """
    account = AdAccount(account)
    try:
        adset_insights = account.get_insights(fields=fields, params=params)
    except Exception:
        exception_catcher('/facebook_ads_comments_analyzer/')

    ad_ids_list = [insight['ad_id'] for insight in adset_insights]

    return ad_ids_list
예제 #5
0
def get_account_ad_performance_for_single_day(ad_account: adaccount.AdAccount,
                                              single_date: datetime) -> adsinsights.AdsInsights:
    """Downloads the ad performance for an ad account for a given day
    https://developers.facebook.com/docs/marketing-api/insights

    Args:
        ad_account: An ad account to download.
        single_date: A single date as a datetime object

    Returns:
        A list containing dictionaries with the ad performance from the report

    """
    logging.info('download Facebook ad performance of act_{ad_account_id} on {single_date}'.format(
        ad_account_id=ad_account['account_id'],
        single_date=single_date.strftime('%Y-%m-%d')))

    ad_insights = ad_account.get_insights(
        # https://developers.facebook.com/docs/marketing-api/insights/fields
        fields=['date_start',
                'ad_id',
                'impressions',
                'actions',
                'spend',
                'action_values'],
        # https://developers.facebook.com/docs/marketing-api/insights/parameters
        params={'action_attribution_windows': ['28d_click'],
                # https://developers.facebook.com/docs/marketing-api/insights/action-breakdowns
                'action_breakdowns': ['action_type'],
                # https://developers.facebook.com/docs/marketing-api/insights/breakdowns
                'breakdowns': ['impression_device'],
                'level': 'ad',
                'limit': 1000,
                'time_range': {'since': single_date.strftime('%Y-%m-%d'),
                               'until': single_date.strftime('%Y-%m-%d')},
                # By default only ACTIVE campaigns get considered.
                'filtering': [{
                    'field': 'ad.effective_status',
                    'operator': 'IN',
                    'value': ['ACTIVE',
                              'PAUSED',
                              'PENDING_REVIEW',
                              'DISAPPROVED',
                              'PREAPPROVED',
                              'PENDING_BILLING_INFO',
                              'CAMPAIGN_PAUSED',
                              'ARCHIVED',
                              'ADSET_PAUSED']}]})

    return ad_insights
예제 #6
0
def get_fb_data(AccountId):
    FacebookAdsApi.init(facebookConfig.Configs['App_id'],
                        facebookConfig.Configs['App_secret'],
                        facebookConfig.Configs['Access_token'])

    my_account = AdAccount(facebookConfig.Accounts[AccountId])

    # Grab insight info for all ads in the adaccount

    #Ever this function will get data from yesterday
    #If somebody wants to change the range of date, is necessary to change the param 'date_preset'
    #You can change the level of your request. example: ad, adset or campaign
    '''
    I dont recommend change the param date_preset because the data comes cumulative, not good for me.
    The data from yesterday is better and trustworthy . I suggest to compare with reports from your Business manager facebook
    '''
    data = my_account.get_insights(
        params={
            'date_preset': 'yesterday',
            'level': 'ad'
        },
        fields=[
            AdsInsights.Field.account_id,
            AdsInsights.Field.account_name,
            #AdsInsights.Field.campaign_id,
            AdsInsights.Field.campaign_name,
            AdsInsights.Field.ad_name,
            AdsInsights.Field.ad_id,
            AdsInsights.Field.adset_id,
            AdsInsights.Field.adset_name,
            AdsInsights.Field.cost_per_outbound_click,
            AdsInsights.Field.outbound_clicks,
            AdsInsights.Field.spend,
            AdsInsights.Field.clicks,
            AdsInsights.Field.inline_link_clicks,
            AdsInsights.Field.inline_link_click_ctr,
            AdsInsights.Field.cpm,
            AdsInsights.Field.ctr,
            AdsInsights.Field.reach,
            AdsInsights.Field.frequency,
            AdsInsights.Field.impressions
        ])

    dataNormalize = normalize.dumps_data(data)

    df = normalize.change_types(dataNormalize)

    return df
 def get_campaign_insights_by_account(self,
                                      account_id,
                                      breakdown_attribute=None,
                                      start_dt=None,
                                      stop_dt=None):
     account = AdAccount('act_%s' % str(account_id))
     params = {}
     if breakdown_attribute: params['breakdowns'] = breakdown_attribute
     if start_dt and stop_dt:
         params['time_range'] = {'since': start_dt, 'until': stop_dt}
     params['fields'] = [
         'account_id', 'account_name', 'campaign_id', 'campaign_name',
         'action_values', 'clicks', 'impressions', 'reach', 'spend',
         'actions', 'cpc', 'cpm', 'cpp', 'ctr', 'frequency'
     ]
     params['level'] = 'campaign'
     insights = account.get_insights(params=params)
     if insights:
         return insights
     return {}
예제 #8
0
sys.path.append(path)
from my_facebook_access import my_app_id, my_app_secret, my_access_token, my_adaccount_id, campaigns_group

# print("my_app_id:", my_app_id)
# print("my_app_secret:", my_app_secret)
# print("my_access_token:", my_access_token)
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)

selected_adaccount = AdAccount(fbid=my_adaccount_id)
print("Selected Ad Account:", selected_adaccount)

fields = [
    AdsInsights.Field.campaign_name, AdsInsights.Field.cpc,
    AdsInsights.Field.cpm, AdsInsights.Field.ctr,
    AdsInsights.Field.impressions, AdsInsights.Field.frequency,
    AdsInsights.Field.spend
]

params = {
    'time_range': {
        'since': str(date(2017, 1, 1)),
        'until': str(date.today())
    },
    'level': 'campaign',
    'limit': 1000,
    'configured_status': 'ACTIVE'
}

insights = selected_adaccount.get_insights(fields=fields, params=params)
print(insights)
예제 #9
0
def get_account_ad_performance_for_single_day(ad_account: adaccount.AdAccount,
                                                          single_date

: datetime) -> adsinsights.AdsInsights:
"""Downloads the ad performance for an ad account for a given day
https://developers.facebook.com/docs/marketing-api/insights
Args:
    ad_account: An ad account to download.
    single_date: A single date as a datetime object
Returns:
    A list containing dictionaries with the ad performance from the report
"""
logging.info('download Facebook ad performance of act_{ad_account_id} on {single_date}'.format(
    ad_account_id=ad_account['account_id'],
    single_date=single_date.strftime('%Y-%m-%d')))

ad_insights = ad_account.get_insights(
    # https://developers.facebook.com/docs/marketing-api/insights/fields
    fields=['date_start',
            'ad_id',
            'impressions',
            'actions',
            'spend',
            'action_values'],
    # https://developers.facebook.com/docs/marketing-api/insights/parameters
    params={'action_attribution_windows': ['28d_click'],
            # https://developers.facebook.com/docs/marketing-api/insights/action-breakdowns
            'action_breakdowns': ['action_type'],
            # https://developers.facebook.com/docs/marketing-api/insights/breakdowns
            'breakdowns': ['impression_device'],
            'level': 'ad',
            'limit': 1000,
            'time_range': {'since': single_date.strftime('%Y-%m-%d'),
                           'until': single_date.strftime('%Y-%m-%d')},
            # By default only ACTIVE campaigns get considered.
            'filtering': [{
                'field': 'ad.effective_status',
                'operator': 'IN',
                'value': ['ACTIVE',
                          'PAUSED',
                          'PENDING_REVIEW',
                          'DISAPPROVED',
                          'PREAPPROVED',
                          'PENDING_BILLING_INFO',
                          'CAMPAIGN_PAUSED',
                          'ARCHIVED',
                          'ADSET_PAUSED']}]})

return ad_insights


def ensure_data_directory(relative_path: Path = None

) -> Path:
"""Checks if a directory in the data dir path exists. Creates it if necessary
Args:
    relative_path: A Path object pointing to a file relative to the data directory
Returns:
    The absolute path Path object
"""
if relative_path is None:
    return Path(config.data_dir())
try:
    path = Path(config.data_dir(), relative_path)
    # if path points to a file, create parent directory instead
    if path.suffix:
        if not path.parent.exists():
            path.parent.mkdir(exist_ok=True, parents=True)
    else:
        if not path.exists():
            path.mkdir(exist_ok=True, parents=True)
    return path
except OSError as exception:
    if exception.errno != errno.EEXIST:
        raise


def parse_labels(labels: [{}]

) -> {str: str}:
"""Extracts labels from a string.
Args:
    labels: Labels in the form of
            [{"id": "1", "name": "{key_1=value_1}"},
             {"id": "2", "name": "{key_2=value_2}"}]"'
Returns:
        A dictionary of labels with {key_1 : value_1, ...} format
"""
labels_dict = {}
for label in labels:
    match = re.search("{([a-zA-Z|_]+)=([a-zA-Z|_]+)}", label['name'])
    if match:
        key = match.group(1).strip().lower().title()
        value = match.group(2).strip()
        labels_dict[key] = value
return labels_dict


@rate_limiting
def _get_ad_accounts() ->


[adaccount.AdAccount]:
"""Retrieves the ad accounts of the user whose access token was provided and
returns them as a list.
Returns:
    A list of ad accounts
"""
system_user = user.User(fbid='me')
ad_accounts = system_user.get_ad_accounts(fields=['account_id',
                                                  'name',
                                                  'created_time',
                                                  'timezone_offset_hours_utc'])
return list(ad_accounts)


def _upsert_ad_performance(ad_insights: [adsinsights.AdsInsights], con

: sqlite3.Connection):
"""Creates the ad performance table if it does not exists and upserts the
ad insights data afterwards
Args:
    ad_insights: A list of Insights objects
    con: A sqlite database connection
"""
con.execute("""
CREATE TABLE IF NOT EXISTS ad_performance (
  date          DATE   NOT NULL,
  ad_id         BIGINT NOT NULL,
  device        TEXT   NOT NULL,
  performance   TEXT   NOT NULL,
  PRIMARY KEY (ad_id, device)
);""")
con.executemany("INSERT OR REPLACE INTO ad_performance VALUES (?,?,?,?)",
                _to_insight_row_tuples(ad_insights))


def _to_insight_row_tuples(ad_insights: [adsinsights.AdsInsights]

) -> Generator[tuple, None, None]:
"""Transforms the Insights objects into tuples that can be directly inserted
into the ad_performance table
Args:
    ad_insights: A list of Insights objects for an ad on a specific day
Returns:
    A list of tuples of ad performance data
"""
for ad_insight in ad_insights:
    actions = ad_insight.get('actions') or []
    actions = [_floatify_values(action) for action in actions]

    action_values = ad_insight.get('action_values') or []
    action_values = [_floatify_values(action_value) for action_value in action_values]

    performance = {'impressions': int(ad_insight['impressions']),
                   'spend': float(ad_insight['spend']),
                   'actions': actions,
                   'action_values': action_values}

    ad_insight_tuple = (ad_insight['date_start'],
                        ad_insight['ad_id'],
                        ad_insight['impression_device'],
                        json.dumps(performance))

    yield ad_insight_tuple


def _floatify(value: str

) -> Union[str, float]:
try:
    return float(value)
except ValueError:
    return value


def _floatify_values(inp: {}

) -> {}:
return {key: _floatify(value) for key, value in inp.items()}


def _first_download_date_of_ad_account(ad_account: adaccount.AdAccount

) -> datetime.date:
"""Finds the first date for which the ad account's performance should be
downloaded by comparing the first download date from the configuration and
the creation date of the account and returning the maximum of the two.
Args:
    ad_account: An ad account to download
Returns:
    The first date to download the performance data for
"""
config_first_date = datetime.datetime.strptime(config.first_date(),
                                               '%Y-%m-%d').date()
if 'created_time' in ad_account:
    account_created_date = datetime.datetime.strptime(ad_account['created_time'],
                                                      "%Y-%m-%dT%H:%M:%S%z").date()
    return max(config_first_date, account_created_date)
else:
    return config_first_date
예제 #10
0
#"event_type": "update_ad_run_status"
header = "access_token=" + access_token

#url = "https://graph.facebook.com/v2.10/act_693598040844198/campaigns?fields=name,status,insights{reach,impressions,clicks}"

#res = r.get(url,headers=header)

#i = AdAccount(aid).get_insights(fields=fields, async=True)
from facebookads.adobjects.campaign import Campaign
from facebookads.adobjects.adsinsights import AdsInsights as Insights

campaign = Campaign('23842767635360263')

params = {'time_range': {'since': '2018-02-01', 'until': '2018-03-01'}}

insights = account.get_insights(params=params)

print(insights)

#print(campaign.Field.name.Impressions)
#print(campaign.get_ads(fields=["name","status","insights{reach,impressions,clicks}"]))
#print(campaign[adobjects.campaign])

#insights = campaign.get_insights(params=params)
#print(insights)

#print(account.remote_read(fields=[AdAccount.Field.end_advertiser_name]))
#adsets = account.get_ad_sets(fields=[AdSet.Field.name])
"""
Date
Campaign
예제 #11
0
def get_account_insights(id, fields, params):
    account = AdAccount(id)
    return list(account.get_insights(params=params, fields=fields))
예제 #12
0
def main():
    from datetime import date, timedelta
    yesterday = date.today() - timedelta(1)
    yesterday = yesterday.strftime('%Y-%m-%d')

    dbname = "ganalytic"
    host = "ganalytic.cvqb8lx7aald.us-west-2.redshift.amazonaws.com"
    port = "5439"
    user = "******"
    password = "******"
    conn = create_engine('postgresql://' + user + ':' + password + '@' + host +
                         ':' + port + '/' + dbname)

    from facebookads.adobjects.adaccount import AdAccount
    from facebookads.adobjects.adsinsights import AdsInsights as Insights
    from facebookads.adobjects.adcreative import AdCreative
    from facebookads.adobjects.ad import Ad

    from facebookads.api import FacebookAdsApi

    app_id = "216523165752115"
    app_secret = "83d3bbd1111701e624685c56915e7f59"

    user_token='EAAF7pPE6azkBABq1SjxBDZALNCyua8KncmEMDeZBHshZA2ySIZCKtN6jfBunBkGbYPS1LwrDxC5ETccT6Tcs' \
               'FDYX7ZA1m9QkSUgH3tEJpJPT6Vj9I5rBqe7TlPT3qCffGcScJm3nI6ZAzNyM9nZB61vCAmOV8IZBZAxm71DwnnVibDgZAdfne3wRpgFuHMcwx5eKoZD'


    access_token="EAADE7TphWzMBAI7u4hwkZB7dtNQpZAjnUkJTzCaVeYmJZC1QOXPxZCzn4V9h9vQ1gZC5gad1dGwyPNTZBSjB35Mx1dn6IVQXO" \
                 "7fvh1edSYoMZCmx3gYGNAiLxm0P3n3cEk4fKS7tNgKGF7P8kXMLLZBNyLREGb2xVDZA2bXkavXS8twZDZD"

    app_token = "417423328701241|hyOJvm8h5JpFXMyOiMQQZhOBwFs"

    FacebookAdsApi.init(app_id, app_secret, access_token)

    account = AdAccount("act_693598040844198")
    #account.remote_read(fields=[AdAccount.Field.timezone_name])

    start = "2018-04-01"
    end = "2018-04-01"

    fields = [
        Insights.Field.campaign_name, Insights.Field.campaign_id,
        Insights.Field.impressions, Insights.Field.reach,
        Insights.Field.actions, Insights.Field.spend, Insights.Field.ad_id,
        Insights.Field.date_start, Insights.Field.date_stop,
        Insights.Field.adset_name, Insights.Field.clicks,
        Insights.Field.frequency, Insights.Field.unique_actions

        #Insights.Field.People Taking action
        #Insights.Field.Post engemnet
        #Insights.Field.actions
    ]

    params = {
        'time_range': {
            'since': start,
            'until': end
        },
        'level': 'ad',
        'limit': 10000,
        'breakdowns': ["impression_device", "publisher_platform"]
    }
    #insights = account.get_insights(fields=fields, params=params)

    #from facebookads.adobjects.campaign import Campaign
    #campaign = Campaign('2384276763536026')
    insights = account.get_insights(fields=fields, params=params)

    res = [i for i in insights]
    print(res)

    #df = pd.DataFrame(res)
    #print(df["unique_actions"])
    """"