Ejemplo n.º 1
0
    def enable_an_on_adsets(self, adsetids):
        """
        Method that takes a list of ad set ids and enables the audience network
        placement on them. Please note that this method does not perform any
        pre-validation check to see whether the ad set passed  satisfies the
        pre-requisites to have audience network enabled.

        Args:
            adsetids: List of ad set ids on which you want audience network
            enabled. Even if you have just one id, pass it as a list.
        Returns:
            A list of 'ad set id' vs. 'status' mapping with a further 'message'
            node whenever there was a failure to update the audience network.
            Returns an empty list when an empty list or non list element is
            passed.

            status 1 is a success.
            status 0 is a failure.

            Sample response format:
            [
                {'<ad_set_id_1>': {'status': 1}},
                {'<ad_set_id_2>': {'status': 0, 'message': '<exception_msg>'}},
                ...
            ]
        """
        results = []

        # check if adsets is a list
        if type(adsetids) is not list:
            return results

        # go over the list of adset ids
        for adsetid in adsetids:
            try:
                # read ad set info
                adset = AdSet(fbid=adsetid)
                adsetobj = adset.remote_read(fields=[AdSet.Field.targeting])

                # edit targeting spec info for placements
                targetinginfo = copy.deepcopy(adsetobj[AdSet.Field.targeting])
                targetinginfo[TargetingSpecsField.publisher_platforms]. \
                    append('audience_network')

                # update ad set info
                adset.update({AdSet.Field.targeting: targetinginfo})
                adset.remote_update()

                # update result list along with status
                results.append({adsetid: {'status': 1}})

            except FacebookError as e:

                # update result list along with status & message
                results.append({adsetid: {'status': 0, 'message': e.message}})

        return results
# As with any software that integrates with the Facebook platform, your use
# of this software is subject to the Facebook Developer Principles and
# Policies [http://developers.facebook.com/policy/]. This copyright notice
# shall be included in all copies or substantial portions of the software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from examples.docs import fixtures
from facebookads import test_config

ad_account_id = test_config.account_id
ad_set_id = fixtures.create_adset().get_id()

# !_DOC [pruno]
# _DOC open [ADSET_UPDATE_PACING_TYPE_STANDARD]
# _DOC vars [ad_account_id:s, ad_set_id]
from facebookads.objects import AdSet

adset = AdSet(fbid=ad_set_id, parent_id=ad_account_id)
adset.update({
    AdSet.Field.pacing_type: [AdSet.PacingType.standard],
})
adset.remote_update()
# _DOC close [ADSET_UPDATE_PACING_TYPE_STANDARD]