Activate and pause Google Display & Video 360 (DV360) line items, using the DV360 API and Python
This file contains 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
################################################### | |
# Author Krisjan Oldekamp / Stacktonic.com | |
# Email krisjan@stacktonic.com | |
# Article https://stacktonic.com/article/how-to-activate-and-pause-line-items-in-google-dv-360-using-python | |
#################################################### | |
import os | |
from urllib.error import HTTPError | |
from googleapiclient import discovery | |
from oauth2client.service_account import ServiceAccountCredentials | |
#################################################### | |
# Settings | |
#################################################### | |
GOOGLE_JSON_KEYFILE = "<your-keyfile>.json" # Google Cloud Platform Service Account JSON keyfile | |
GOOGLE_DV360_API_VERSION = "v1" | |
GOOGLE_DV360_API_SCOPES = ["https://www.googleapis.com/auth/display-video"] | |
DV360_ADVERTISER_ID = "<your-dv360-advertiser-id>" | |
#################################################### | |
# Google D&V360 API service | |
def get_dv360_service(): | |
credentials = ServiceAccountCredentials.from_json_keyfile_name( | |
GOOGLE_JSON_KEYFILE, | |
scopes=GOOGLE_DV360_API_SCOPES) | |
return discovery.build('displayvideo', GOOGLE_DV360_API_VERSION, credentials=credentials, cache_discovery=False) | |
# Change DV360 Line Item status | |
def update_lineitem_status(advertiser_id, line_item_id, status_new): | |
service = get_dv360_service() | |
status_dv360 = 'ENTITY_STATUS_ACTIVE' if status_new == 'ACTIVE' else 'ENTITY_STATUS_PAUSED' | |
line_item_obj = { | |
'entityStatus': status_dv360, | |
} | |
# Update the line item. | |
update_line_item = service.advertisers().lineItems().patch( | |
advertiserId=advertiser_id, | |
lineItemId=line_item_id, | |
updateMask="entityStatus", | |
body=line_item_obj | |
).execute() | |
print("Lineitem " + line_item_id + " updated to " + status_dv360) | |
# Activate a manual trigger | |
def manual_trigger(advertiser_id, trigger_id): | |
service = get_dv360_service() | |
# Activate the trigger | |
update_line_item = service.advertisers().manualTriggers().activate( | |
advertiserId=advertiser_id, | |
triggerId=trigger_id | |
).execute() | |
print("TriggerId " + trigger_id + " activated.") | |
# Usecase 1: Update lineitem status | |
line_item_id = "123" # DV360 Line-Item-ID | |
line_item_status_new = "ACTIVE" # New status -> ACTIVE or PAUSED | |
update_lineitem_status(DV360_ADVERTISER_ID, line_item_id, line_item_status_new) | |
# Usecase 2: Activate a manual trigger (create and attach a manual trigger in the DV360 interface first) | |
trigger_id = "123" | |
manual_trigger(DV360_ADVERTISER_ID, trigger_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment