try {
/* @var GoogleAdsClient $googleAdsClient */
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile(GOOGLE_ADS_SETTINGS)->build();
// Construct a Google Ads client configured from a properties file and the
// OAuth2 credentials above.
$googleAdsClient = (new GoogleAdsClientBuilder())
->fromFile(GOOGLE_ADS_SETTINGS)
->withOAuth2Credential($oAuth2Credential)
->build();
// Create the budget
$budget = new CampaignBudget();
$budget->setName(new StringValue(['value' => 'Universal App Campaign test budget [' . uniqid() . ']']))
->setDeliveryMethod(BudgetDeliveryMethod::STANDARD)
->setAmountMicros(new Int64Value(['value' => 500000]))
->setExplicitlyShared(new BoolValue(['value' => false]));
// Creates a campaign budget operation.
$campaignBudgetOperation = new CampaignBudgetOperation();
$campaignBudgetOperation->setCreate($budget);
// Issues a mutate request.
$campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient();
$response = $campaignBudgetServiceClient->mutateCampaignBudgets(
$customerId,
[$campaignBudgetOperation]
);
$addedBudget = $response->getResults()[0];
$budgetResourceName = $addedBudget->getResourceName();
// Create the bidding strategy
$strategy = new BiddingStrategy();
$strategy->setName(new StringValue(['value' => "testingStrategy19"]));
$targetcpa = new TargetCpa();
$targetcpa->setTargetCpaMicros(new Int64Value(['value' => 10000]));
$strategy->setTargetCpa($targetcpa);
$biddingStrategyOperation = new BiddingStrategyOperation();
$biddingStrategyOperation->setCreate($strategy);
// Issues a mutate request.
$campaignBudgetServiceClient = $googleAdsClient->getBiddingStrategyServiceClient();
$response = $campaignBudgetServiceClient->mutateBiddingStrategies(
$customerId,
[$biddingStrategyOperation]
);
$addedBudget = $response->getResults()[0];
$biddingStrategy = $addedBudget->getResourceName();
// Create the campaign object
$startDate = new StringValue(['value' => date('Ymd', strtotime("2020-4-1"))]);
$endDate = new StringValue(['value' => date('Ymd', strtotime("2020-4-10"))]);
$campaignObject = new Campaign();
$campaignObject->setName(new StringValue(['value' => 'Universal App Campaign Test [' . uniqid() . ']']))
->setAdvertisingChannelType(AdvertisingChannelType::MULTI_CHANNEL)
->setStatus(CampaignStatus::PAUSED)
->setCampaignBudget(new StringValue(['value' => $budgetResourceName]))
->setStartDate($startDate)
->setEndDate($endDate)
->setAdvertisingChannelSubType(AdvertisingChannelSubType::APP_CAMPAIGN)
->setBiddingStrategy(new StringValue(['value' => $biddingStrategy]))
->setBiddingStrategyType(\Google\Ads\GoogleAds\V3\Enums\BiddingStrategyTypeEnum\BiddingStrategyType::TARGET_CPA);
$appCampaignSetting = new AppCampaignSetting();
$appCampaignSetting->setBiddingStrategyGoalType(AppCampaignBiddingStrategyGoalType::OPTIMIZE_INSTALLS_TARGET_INSTALL_COST);
$appCampaignSetting->setAppId(new StringValue(['value' => "com.nakko.android.nl100"]));
$appCampaignSetting->setAppStore(AppCampaignAppStore::GOOGLE_APP_STORE);
$campaignObject->setAppCampaignSetting($appCampaignSetting);
// Creates a campaign operation.
$campaignOperation = new CampaignOperation();
$campaignOperation->setCreate($campaignObject);
// Define campaign operations to be executed
$campaignOperations = [];
$campaignOperations[] = $campaignOperation;
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns($customerId, $campaignOperations, ['partialFailure' => false]);
$result = [];
$campaignId = 0;
foreach ($response->getResults() as $addedCampaign) {
$campaignId = $addedCampaign->getResourceName();
$result[] = [
"resourceName" => $addedCampaign->getResourceName()
];
}
echo $campaignId;
} catch (GoogleAdsException $googleAdsException) {
$errors = [];
foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
$errors[] = [
"code" => $error->getErrorCode()->getErrorCode(),
"message" => $error->getMessage()
];
}
$errorString = "";
foreach($errors as $error) {
$errorString = $errorString.implode(", ", $error);
}
throw new Exception("GoogleAdsException: ".$errorString);
} catch (ApiException $apiException) {
throw new Exception("ApiException: ".$apiException->getMessage());
} catch (Exception $exception){
throw $exception;
}