private static function addCampaignBudget(GoogleAdsClient $googleAdsClient, int $customerId)
{
// Creates a campaign budget.
$budget = new CampaignBudget([
'name' => 'Interplanetary Cruise Budget #' . getPrintableDatetime(),
'delivery_method' => BudgetDeliveryMethod::STANDARD,
'amount_micros' => 500000
]);
// Creates a campaign budget operation.
$campaignBudgetOperation = new CampaignBudgetOperation();
$campaignBudgetOperation->setCreate($budget);
// Issues a mutate request.
$campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient();
$response = $campaignBudgetServiceClient->mutateCampaignBudgets(
$customerId,
[$campaignBudgetOperation]
);
/** @var CampaignBudget $addedBudget */
$addedBudget = $response->getResults()[0];
printf("Added budget named '%s'%s", $addedBudget->getResourceName(), PHP_EOL);
return $addedBudget->getResourceName();
}
public function create()
{
$manager_id=xxxxxxxxxx;
$customer_id=xxxxxxxxxxxx;
$random=rand(1,999999999);
// Replace with your client ID, client secret, and refresh token.
$oAuth2Credential = (new OAuth2TokenBuilder())
->withClientId(env('GOOGLE_CLIENT_ID'))
->withClientSecret(env('GOOGLE_CLIENT_SECRET'))
->withRefreshToken(env('GOOGLE_REFRESH_TOKEN'))
->build();
// Replace with your developer token, client customer ID, and user agent.
$googleAdsClient = (new GoogleAdsClientBuilder())
->withDeveloperToken(env('GOOGLE_DEVELOPER_TOKEN'))
->withOAuth2Credential($oAuth2Credential)
->build();
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
// Creates a query that retrieves all campaigns.
$query = 'SELECT
campaign.name,campaign_budget.amount_micros,campaign.status,campaign.optimization_score,campaign.advertising_channel_type,
metrics.clicks,metrics.impressions,metrics.ctr,metrics.average_cpc,metrics.cost_micros,campaign.bidding_strategy_type FROM campaign';
// Issues a search stream request.
/** @var GoogleAdsServerStreamDecorator $stream */
$stream =
$googleAdsServiceClient->searchStream($customer_id, $query);
/* var_dump( $stream->iterateAllElements());
foreach ($stream->iterateAllElements() as $googleAdsRow) {
printf(
"Campaign with ID %d and name '%s' was found.%s",
$googleAdsRow->getCampaign()->getId(),
$googleAdsRow->getCampaign()->getName(),
PHP_EOL
);
}
die(); */
$budgetResourceName = $this->addCampaignBudget($googleAdsClient, $customer_id);
// Configures the campaign network options.
$networkSettings = new NetworkSettings([
'target_google_search' => true,
'target_search_network' => true,
// Enables Display Expansion on Search campaigns. See
'target_content_network' => true,
'target_partner_search_network' => false
]);
$campaignOperations = [];
for ($i = 0; $i < self::NUMBER_OF_CAMPAIGNS_TO_ADD; $i++) {
// Creates a campaign.
// [START add_campaigns_1]
$campaign = new Campaign([
'name' => 'Interplanetary Cruise #' . getPrintableDatetime(),
'advertising_channel_type' => AdvertisingChannelType::SEARCH,
// Recommendation: Set the campaign to PAUSED when creating it to prevent
// the ads from immediately serving. Set to ENABLED once you've added
// targeting and the ads are ready to serve.
'status' => CampaignStatus::PAUSED,
// Sets the bidding strategy and budget.
'manual_cpc' => new ManualCpc(),
'campaign_budget' => $budgetResourceName,
// Adds the network settings configured above.
'network_settings' => $networkSettings,
// Optional: Sets the start and end dates.
'start_date' => date('Ymd', strtotime('+1 day')),
'end_date' => date('Ymd', strtotime('+1 month'))
]);
// [END add_campaigns_1]
// Creates a campaign operation.
$campaignOperation = new CampaignOperation();
$campaignOperation->setCreate($campaign);
$campaignOperations[] = $campaignOperation;
}
// Issues a mutate request to add campaigns.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns($customerId, $campaignOperations);
printf("Added %d campaigns:%s", $response->getResults()->count(), PHP_EOL);
foreach ($response->getResults() as $addedCampaign) {
/** @var Campaign $addedCampaign */
print "{$addedCampaign->getResourceName()}" . PHP_EOL;
}
// Iterates over all rows in all messages and prints the requested field values for
// the campaign in each row.
/*foreach ($stream->iterateAllElements() as $googleAdsRow) {
printf(
"Campaign with ID %d and name '%s' was found.%s",
$googleAdsRow->getCampaign()->getId(),
$googleAdsRow->getCampaign()->getName(),
PHP_EOL
);
} */
// Kampanya gurubu oluşturmak için CampaignServiceClient nesnesini oluşturun
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
// Yeni bir kampanya oluşturmak için Campaign nesnesi oluşturun
$campaign = new Campaign([
'name' => 'My Campaign',
'status' => CampaignStatus::PAUSED,
'advertising_channel_type' => AdvertisingChannelType::SEARCH,
'budget' => new CampaignBudget(['amount' => new Money(['micros' => 5000000])])
]);
// Kampanya oluşturmak için CampaignOperation nesnesi oluşturun
$campaignOperation = new CampaignOperation();
$campaignOperation->setCreate($campaign);
// Kampanyayı oluşturun
$response = $campaignServiceClient->mutateCampaigns($customerId, [$campaignOperation]);
$result = $response->getResults()[0];
$campaignResourceName = $result->getResourceName();
echo "Created campaign with resource name: " . $campaignResourceName . "\n";
// Kampanya gurubu oluşturma
$campaignCriterion = new CampaignCriterion([
'campaign' => ResourceNames::forCampaign($customer_id, $campaignResourceName),
'status' => CampaignStatus::PAUSED,
'type' => CriterionType::PLACEMENT
]);
$campaignCriterionOperation = new CampaignCriterionOperation();
$campaignCriterionOperation->setCreate($campaignCriterion);
// İstekleri gönderme
$campaignServiceClient = new CampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns($customerId, [$campaignOperation]);
$response = $campaignServiceClient->mutateCampaignCriteria($customerId, [$campaignCriterionOperation]);
// Sonuçları yazdırma
printf('Oluşturulan kampanya adı: "%s"%s', $response->getResults()[0]->getResourceName(), PHP_EOL);
printf('Oluşturulan kampanya gurubu adı: "%s"%s', $response->getResults()[0]->getResourceName(), PHP_EOL);