コード例 #1
0
ファイル: lineitems.py プロジェクト: ryankicks/data-ads-demo
def new(request):
    """
    Returns a new line item
    """
    client = Client(
        settings.SOCIAL_AUTH_TWITTER_KEY,
        settings.SOCIAL_AUTH_TWITTER_SECRET,
        settings.TWITTER_ACCESS_TOKEN,
        settings.TWITTER_ACCESS_TOKEN_SECRET)
    account_id = request.GET.get("account_id", "")
    campaign_id = request.GET.get("campaign_id", "")
    campaign_name = request.GET.get("name", "")
    budget = request.GET.get("budget", "")
    account_id = request.GET.get("account_id", "")
    name = request.GET.get("name", "")
    bid_amount = request.GET.get("bid_amount", "")

    json_data = {}
    try:
        account = client.accounts(account_id)
        # create your campaign
        line_item = LineItem(account)
        line_item.campaign_id = campaign_id
        line_item.name = name
        line_item.product_type = PRODUCT.PROMOTED_TWEETS
        line_item.placements = [PLACEMENT.ALL_ON_TWITTER]
        line_item.objective = OBJECTIVE.TWEET_ENGAGEMENTS
        line_item.bid_amount_local_micro = int(bid_amount) * 1000
        line_item.paused = True
        line_item.save()
        json_data = {
            "account_id": account_id,
            "campaign_name": campaign_name,
            "campaign_id": campaign_id}
    except Error as e:
        json_data["response"] = e.details
        json_data["valid"] = False

        pass
    return HttpResponse(json.dumps(json_data), content_type="application/json")
コード例 #2
0
def test_line_item_load():
    responses.add(responses.GET,
                  with_resource('/' + API_VERSION + '/accounts/2iqph'),
                  body=with_fixture('accounts_load'),
                  content_type='application/json')

    responses.add(responses.GET,
                  with_resource('/' + API_VERSION + '/accounts/2iqph/line_items/bw2'),
                  body=with_fixture('line_items_load'),
                  content_type='application/json')

    client = Client(
        characters(40),
        characters(40),
        characters(40),
        characters(40)
    )

    account = Account.load(client, '2iqph')

    line_item = LineItem.load(account, 'bw2')
    assert line_item.id == 'bw2'
    assert line_item.entity_status == 'ACTIVE'
コード例 #3
0
account_media.creative_type = CREATIVE_TYPE.PREROLL
account_media.save()

# create a campaign
campaign = Campaign(account)
campaign.name = "Video tutorial test"
# get the first funding instrument on the account
campaign.funding_instrument_id = account.funding_instruments().first.id
campaign.daily_budget_amount_local_micro = 1000000000
campaign.entity_status = ENTITY_STATUS.PAUSED
campaign.start_time = datetime.utcnow()
campaign.save()

# create a line item with the VIDEO_VIEWS_PREROLL
# objective and product_type MEDIA
line_item = LineItem(account)
line_item.objective = OBJECTIVE.VIDEO_VIEWS_PREROLL

line_item.campaign_id = campaign.id
line_item.name = 'Video tutorial example'
line_item.product_type = 'MEDIA'
line_item.placements = [PLACEMENT.ALL_ON_TWITTER]
line_item.bid_amount_local_micro = 1000000
line_item.entity_status = ENTITY_STATUS.PAUSED
line_item.categories = 'IAB1'
line_item.save()

from twitter_ads.http import Request

resource = '/' + API_VERSION + '/accounts/18ce54bgxky/preroll_call_to_actions'.format(
    account_id=account.id)
コード例 #4
0
# load the advertiser account instance
account = client.accounts(ACCOUNT_ID)

# limit request count and grab the first 10 line items from Cursor
line_items = list(account.line_items(None, count=10))[:10]

# the list of metrics we want to fetch, for a full list of possible metrics
# see: https://dev.twitter.com/ads/analytics/metrics-and-segmentation
metric_groups = [METRIC_GROUP.BILLING]

# fetching stats on the instance
line_items[0].stats(metric_groups)

# fetching stats for multiple line items
ids = map(lambda x: x.id, line_items)
LineItem.all_stats(account, ids, metric_groups)

# fetching async stats on the instance
queued_job = LineItem.queue_async_stats_job(account, ids, metric_groups)

# get the job_id:
job_id = queued_job['id']

# let the job complete
seconds = 15
time.sleep(seconds)

async_stats_job_result = LineItem.async_stats_job_result(account,
                                                         [job_id]).first

async_data = LineItem.async_stats_job_data(account,
コード例 #5
0
campaign_2.daily_budget_amount_local_micro = 2000000
campaign_2.name = "my second campaign"
campaign_2.paused = True
campaign_2.start_time = datetime.utcnow()

campaigns_list = [campaign_1, campaign_2]
Campaign.batch_save(account, campaigns_list)

# modify the created campaigns
campaign_1.name = "my modified first campaign"
campaign_2.name = "my modified second campaign"

Campaign.batch_save(account, campaigns_list)

# create line items for campaign_1
line_item_1 = LineItem(account)
line_item_1.campaign_id = campaign_1.id
line_item_1.name = "my first ad"
line_item_1.product_type = PRODUCT.PROMOTED_TWEETS
line_item_1.placements = [PLACEMENT.ALL_ON_TWITTER]
line_item_1.objective = OBJECTIVE.TWEET_ENGAGEMENTS
line_item_1.bid_amount_local_micro = 10000
line_item_1.paused = True

line_item_2 = LineItem(account)
line_item_2.campaign_id = campaign_1.id
line_item_2.name = "my second ad"
line_item_2.product_type = PRODUCT.PROMOTED_TWEETS
line_item_2.placements = [PLACEMENT.ALL_ON_TWITTER]
line_item_2.objective = OBJECTIVE.TWEET_ENGAGEMENTS
line_item_2.bid_amount_local_micro = 20000
コード例 #6
0
campaign_2.daily_budget_amount_local_micro = 2000000
campaign_2.name = 'my second campaign'
campaign_2.paused = True
campaign_2.start_time = datetime.utcnow()

campaigns_list = [campaign_1, campaign_2]
Campaign.batch_save(account, campaigns_list)

# modify the created campaigns
campaign_1.name = 'my modified first campaign'
campaign_2.name = 'my modified second campaign'

Campaign.batch_save(account, campaigns_list)

# create line items for campaign_1
line_item_1 = LineItem(account)
line_item_1.campaign_id = campaign_1.id
line_item_1.name = 'my first ad'
line_item_1.product_type = PRODUCT.PROMOTED_TWEETS
line_item_1.placements = [PLACEMENT.ALL_ON_TWITTER]
line_item_1.objective = OBJECTIVE.TWEET_ENGAGEMENTS
line_item_1.bid_amount_local_micro = 10000
line_item_1.paused = True

line_item_2 = LineItem(account)
line_item_2.campaign_id = campaign_1.id
line_item_2.name = 'my second ad'
line_item_2.product_type = PRODUCT.PROMOTED_TWEETS
line_item_2.placements = [PLACEMENT.ALL_ON_TWITTER]
line_item_2.objective = OBJECTIVE.TWEET_ENGAGEMENTS
line_item_2.bid_amount_local_micro = 20000
コード例 #7
0
                ACCESS_TOKEN_SECRET)

# load the advertiser account instance
account = client.accounts(ACCOUNT_ID)

# create your campaign
campaign = Campaign(account)
campaign.funding_instrument_id = account.funding_instruments().next().id
campaign.daily_budget_amount_local_micro = 1000000
campaign.name = 'my first campaign'
campaign.entity_status = ENTITY_STATUS.PAUSED
campaign.start_time = datetime.utcnow()
campaign.save()

# create a line item for the campaign
line_item = LineItem(account)
line_item.campaign_id = campaign.id
line_item.name = 'my first ad'
line_item.product_type = PRODUCT.PROMOTED_TWEETS
line_item.placements = [PLACEMENT.ALL_ON_TWITTER]
line_item.objective = OBJECTIVE.TWEET_ENGAGEMENTS
line_item.bid_amount_local_micro = 10000
line_item.entity_status = ENTITY_STATUS.PAUSED
line_item.save()

# add targeting criteria
targeting_criteria = TargetingCriteria(account)
targeting_criteria.line_item_id = line_item.id
targeting_criteria.targeting_type = 'LOCATION'
targeting_criteria.targeting_value = '00a8b25e420adc94'
targeting_criteria.save()
コード例 #8
0
# https://dev.twitter.com/ads/analytics/metrics-derived

from twitter_ads.client import Client
from twitter_ads.campaign import LineItem

CONSUMER_KEY = 'your consumer key'
CONSUMER_SECRET = 'your consumer secret'
ACCESS_TOKEN = 'access token'
ACCESS_TOKEN_SECRET = 'access token secret'
ACCOUNT_ID = 'account id'

# initialize the client
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

# load the advertiser account instance
account = client.accounts(ACCOUNT_ID)

# limit request count and grab the first 10 line items from Cursor
line_items = list(account.line_items(None, count=10))[:10]

# the list of metrics we want to fetch, for a full list of possible metrics
# see: https://dev.twitter.com/ads/analytics/metrics-and-segmentation
metrics = ['billed_engagements', 'billed_follows']

# fetching stats on the instance
line_items[0].stats(metrics)

# fetching stats for multiple line items
ids = map(lambda x: x.id, line_items)
LineItem.all_stats(account, ids, metrics)
コード例 #9
0
ファイル: test2.py プロジェクト: wangchenshu/test
# load the advertiser account instance
account = client.accounts(ACCOUNT_ID)

# create your campaign
campaign = Campaign(account)
campaign.funding_instrument_id = account.funding_instruments().next().id
campaign.daily_budget_amount_local_micro = 1000000
campaign.name = 'my first campaign'
campaign.paused = True
campaign.start_time = datetime.datetime.utcnow()
campaign.save()


# create a line item for the campaign
line_item = LineItem(account)
line_item.campaign_id = campaign.id
line_item.name = 'my first ad'
line_item.product_type = PRODUCT.PROMOTED_TWEETS
line_item.placements = [PLACEMENT.ALL_ON_TWITTER]
line_item.objective = OBJECTIVE.TWEET_ENGAGEMENTS
line_item.bid_amount_local_micro = 10000
line_item.paused = True
line_item.save()

# add targeting criteria
targeting_criteria = TargetingCriteria(account)
targeting_criteria.line_item_id = line_item.id
targeting_criteria.targeting_type = 'LOCATION'
targeting_criteria.targeting_value = '00a8b25e420adc94'
targeting_criteria.save()
コード例 #10
0
account_media.creative_type = CREATIVE_TYPE.PREROLL
account_media.save()

# create a campaign
campaign = Campaign(account)
campaign.name = "Video tutorial test"
# get the first funding instrument on the account
campaign.funding_instrument_id = account.funding_instruments().first.id
campaign.daily_budget_amount_local_micro = 1000000000
campaign.paused = True
campaign.start_time = datetime.utcnow()
campaign.save()

# create a line item with the VIDEO_VIEWS_PREROLL
# objective and product_type MEDIA
line_item = LineItem(account)
line_item.objective = OBJECTIVE.VIDEO_VIEWS_PREROLL

line_item.campaign_id = campaign.id
line_item.name = "Video tutorial example"
line_item.product_type = "MEDIA"
line_item.placements = [PLACEMENT.ALL_ON_TWITTER]
line_item.bid_amount_local_micro = 1000000
line_item.paused = True
line_item.categories = "IAB1"
line_item.save()

from twitter_ads.http import Request

resource = "/1/accounts/18ce54bgxky/preroll_call_to_actions".format(account_id=account.id)
params = {
コード例 #11
0
def new(request):
    """
    Returns a new line item
    """
    client = Client(settings.SOCIAL_AUTH_TWITTER_KEY,
                    settings.SOCIAL_AUTH_TWITTER_SECRET,
                    settings.TWITTER_ACCESS_TOKEN,
                    settings.TWITTER_ACCESS_TOKEN_SECRET)
    account_id = request.GET.get("account_id", "")
    campaign_id = request.GET.get("campaign_id", "")
    campaign_name = request.GET.get("name", "")
    budget = request.GET.get("budget", "")
    account_id = request.GET.get("account_id", "")
    name = request.GET.get("name", "")
    bid_amount = request.GET.get("bid_amount", "")

    json_data = {}
    try:
        account = client.accounts(account_id)
        # create your campaign
        line_item = LineItem(account)
        line_item.campaign_id = campaign_id
        line_item.name = name
        line_item.product_type = PRODUCT.PROMOTED_TWEETS
        line_item.placements = [PLACEMENT.ALL_ON_TWITTER]
        line_item.objective = OBJECTIVE.TWEET_ENGAGEMENTS
        line_item.bid_amount_local_micro = int(bid_amount) * 1000
        line_item.paused = True
        line_item.save()
        json_data = {
            "account_id": account_id,
            "campaign_name": campaign_name,
            "campaign_id": campaign_id
        }
    except Error as e:
        json_data["response"] = e.details
        json_data["valid"] = False

        pass
    return HttpResponse(json.dumps(json_data), content_type="application/json")
コード例 #12
0
metric_groups = [METRIC_GROUP.BILLING]

# fetching stats on the instance
line_items[0].stats(metric_groups)

# fetching stats for multiple line items
ids = list(map(lambda x: x.id, line_items))
if not ids:
    print('Error: A minimum of 1 items must be provided for entity_ids')
    sys.exit()

sync_data = []
# Sync/Async endpoint can handle max 20 entity IDs per request
# so split the ids list into multiple requests
for chunk_ids in split_list(ids, 20):
    sync_data.append(LineItem.all_stats(account, chunk_ids, metric_groups))

print(sync_data)

# create async stats jobs and get job ids
queued_job_ids = []
for chunk_ids in split_list(ids, 20):
    queued_job_ids.append(
        LineItem.queue_async_stats_job(account, chunk_ids, metric_groups).id)

print(queued_job_ids)

# let the job complete
seconds = 30
time.sleep(seconds)
コード例 #13
0
# load the advertiser account instance
account = client.accounts(ACCOUNT_ID)

# limit request count and grab the first 10 line items from Cursor
line_items = list(account.line_items(None, count=10))[:10]

# the list of metrics we want to fetch, for a full list of possible metrics
# see: https://dev.twitter.com/ads/analytics/metrics-and-segmentation
metric_groups = [METRIC_GROUP.BILLING]

# fetching stats on the instance
line_items[0].stats(metric_groups)

# fetching stats for multiple line items
ids = map(lambda x: x.id, line_items)
LineItem.all_stats(account, ids, metric_groups)

# fetching async stats on the instance
queued_job = LineItem.queue_async_stats_job(account, ids, metric_groups)

# get the job_id:
job_id = queued_job['id']

# let the job complete
seconds = 15
time.sleep(seconds)

async_stats_job_result = LineItem.async_stats_job_result(account, job_id)

async_data = LineItem.async_stats_job_data(account, async_stats_job_result['url'])
コード例 #14
0
                ACCESS_TOKEN_SECRET)

# load the advertiser account instance
account = client.accounts(ACCOUNT_ID)

# analytics request parameters
metric_groups = [METRIC_GROUP.ENGAGEMENT]
granularity = GRANULARITY.HOUR
placement = PLACEMENT.ALL_ON_TWITTER

# For checking the active entities endpoint for the last day
end_time = datetime.utcnow().date()
start_time = end_time - timedelta(days=1)

# Active Entities for Line Items
active_entities = LineItem.active_entities(account, start_time, end_time)

# Entity IDs to fetch analytics data for
# Note: analytics endpoints support a
# maximum of 20 entity IDs per request
ids = [d['entity_id'] for d in active_entities]


# Function for determining the start and end time
# to be used in the subsequent analytics request
def date_range(data):
    """Returns the minimum activity start time and the maximum activity end time
    from the active entities response.

    This function assumes that the subsequent analytics request will use `HOUR`
    granularity. Thus, minutes (and seconds and so on) are removed from the