Exemplo n.º 1
0
def view_campaign(request):
    FacebookAdsApi.init(access_token=access_token,
                        app_id=app_id,
                        app_secret=app_secret)

    my_account = AdAccount('act_' + user_id)

    campaigns = my_account.get_campaigns()
    camp_id = []
    for i in range(len(campaigns)):
        camp_id.append(campaigns[i]["id"])
    campaign_data = []
    for id in camp_id:
        campaign = Campaign(fbid=id)
        fields = [
            Campaign.Field.id,
            Campaign.Field.name,
            Campaign.Field.status,
        ]
        campaign.remote_read(fields=fields)

        result = {}
        result["id"] = campaign[Campaign.Field.id]
        result["name"] = campaign[Campaign.Field.name]
        result["status"] = campaign[Campaign.Field.status]
        result["data_1"] = "ACTIVE"
        result["data_2"] = "PAUSED"
        campaign_data.append(result)

    context = {'campaigns': campaign_data}

    return render(request, 'fbapp/view_campaign.html', context)
Exemplo n.º 2
0
    def get_ad_campaign(self, cache, campaign_id):
        """
        Get the ad campaign. As some ad sets being analyzed belong to the
        same ad campaign, a caching is used to reduce the API calls.

        Params:

        * `cache` ad campaigns obtained already.
        * `campaign_id` the id of the ad campaign to be queried out.
        """

        if (cache.get(campaign_id) is None):
            campaign = Campaign(fbid=campaign_id)
            campaign.remote_read(fields=[
                Campaign.Field.name,
                Campaign.Field.configured_status,
                Campaign.Field.objective,
            ])
            cache[campaign_id] = campaign
        return cache.get(campaign_id)
Exemplo n.º 3
0
    def retrieve_eligible_adsets_for_an(
        self,
        accountid,
        includepaused=False,
    ):
        """
        This method returns all eligible ad sets that can have audience
        networked turned on for a given ad account id.

        Args:
            accountid: The ad account id (should be of the form act_<act_id>)
                for which you are running this exercise.
            inlcudepaused: Boolen parameter to make your method consider ad
                sets with paused states (PAUSED & CAMPAIGN_PAUSED). Checks
                only ACTIVE by default.

        Returns:
            List of ad set objects (if found satisfying the conditions) or
            an empty list.

        """
        # use accountid to retrieve all active adsets
        account = AdAccount(accountid)
        adsetfields = [
            AdSet.Field.id,
            AdSet.Field.name,
            AdSet.Field.campaign_id,
            AdSet.Field.targeting,
            AdSet.Field.effective_status,
        ]
        adsets = list(account.get_ad_sets(fields=adsetfields))

        # Filter ad sets received by desired status and placement types.
        # Further filter by campaign objectives listed in the criteria below.
        #
        # Ref: https://developers.facebook.com/docs/
        #               marketing-api/audience-network/v2.5

        desired_campaign_status = set(['ACTIVE'])

        # mostly useful in testing when you don't have active campaigns
        if includepaused is True:
            desired_campaign_status.update({'PAUSED', 'CAMPAIGN_PAUSED'})

        desired_campaign_objectives = set([
            'MOBILE_APP_INSTALLS',
            'MOBILE_APP_ENGAGEMENT',
            'LINK_CLICKS',
            'CONVERSIONS',
            'PRODUCT_CATALOG_SALES',
        ])

        # Hold the result set
        eligible_adsets = []

        for adset in adsets:
            if adset[AdSet.Field.effective_status] in desired_campaign_status:
                """
                'devide_platforms', 'publisher_platforms' and
                'facebook_positions' could be absent for the default of 'ALL'
                """
                device_platforms = None
                if Targeting.Field.device_platforms in \
                        adset[AdSet.Field.targeting]:
                    device_platforms = set(adset[AdSet.Field.targeting][
                        Targeting.Field.device_platforms])

                publisher_platforms = None
                if Targeting.Field.publisher_platforms in \
                        adset[AdSet.Field.targeting]:
                    publisher_platforms = set(adset[AdSet.Field.targeting][
                        Targeting.Field.publisher_platforms])

                facebook_positions = None
                if Targeting.Field.facebook_positions in \
                        adset[AdSet.Field.targeting]:
                    facebook_positions = set(adset[AdSet.Field.targeting][
                        Targeting.Field.facebook_positions])

                if ((facebook_positions is None
                     or 'feed' in facebook_positions)
                        and (device_platforms is None
                             or 'mobile' in device_platforms)):

                    if (publisher_platforms is None
                            or 'audience_network' in publisher_platforms):
                        # audience network already enabled, so just pass
                        continue
                    else:
                        campaign = Campaign(adset[AdSet.Field.campaign_id])
                        campaignfields = [
                            Campaign.Field.id,
                            Campaign.Field.name,
                            Campaign.Field.effective_status,
                            Campaign.Field.objective,
                        ]
                        campaign = campaign.remote_read(fields=campaignfields)
                        if (campaign[Campaign.Field.objective]
                                in desired_campaign_objectives):
                            eligible_adsets.append(adset)

        return eligible_adsets
Exemplo n.º 4
0
 def readCampaign(self, campaign_id):
     campaign = Campaign(fbid=campaign_id)
     campaign.remote_read(fields=[Campaign.Field.id, Campaign.Field.name])
     return campaign
 def get_campaign_features(self):
     ad_campaign = Campaign(self.campaign_id)
     adcamps = ad_campaign.remote_read(fields=list(campaign_field.values()))
     for k in list(adcamps.keys()):
         self.campaign_features.update({k: adcamps.get(k)})
     return self.campaign_features