Last active
November 7, 2017 19:49
-
-
Save rafaelverger/ebc0bd7863fc29d8741043892c9c97d3 to your computer and use it in GitHub Desktop.
DFP - Create Prebid LineItems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from googleads import dfp | |
DFP_CLIENT = dfp.DfpClient.LoadFromStorage() | |
ORDER_SERVICE = DFP_CLIENT.GetService('OrderService', version='v201705') | |
LI_SERVICE = DFP_CLIENT.GetService('LineItemService', version='v201705') | |
CTGT_SERVICE = DFP_CLIENT.GetService('CustomTargetingService', version='v201705') | |
LICA_SERVICE = DFP_CLIENT.GetService('LineItemCreativeAssociationService', version='v201705') | |
PREBID_CREATIVE_IDS = [1234567890, 9087654321, 1357924680, 2468013579, 1234509876] | |
CREATIVE_SIZES = [{'width': 970, 'height': 250}, {'width': 300, 'height': 600}] | |
ADUNITS_SPOTS = [{'adUnitId': '123123123'}, {'adUnitId': '456456456'}] | |
HBPB_KEY = 33333333 # targeting key for Header Bidding Price Bucket | |
def frange(start, stop, step): | |
x = start | |
while x < stop: | |
yield x | |
x += step | |
def get_hbpb_values(tgts = [], last = 0): | |
rsp = CTGT_SERVICE.getCustomTargetingValuesByStatement( | |
dfp.FilterStatement( | |
"WHERE customTargetingKeyId = %d AND status = 'ACTIVE' AND id > %d ORDER BY id ASC" % (HBPB_KEY, last) | |
).ToStatement() | |
) | |
print 'Found %d results' % rsp.totalResultSetSize | |
if 'results' in rsp: | |
return get_hbpb_values(tgts + rsp['results'], rsp['results'][-1].id) | |
return tgts | |
HB_PB_VALUES = {tgt.name: tgt.id for tgt in get_hbpb_values()} | |
def build_prebid_li(cpm, currency, orderId): | |
return { | |
'name': 'Prebid - CPM %s [%s]' % (cpm, currency), | |
'orderId': orderId, | |
'startDateTimeType': 'IMMEDIATELY', | |
'unlimitedEndDateTime': True, | |
'creativeRotationType': 'EVEN', | |
'deliveryRateType': 'EVENLY', | |
'roadblockingType': 'ONE_OR_MORE', | |
'lineItemType': 'PRICE_PRIORITY', | |
'priority': 12, | |
'costPerUnit': { | |
'currencyCode': currency, | |
'microAmount': int(float(cpm)*1e6) | |
}, | |
'costType': 'CPM', | |
'creativePlaceholders': [{'size': size} for size in CREATIVE_SIZES], | |
'creativePersistenceType': 'NOT_PERSISTENT', | |
'allowOverbook': False, | |
'disableSameAdvertiserCompetitiveExclusion': False, | |
'primaryGoal': { | |
'goalType': 'NONE', | |
'unitType': 'IMPRESSIONS', | |
'units': -1 | |
}, | |
'targeting': { | |
'inventoryTargeting': { | |
'targetedAdUnits': ADUNITS_SPOTS, | |
}, | |
'customTargeting': { | |
'xsi_type': 'CustomCriteriaSet', | |
'logicalOperator': 'AND', | |
'children': [{ | |
'xsi_type': 'CustomCriteria', | |
'keyId': HBPB_KEY, | |
'valueIds': [HB_PB_VALUES[cpm]], | |
'operator': 'IS' | |
}] | |
} | |
} | |
} | |
def build_prebid_lis(cpmStart, cpmStop, cpmStep, currency, orderId): | |
return [ | |
build_prebid_li('%.2f' % (cpm/100.0), currency, orderId) | |
for cpm in frange(cpmStart, cpmStop, cpmStep) | |
] | |
def create_prebid_lis(prebid_lis) | |
created_lis = [] | |
for bkt in [prebid_lis[i:i + 50] for i in xrange(0, len(prebid_lis), 50)]: | |
print 'creating %d line items:\n%s\n- - - - - - - - - - -' % (len(bkt), '\n'.join(map(lambda li: li['name'], bkt))) | |
created_lis += LI_SERVICE.createLineItems(bkt) | |
return created_lis | |
def create_licas(created_lis): | |
licas = [] | |
for li in created_lis: | |
licas += [{ | |
'creativeId': cid, | |
'lineItemId': li.id, | |
'sizes': CREATIVE_SIZES | |
} for cid in PREBID_CREATIVE_IDS] | |
# send licas to dfp service | |
licas = LICA_SERVICE.createLineItemCreativeAssociations(licas) | |
def create_order(name, advertiserId, traffickerId): | |
return ORDER_SERVICE.createOrders({ | |
'name': name, 'advertiserId': advertiserId, 'traffickerId': traffickerId | |
})[0] | |
# executing; max line items per order = 450; CPM in cents | |
prebid_params = [ | |
{'cpmStart': 1, 'cpmStop': 451, 'cpmStep': 1, 'currency': 'USD'}, | |
{'orderId': 135135246246, 'cpmStart': 455, 'cpmStop': 2705, 'cpmStep': 5, 'currency': 'USD'}, | |
# if you wanna map CPMs above $2700 | |
#{'orderId': 109109287287, 'cpmStart': 2710, 'cpmStop': 7210, 'cpmStep': 10, 'currency': 'USD'}, | |
#{'orderId': 163163948948, 'cpmStart': 7220, 'cpmStop': 16220, 'cpmStep': 20, 'currency': 'USD'}, | |
#{'orderId': 163163948948, 'cpmStart': 16250, 'cpmStop': 38750, 'cpmStep': 50, 'currency': 'USD'}, | |
] | |
for params in prebid_params: | |
if 'orderId' not in params: | |
params['orderId'] = create_order( | |
'Prebid - CPMs %.2f to %.2f [%s]' % ( | |
params['cpmStart']/100.0, | |
(params['cpmStop']-params['cpmStep'])/100.0, | |
params['currency'] | |
), 4411568867, 113888785 | |
).id | |
prebid_lis = build_prebid_lis(**params) | |
created_lis = create_prebid_lis(prebid_lis) | |
created_licas = create_licas(created_lis) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from googleads import dfp | |
DFP_CLIENT = dfp.DfpClient.LoadFromStorage() | |
LI_SERVICE = DFP_CLIENT.GetService('LineItemService', version='v201705') | |
PREBID_ORDERS = [11111111,22222222,33333333,44444444] | |
HB_BIDDER_KEY = 99999 | |
HB_BIDDERS = {'appnexus': 1234, 'aol': 2345, 'rubicon': 3456, 'criteo': 4567} | |
HB_BIDDER_TARGETING = { | |
'xsi_type': 'CustomCriteria', | |
'keyId': HB_BIDDER_KEY, | |
'valueIds': [HB_BIDDERS.values()], | |
'operator': 'IS' | |
} | |
def get_all_prebid_li(lis=[], last=0): | |
rs = LI_SERVICE.getLineItemsByStatement(dfp.FilterStatement( | |
"where orderId IN (%s) and status != 'INACTIVE' and id > %s ORDER BY id ASC" % (','.join(PREBID_ORDERS), last) | |
).ToStatement()) | |
if 'results' in rs: | |
print 'found %d results; last is %s' % (len(rs['results']), rs['results'][-1].id) | |
lis += rs['results'] | |
return get_all_prebid_li(lis, lis[-1].id) | |
return lis | |
def transform_li(li): | |
return { | |
'id': li.id, | |
'name': li.name, | |
'orderId': li.orderId, | |
'costPerUnit': li.costPerUnit, | |
'primaryGoal': li.primaryGoal, | |
'creativePlaceholders': li.creativePlaceholders, | |
'costType': li.costType, | |
'startDateTime': li.startDateTime, | |
'unlimitedEndDateTime': li.unlimitedEndDateTime, | |
'lineItemType': li.lineItemType, | |
'targeting': { | |
'inventoryTargeting': { | |
'targetedAdUnits': map(lambda o: { | |
'adUnitId': o.adUnitId, | |
'includeDescendants': o.includeDescendants | |
}, li.targeting.inventoryTargeting.targetedAdUnits), | |
}, | |
'customTargeting': { | |
'xsi_type': 'CustomCriteriaSet', | |
'logicalOperator': 'AND', | |
'children': [{ | |
'xsi_type': 'CustomCriteria', | |
'operator': 'IS', | |
'keyId': li.targeting.customTargeting.children[0].children[0].keyId, | |
'valueIds': li.targeting.customTargeting.children[0].children[0].valueIds | |
}, dict(HB_BIDDER_TARGETING)] | |
} | |
} | |
} | |
prebid_lis = sorted(get_all_prebid_li(), key=lambda li: li['costPerUnit'].microAmount) | |
updated_lis = [] | |
for bkt in [prebid_lis[i:i + 50] for i in xrange(0, len(prebid_lis), 50)]: | |
print 'udpating %d line items:\n%s\n- - - - - - - - - - -' % (len(bkt), '\n'.join(map(lambda li: li['name'], bkt))) | |
updated_lis += LI_SERVICE.updateLineItems(map(transform_li, bkt)) | |
print 'updated %d items' % len(updated_lis) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment