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?
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"AdWords API and Google Ads API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/903b0117-2069-4a9d-987d-0b936c501e7a%40googlegroups.com.