コード例 #1
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,
コード例 #2
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)
コード例 #3
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'])
コード例 #4
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)
コード例 #5
0
    end = remove_minutes(end) + timedelta(hours=1)
    return start, end


# Date range for analytics request
start, end = date_range(active_entities)

# Analytics request for specific Line Item IDs
# Granularity is set to `HOUR`
# Based on the activity start and end times
# only one hour of data is being requested
# which is why the arrays have a single element
LineItem.all_stats(account,
                   ids,
                   metric_groups,
                   granularity=granularity,
                   placement=placement,
                   start_time=start,
                   end_time=end)
"""
[
  {
    "id": "eeukx",
    "id_data": [
      {
        "metrics": {
          "app_clicks": None,
          "card_engagements": None,
          "carousel_swipes": None,
          "clicks": [
            71