Finding which campaign an active line item belongs to

I have found all active line items using:

active_entities = LineItem.active_entities(account, start_time, end_time)
How can I get the campaign that is associated with each of these line items? I know that if you have a LineItem object you can get the campaign_id from there. However when you get all the active entities from here, you get a dictionary, not a list of line items. Further, what’s the best way to get the actual campaign name from a campaign_id?


You may want to read this topic as it has a bit similar context there.

If I understand your question correctly, your questions are:

  1. How to get a campaign_id from a line_item_id
  2. How to get a campaign name from a camlaign_id
Correct? So after you fetched the active line items using the Active Entities endpoint, it’s possible to get associated parent campaign’s metadata using GET accounts/:account_id/line_items and GET accounts/:account_id/campaigns endpoint. For instance, you can extract all line_item_id from the response (dict) of the Active Entities endpoint call, and then pass a list of the line_item_ids (up to 200 ids) using line_item_ids parameter of the GET accounts/:account_id/line_items endpoint. Then, you can get all the associated parent campaign_id from its response. Finally, you can retrieve campaign name of those campaign ids using campaign_ids parameter of the GET accounts/:account_id/campaigns.

Which attribute of line item ids does that go under?
When I run:
LineItem.all_stats(account, ids[:20], metric_groups, granularity=granularity, placement=placement, start_time=start, end_time=end)
I get:

  {'id': 'XXXXX',
'id_data': [{'segment': None,
'metrics': {'impressions': [2, 2, 0],
'tweets_send': None,
'qualified_impressions': None,
'follows': None,
'app_clicks': None,
'retweets': None,
'unfollows': None,
'likes': None,
'engagements': [0, 1, 0],
'clicks': [0, 1, 0],
'card_engagements': None,
'poll_card_vote': None,
'replies': None,
'url_clicks': None,
'carousel_swipes': None}}]}

Which doesn't have a campaign id in it.

I spoke to Carmen and got my previous question answered.
However, I have a python specific question.
How do I modify this code to be only for the past day:
LineItem.stats(billing, granularity=“DAY”)[0][‘id_data’][0][‘metrics’][‘billed_charge_local_micro’]

@Mantra Please use start_time and end_time parameters.