def get_campaign_insights(self, date_preset=None):
     campaigns = campaign.Campaign(self.campaign_id)
     params = {
         'date_preset': date_preset,
     }
     insights = campaigns.get_insights(params=params,
                                       fields=list(GENERAL_FIELD.values()) +
                                       list(TARGET_FIELD.values()))
     if len(insights) > 0:
         current_campaign = insights[0]
         if current_campaign.get(Field.impressions):
             spend = current_campaign.get(Field.spend)
             impressions = current_campaign.get(Field.impressions)
             self.campaign_insights.update({Field.spend: int(spend)})
             self.campaign_insights.update(
                 {Field.impressions: int(impressions)})
             try:
                 for act in current_campaign.get(Field.actions):
                     if act["action_type"] in LEAD_METRICS:
                         self.campaign_insights.update({
                             LEAD_METRICS[act["action_type"]]:
                             int(act["value"])
                         })
                         self.campaign_insights.update({
                             'cost_per_' + LEAD_METRICS[act["action_type"]]:
                             float(spend) / float(act["value"])
                         })
             except Exception as e:
                 print(
                     '[conversion_index_collector.Campaigns.get_campaign_insights]',
                     e)
         return self.campaign_insights
 def get_campaign_features(self):
     ad_campaign = campaign.Campaign(self.campaign_id)
     adcamps = ad_campaign.remote_read(fields=list(CAMPAIGN_FIELD.values()))
     for campaign_field in list(adcamps.keys()):
         self.campaign_features.update(
             {campaign_field: adcamps.get(campaign_field)})
     return self.campaign_features
Пример #3
0
 def get_adsets(self):
     adset_list = list()
     camp = campaign.Campaign(self.campaign_id)
     adsets = camp.get_ad_sets(fields=[adset.AdSet.Field.id])
     for adset_id in adsets:
         adset_list.append(adset_id.get("id"))
     return adset_list
Пример #4
0
 def get_campaign_insights(self, date_preset=None):
     camp = campaign.Campaign(self.campaign_id)
     params = {
         'date_preset': date_preset,
     }
     insights = camp.get_insights(params=params,
                                  fields=list(GENERAL_FIELD.values()) +
                                  list(TARGET_FIELD.values()))
     self.campaign_insights.update({Field.cost_per_target: 0})
     self.campaign_insights.update({Field.target: 0})
     if len(insights) > 0:
         current_campaign = insights[0]
         if current_campaign.get(Field.impressions):
             spend = int(current_campaign.get(Field.spend))
             try:
                 for act in current_campaign.get(Field.actions):
                     if act["action_type"] == CAMPAIGN_OBJECTIVE_FIELD[
                             self.charge_type]:
                         target = int(act["value"])
                         self.campaign_insights.update(
                             {Field.target: target})
                         self.campaign_insights.update(
                             {Field.cost_per_target: spend / target})
             except Exception as e:
                 print(
                     '[facebook_datacollector.Campaigns.get_campaign_insights]',
                     e)
             for campaign_field in list(GENERAL_FIELD.keys()):
                 self.campaign_insights.update(
                     {campaign_field: current_campaign.get(campaign_field)})
             if self.charge_type != 'ALL_CLICKS':
                 '''clicks and cpc is only for ALL_CLICKS'''
                 self.campaign_insights.pop(Field.clicks)
                 self.campaign_insights.pop(Field.cpc)
             else:
                 '''assign to field target and cost_per_target'''
                 self.campaign_insights[
                     Field.target] = self.campaign_insights.pop(
                         Field.clicks)
                 self.campaign_insights[
                     Field.cost_per_target] = self.campaign_insights.pop(
                         Field.cpc)
         return self.campaign_insights
Пример #5
0
 def get_account_id(self):
     camp = campaign.Campaign(self.campaign_id)
     account = camp.get_insights(fields=[campaign.CampaignField.account_id])
     current_account = account[0]
     return current_account.get(Field.account_id)