Google Ads: update campaign bidding strategy

74 views
Skip to first unread message

integrations bidalgo

unread,
Jun 24, 2019, 11:22:24 AM6/24/19
to AdWords API and Google Ads API Forum
Hi,

I'm using the following code to update campaign bid:

/**
 * Set campaign bid
* @param float $bid
* @param string $campaignId
* @param int $type , type is TARGET_CPA or TARGET_ROAS
* @return mixed
* @throws \Google\ApiCore\ApiException
* @throws \Google\ApiCore\ValidationException
*/
public function setCampaignBid($bid, $campaignId, $type)
{
// get campaign bidding strategy id
$fields = array('bidding_strategy.id');

$params = new GoogleAdsQueryParams();
$params->fromObject = 'campaign';
$params->fields = $fields;
$params->whereObjects = array('campaign.id');
$params->whereFields = array(array($campaignId));
$params->whereTokens =array(GoogleAdsQueryBuilder::EQUALS);
$params->limit =1;

$campaignObj = $this->executeGoogleAdsServiceQuery($params);
$biddingStrategyResource = $this->createCampaignBiddingStrategy($bid, $type, $campaignObj? $campaignObj['bidding_strategy']['id']: null);

$campaign = new Campaign(['resource_name' => ResourceNames::forCampaign($this->account_google_id, $campaignId)]);

// Create campaign using an existing ID.
/** @var StringValue $biddingStrategyResource */
$campaign->setBiddingStrategy($biddingStrategyResource);

$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));
$response = $this->campaignServiceClient->mutateCampaigns(
$this->account_google_id,
[$campaignOperation]
);
return $response->getResults()[0];
}

/**
* @param $bid
* @param $type
* @param null $campaignBiddingStrategyId
* @return string
* @throws \Google\ApiCore\ApiException
*/
private function createCampaignBiddingStrategy($bid, $type, $campaignBiddingStrategyId = null): string
{
$biddingStrategy = new BiddingStrategy();
if ($campaignBiddingStrategyId)
{
$biddingStrategy->setId($campaignBiddingStrategyId);
}
$biddingStrategy->setType($type);

if ($type == BiddingStrategyType::TARGET_ROAS)
{
$targetRoas = new TargetRoas(['target_roas' => $bid]);
$biddingStrategy->setTargetRoas($targetRoas);
}
else
{
$money = new Money();
$bidValue = new Int64Value();
$bidValue->setValue($bid);
$money->setAmountMicros($bidValue);
$targetSpa = new TargetCpa(['target_cpa_micros' => $money->getAmountMicros()]);
$biddingStrategy->setTargetCpa($targetSpa);
}
// create operation
$biddingStrategyOperation = new BiddingStrategyOperation();
if ($campaignBiddingStrategyId)
{
$biddingStrategyOperation->setUpdate($biddingStrategy);
}
else
{
$biddingStrategyOperation->setCreate($biddingStrategy);
}
$response = $this->biddingStrategyService->mutateBiddingStrategies($this->account_google_id, [$biddingStrategyOperation]);
/** @var BiddingStrategy $mutatedBiddingStrategy */
$mutatedBiddingStrategy = $response->getResults()[0];
return $mutatedBiddingStrategy->getResourceName();
}

In the 1st function (setCampaignBid) I'm getting the campaign bidding strategy id. If it's not null, I'm using it to get campaign bidding strategy , if it does not exists, I'm creating it (createCampaignBiddingStrategy).
After that, I'm using CampaignOperation to update/create campaign's bidding strategy.
The problem is that I'm getting NULL when querying campaign's bidding strategy (there is bidding strategy in the campaign I'm querying).
What I'm doing wrong?


integrations bidalgo

unread,
Jun 24, 2019, 11:24:15 AM6/24/19
to AdWords API and Google Ads API Forum
addition:

the query I'm sending to query campaign's bidding strategy id is:

SELECT bidding_strategy.id FROM campaign WHERE campaign.id = 1703363901 LIMIT 1

Google Ads API Forum Advisor Prod

unread,
Jun 24, 2019, 4:27:37 PM6/24/19
to integr...@bidalgo.ai, adwor...@googlegroups.com
Hello, 

The bidding_strategy data is returned if you have Portfolio strategy associated with the campaign. It looks like the specific account has no Portfolio bid strategies in the account. You may use the campaign.bidding_strategy to pull the Campaign bid strategy details. 

Please let us know if you have any issues.

Thanks,
Bharani, Google Ads API Team

ref:_00D1U1174p._5001UCZBMR:ref

integrations bidalgo

unread,
Jun 25, 2019, 5:42:04 AM6/25/19
to AdWords API and Google Ads API Forum
Thanks, worked for me!
Reply all
Reply to author
Forward
0 new messages