Display & Video 360 API lets you fully manage custom bidding implementations. You can create custom bidding algorithms, upload and verify individual scripts, and assign a specific algorithm to a resource as its bidding strategy.
This page describes how to create, update, and assign custom bidding algorithms with the Display & Video 360 API. Each section provides a code sample.
Create a Custom Bidding Algorithm
A CustomBiddingAlgorithm
object represents an individual
algorithm that you can assign to a line item for use in its bid strategy. This
object has details on the algorithm, such as the
customBiddingAlgorithmType
and
entityStatus
, as well as the
readinessState
and suspensionState
for
each relevant advertiser's generated model. You can create
CustomBiddingScript
and
CustomBiddingAlgorithmRules
objects as child resources for the
algorithm to use.
Here's an example of how to create a script-based custom bidding algorithm:
Java
// Create the custom bidding algorithm structure.
CustomBiddingAlgorithm customBiddingAlgorithm =
new CustomBiddingAlgorithm()
.setAdvertiserId(advertiser-id)
.setDisplayName(display-name)
.setEntityStatus("ENTITY_STATUS_ACTIVE")
.setCustomBiddingAlgorithmType("SCRIPT_BASED");
// Configure the create request.
CustomBiddingAlgorithms.Create request =
service.customBiddingAlgorithms().create(customBiddingAlgorithm);
// Create the custom bidding algorithm.
CustomBiddingAlgorithm response = request.execute();
// Display the new custom bidding algorithm name.
System.out.printf(
"Custom bidding algorithm %s was created.%n",
response.getName()
);
Python
# Create a custom bidding algorithm object.
custom_bidding_algorithm_obj = {
'advertiserId': advertiser-id,
'displayName': display-name,
'entityStatus': 'ENTITY_STATUS_ACTIVE',
'customBiddingAlgorithmType': 'SCRIPT_BASED'
}
# Create the custom bidding algorithm.
response = service.customBiddingAlgorithms().create(
body=custom_bidding_algorithm_obj
).execute()
# Display the new custom bidding algorithm.
print(f'The following Custom Bidding Algorithm was created: {response}')
PHP
// Create a custom bidding algorithm object.
$customBiddingAlgorithm =
new Google_Service_DisplayVideo_CustomBiddingAlgorithm();
$customBiddingAlgorithm->setAdvertiserId(advertiser-id);
$customBiddingAlgorithm->setDisplayName(display-name);
$customBiddingAlgorithm->setEntityStatus('ENTITY_STATUS_ACTIVE');
$customBiddingAlgorithm->setCustomBiddingAlgorithmType('SCRIPT_BASED');
// Create the custom bidding algorithm.
$result =
$this->service->customBiddingAlgorithms->create($customBiddingAlgorithm);
// Display the new custom bidding algorithm name.
printf('Custom Bidding Algorithm %s was created.\n', $result['name']);
Manage algorithm access
Custom bidding algorithms can be owned by a partner or an advertiser. Algorithms
owned by a partner can be accessed and modified by that partner and any child
advertisers listed in the sharedAdvertiserIds
field.
Algorithms owned by an advertiser can be accessed and modified by that
advertiser and its parent partner, but cannot be shared with other advertisers.
If you're only using the algorithm for a single advertiser, assign the
advertiser as the owner with the advertiserId
field.
Otherwise, assign the parent partner of the advertisers as the owner with the
partnerId
field, and give the advertisers access with the
sharedAdvertiserIds
field.
Upload algorithm logic
Depending on the type of your custom bidding algorithm, you next need to create either a script or a rules object where you can provide the logic for the algorithm to use.
Upload a script
Script-based custom bidding algorithms employ user-provided scripts to evaluate the worth of an impression. Samples of simple scripts and a list of advanced fields are available through the Display & Video 360 Help Center.
The following sections teach you how to add a new or updated script to a custom bidding algorithm.
Retrieve a script resource location
First, retrieve an available resource location under the custom bidding
algorithm resource with the
customBiddingAlgorithms.uploadScript
method. This
request returns a CustomBiddingScriptRef
object with a
resource name. You can
upload your script file to the location dictated by the
resource name. Then use the custom bidding script reference object to
create your script resource.
Here's an example of how to retrieve an available resource location:
Java
// Retrieve a usable custom bidding script
// reference.
CustomBiddingScriptRef scriptRef =
service
.customBiddingAlgorithms()
.uploadScript(custom-bidding-algorithm-id)
.setAdvertiserId(advertiser-id)
.execute();
// Display the custom bidding script reference resource path.
System.out.printf(
"The script can be uploaded to the following resource path: %s%n",
scriptRef.getResourceName()
);
Python
# Retrieve a usable custom bidding script reference
# object.
custom_bidding_script_ref = service.customBiddingAlgorithms().uploadScript(
customBiddingAlgorithmId=custom-bidding-algorithm-id,
advertiserId=advertiser-id
).execute()
# Display the new custom bidding script reference object.
print('The following custom bidding script reference object was retrieved:'
f'{custom_bidding_script_ref}')
PHP
// Set parent advertiser ID of custom bidding
// algorithm in optional parameters array for request.
$optParams = array('advertiserId' => advertiser-id);
// Retrieve a usable custom bidding script reference.
$scriptRefResponse = $this->service->customBiddingAlgorithms->uploadScript(
custom-bidding-algorithm-id,
$optParams
);
// Display the new custom bidding script reference object.
printf(
'The script can be uploaded to the following resource path: %s\n',
$scriptRefResponse->getResourceName()
);
Upload a script file
After retrieving an available resource location, upload your script file to that
location in the Display & Video 360 system with the
media.upload
method. This method supports a
simple upload that requires the query parameter
uploadType=media
.
Here's an example of how to upload a script file given a retrieved custom bidding script reference object:
Java
// Create media object.
GoogleBytestreamMedia media = new GoogleBytestreamMedia();
media.setResourceName(resource-name);
// Create input stream for the script file.
InputStreamContent scriptFileStream =
new InputStreamContent(
null, new FileInputStream(script-path));
// Create media.upload request.
Media.Upload uploadRequest =
service
.media()
.upload(
resource-name,
media,
scriptFileStream);
// Retrieve uploader from the request and set it to us a simple
// upload request.
MediaHttpUploader uploader = uploadRequest.getMediaHttpUploader();
uploader.setDirectUploadEnabled(true);
// Execute the upload using an Upload URL with the destination resource
// name.
uploader
.upload(
new GenericUrl(
"https://displayvideo.googleapis.com/upload/media/"
+ resource-name));
Python
# Create a media upload object.
media = MediaFileUpload(script-path)
# Create upload request.
upload_request = service.media().upload(
resourceName=resource-name, media_body=media)
# Override response handler to expect null response.
upload_request.postproc = HttpRequest.null_postproc
# Upload script to resource location given in retrieved custom bidding
# script reference object.
upload_request.execute()
PHP
// Create a media object.
$mediaBody = new Google_Service_DisplayVideo_GoogleBytestreamMedia();
$mediaBody->setResourceName(resource-name);
// Set parameters for upload request.
$optParams = array(
'data' => file_get_contents(script-path),
'uploadType' => 'media',
'resourceName' => resource-name
);
// Upload script file to given resource location.
$this->service->media->upload(
resource-name,
$mediaBody,
$optParams
);
cURL
curl --request POST 'https://displayvideo.googleapis.com/upload/media/resource-name?uploadType=media'
-H 'authorization: Bearer access-token'
-H 'Content-Type: text/plain'
--data-binary @script-path
Create a script object
Once the script file is uploaded, create a custom bidding script resource with
the customBiddingAlgorithms.scripts.create
method. The
CustomBiddingScript
object passed in the request should only
include the CustomBiddingScriptRef
object as the assigned
value of the script
field. This associates the uploaded
script file with the new script resource.
Here's an example of how to create a script resource:
Java
// Create the custom bidding script structure.
CustomBiddingScript customBiddingScript =
new CustomBiddingScript()
.setScript(custom-bidding-script-ref);
// Create the custom bidding script.
CustomBiddingScript response =
service
.customBiddingAlgorithms()
.scripts()
.create(custom-bidding-algorithm-id, customBiddingScript)
.setAdvertiserId(advertiser-id)
.execute();
// Display the new script resource name
System.out.printf(
"The following script was created: %s%n",
response.getName());
Python
# Create a custom bidding script object.
script_obj = {
'script': custom-bidding-script-ref
}
# Create the custom bidding script.
response = service.customBiddingAlgorithms().scripts().create(
customBiddingAlgorithmId=custom-bidding-algorithm-id,
advertiserId=advertiser-id,
body=script_obj).execute()
# Display the new custom bidding script object.
print(f'The following custom bidding script was created: {response}')
PHP
// Create the custom bidding script object.
$customBiddingScript =
new Google_Service_DisplayVideo_CustomBiddingScript();
$customBiddingScript->setScript(custom-bidding-script-ref);
// Set parameters for create script request.
$optParams = array(
'advertiserId' => advertiser-id
);
// Create the custom bidding script.
$result = $this->service->customBiddingAlgorithms_scripts->create(
custom-bidding-algorithm-id,
$customBiddingScript,
$optParams
);
// Display the new script resource name.
printf('The following script was created: %s.\n', $result->getName());
Once you create a custom bidding script resource, Display & Video 360 processes the
script to make sure that it can be successfully used to score impressions.
Retrieve the state of this processing through the script object's
state
field. Once the new script is accepted, the custom
bidding algorithm begins using the script to score impression values. This
happens immediately, so be sure that you want to update the algorithm before
creating a new script resource.
Upload rules
Rules-based custom bidding algorithms employ logic provided in an
AlgorithmRules
object to evaluate the worth of an impression.
AlgorithmRules
objects are uploaded in a JSON file and then
associated with a custom bidding algorithm through a
CustomBiddingAlgorithmRules
object.
Retrieve a rules resource location
First, retrieve an available resource location under the custom bidding
algorithm resource with the
customBiddingAlgorithms.uploadRules
method. This
request returns a CustomBiddingAlgorithmsRulesRef
object
with a resource name. You can upload your rules
file to the location dictated by the resource name. Then
use the custom bidding algorithm rules reference object to create your rules
resource.
Here's an example of how to retrieve an available resource location:
Java
// Create the custom bidding algorithm structure.
CustomBiddingAlgorithmRulesRef rulesRef =
service
.customBiddingAlgorithms()
.uploadRules(custom-bidding-algorithm-id)
.setAdvertiserId(advertiser-id)
.execute();
System.out.printf(
"The rules can be uploaded to the following resource path: %s%n",
rulesRef.getResourceName()
);
Python
# Retrieve a usable custom bidding algorithm rules reference
# object.
custom_bidding_algorithm_rules_ref = service.customBiddingAlgorithms().uploadRules(
customBiddingAlgorithmId=custom-bidding-algorithm-id,
advertiserId=advertiser-id
).execute()
# Display the new custom bidding algorithm rules reference object.
print('The following custom bidding algorithm rules reference object was retrieved:'
f' {custom_bidding_algorithm_rules_ref}')
PHP
// Set parent advertiser ID of custom bidding algorithm
// in optional parameters array for request.
$optParams = array('advertiserId' => advertiser-id);
// Retrieve a usable custom bidding algorithm rules reference.
$rulesRefResponse = $this->service->customBiddingAlgorithms->uploadRules(
custom-bidding-algorithm-id,
$optParams
);
// Display the new custom bidding algorithm rules reference object resource path.
printf(
'The rules can be uploaded to the following resource path: %s\n',
$rulesRefResponse->getResourceName()
);
Upload an AlgorithmRules
file
After retrieving an available resource location, upload your rules file to that
location in the Display & Video 360 system with the
media.upload
method. This method supports a
simple upload that requires the query parameter
uploadType=media
.
Here's an example of how to upload an AlgorithmRules
file given a retrieved
custom bidding algorithm rules reference object:
Java
// Create media object.
GoogleBytestreamMedia media = new GoogleBytestreamMedia();
media.setResourceName(resource-name);
// Create input stream for the rules file.
InputStreamContent rulesFileStream =
new InputStreamContent(
null, new FileInputStream(rules-file-path));
// Create media.upload request.
Media.Upload uploadRequest =
service
.media()
.upload(
resource-name,
media,
rulesFileStream);
// Retrieve uploader from the request and set it to us a simple
// upload request.
MediaHttpUploader uploader = uploadRequest.getMediaHttpUploader();
uploader.setDirectUploadEnabled(true);
// Execute the upload using an Upload URL with the destination resource
// name.
uploader
.upload(
new GenericUrl(
"https://displayvideo.googleapis.com/upload/media/"
+ resource-name));
Python
# Create a media upload object.
media = MediaFileUpload(rules-file-path)
# Create upload request.
upload_request = service.media().upload(
resourceName=resource-name, media_body=media)
# Override response handler to expect null response.
upload_request.postproc = HttpRequest.null_postproc
# Upload rules file to resource location given in retrieved custom bidding
# algorithm rules reference object.
upload_request.execute()
PHP
// Create a media object.
$mediaBody = new Google_Service_DisplayVideo_GoogleBytestreamMedia();
$mediaBody->setResourceName(resource-name);
// Set parameters for upload request.
$optParams = array(
'data' => file_get_contents(rules-file-path),
'uploadType' => 'media',
'resourceName' => resource-name
);
// Upload rules file to given resource location.
$this->service->media->upload(
resource-name,
$mediaBody,
$optParams
);
cURL
curl --request POST 'https://displayvideo.googleapis.com/upload/media/resource-name?uploadType=media'
-H 'authorization: Bearer access-token'
-H 'Content-Type: text/plain'
--data-binary @rules-file-path
Create a rules object
Once the AlgorithmRules
JSON file is uploaded, create a custom bidding
algorithm rules resource with the
customBiddingAlgorithms.rules.create
method. The
CustomBiddingAlgorithmRules
object passed in the request should
only include the CustomBiddingAlgorithmRulesRef
object as
the assigned value of the rules
field. This associates the
uploaded AlgorithmRules
JSON file with the new rules resource.
Here's an example of how to create a rules resource:
Java
// Create the custom bidding algorithm rules structure.
CustomBiddingAlgorithmRules customBiddingAlgorithmRules =
new CustomBiddingAlgorithmRules()
.setRules(custom-bidding-algorithm-rules-ref);
// Create the rules resource.
CustomBiddingAlgorithmRules response =
service
.customBiddingAlgorithms()
.rules()
.create(custom-bidding-algorithm-id, customBiddingAlgorithmRules)
.setAdvertiserId(advertiser-id)
.execute();
// Display the new rules resource name.
System.out.printf(
"The following custom bidding algorithm rules object was created: %s%n",
response.getName());
Python
# Create the custom bidding algorithm rules object.
rules_obj = {
'rules': custom-bidding-algorithm-rules-ref
}
# Create the rules resource.
response = service.customBiddingAlgorithms().rules().create(
customBiddingAlgorithmId=custom-bidding-algorithm-id,
advertiserId=advertiser-id,
body=rules_obj).execute()
# Display the new custom bidding algorithm rules object.
print(f'The following custom bidding algorithm rules resource was created: {response}')
PHP
// Create the custom bidding algorithm rules object.
$customBiddingAlgorithmRules =
new Google_Service_DisplayVideo_CustomBiddingAlgorithmRules();
$customBiddingAlgorithmRules->setRules(custom-bidding-algorithm-rules-ref);
// Set parameters for create rules request.
$optParams = array(
'advertiserId' => advertiser-id
);
// Create the custom bidding algorithm rules resource.
$result = $this->service->customBiddingAlgorithms_rules->create(
custom-bidding-algorithm-id,
$customBiddingAlgorithmRules,
$optParams
);
// Display the new custom bidding algorithm rules resource name.
printf('The following rules resource was created: %s.\n', $result->getName());
Once you create a rules resource, Display & Video 360 processes the ruleset to make
sure that it can be successfully used to score impressions. Retrieve the state
of this processing through the rules object's state
field.
Once the new rules are accepted, the custom bidding algorithm begins using the
rules to score impression values immediately.
If the rules are rejected, retrieve the reason for the rejection from the rules
object's error
. In the case of rejection, update your
AlgorithmRules
object to fix the error and repeat the upload process starting
from retrieving the rules reference object.
Assign a Custom Bidding Algorithm
After you create a custom bidding algorithm, upload accepted logic, and meet the necessary requirements, you can assign your custom bidding algorithm to the bidding strategy of a line item or insertion order.
You can use custom bidding algorithms in
maximize spend and
performance goal bid strategies by assigning BIDDING_STRATEGY_PERFORMANCE_GOAL_TYPE_CUSTOM_ALGO
and the custom bidding algorithm ID to the
performanceGoalType
and
customBiddingAlgorithmId
fields, respectively.
Depending on the bid strategy, other bid parameters may be available or
required.
Here's an example of how to update a line item to use a maximize spend bid strategy with a given custom bidding algorithm:
Java
// Create the line item structure.
LineItem lineItem = new LineItem();
// Create and set the bidding strategy structure.
BiddingStrategy biddingStrategy = new BiddingStrategy();
MaximizeSpendBidStrategy maxSpendBidStrategy =
new MaximizeSpendBidStrategy()
.setPerformanceGoalType(
"BIDDING_STRATEGY_PERFORMANCE_GOAL_TYPE_CUSTOM_ALGO")
.setCustomBiddingAlgorithmId(custom-bidding-algorithm-id);
biddingStrategy.setMaximizeSpendAutoBid(maxSpendBidStrategy);
lineItem.setBidStrategy(biddingStrategy);
// Configure the patch request and set update mask to only update
// the bid strategy.
LineItems.Patch request =
service
.advertisers()
.lineItems()
.patch(advertiser-id, line-item-id, lineItem)
.setUpdateMask("bidStrategy");
// Update the line item.
LineItem response = request.execute();
// Display the custom bidding algorithm ID used in the new
// bid strategy.
System.out.printf(
"LineItem %s now has a bid strategy utilizing custom "
+ "bidding algorithm %s%n",
response.getName(),
response
.getBidStrategy()
.getMaximizeSpendAutoBid()
.getCustomBiddingAlgorithmId());
Python
# Create the new bid strategy object.
bidding_strategy = {
'maximizeSpendAutoBid': {
'performanceGoalType':
'BIDDING_STRATEGY_PERFORMANCE_GOAL_TYPE_CUSTOM_ALGO',
'customBiddingAlgorithmId': custom-bidding-algorithm-id
}
}
# Create a line item object assigning the new bid strategy.
line_item_obj = {'bidStrategy': bidding_strategy}
# Update the line item with a new bid strategy.
response = service.advertisers().lineItems().patch(
advertiserId=advertiser-id,
lineItemId=line-item-id,
updateMask='bidStrategy',
body=line_item_obj).execute()
# Display the line item's new bid strategy
print(f'Line Item {response["name"]} is now using the following bid'
f' strategy: {response["bidStrategy"]}.')
PHP
// Create the line item structure.
$lineItem = new Google_Service_DisplayVideo_LineItem();
// Create and set the bidding strategy structure.
$biddingStrategy = new Google_Service_DisplayVideo_BiddingStrategy();
$maximizeSpendBidStrategy =
new Google_Service_DisplayVideo_MaximizeSpendBidStrategy();
$maximizeSpendBidStrategy->setPerformanceGoalType(
'BIDDING_STRATEGY_PERFORMANCE_GOAL_TYPE_CUSTOM_ALGO'
);
$maximizeSpendBidStrategy->setCustomBiddingAlgorithmId(
custom-bidding-algorithm-id
);
$biddingStrategy->setMaximizeSpendAutoBid($maximizeSpendBidStrategy);
$lineItem->setBidStrategy($biddingStrategy);
// Set update mask.
$optParams = array('updateMask' => 'bidStrategy');
// Update the line item.
$result = $this->service->advertisers_lineItems->patch(
advertiser-id,
line-item-id,
$lineItem,
$optParams
);
// Display the custom bidding algorithm ID used in the new bid strategy.
printf(
'Line Item %s now has a bid strategy utilizing custom bidding algorithm %s.\n',
$result['name'],
$result['bidStrategy']['maximizeSpendBidStrategy']['customBiddingAlgorithmId']
);