public function handle()
{
$clientId = "xxxx";
$clientSecret = "xxxx";
$refreshToken = "xxxx";
$developerToken = "xxxx";
$oAuth2Credential = (new OAuth2TokenBuilder())
->withClientId($clientId)
->withClientSecret($clientSecret)
->withRefreshToken($refreshToken)
->build()
;
$googleAdsClient = (new GoogleAdsClientBuilder())
->withDeveloperToken($developerToken)
->withLoginCustomerId($this->loginCustomerId)
->withOAuth2Credential($oAuth2Credential)
->build()
;
try{
DB::table('actions')->insert([
'type' => 'campaignEnabled',
'accountId' => $this->accountId,
'campaignId' => $this->campaignId,
'date' => (new \DateTime())->format("Y-m-d"),
'time' => new \DateTime('now', new \DateTimeZone("GMT")),
'hash' => md5(json_encode([
'budget' => $this->campaignBudget / 1000000,
])),
'data' => json_encode([
'budget' => $this->campaignBudget / 1000000,
'accountName' => $this->accountName,
'campaignName' => $this->campaignName,
]),
]);
$campaign = new Campaign([
'resource_name' => ResourceNames::forCampaign($this->accountId, $this->campaignId),
'status' => CampaignStatus::ENABLED
]);
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
$this->accountId,
[$campaignOperation]
);
}catch (\Illuminate\Database\QueryException $e){
$errorCode = $e->errorInfo[1];
if($errorCode !== 1062){
throw $e;
}
}
}