175 lines
6.8 KiB
PHP
175 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace app\service;
|
|
|
|
|
|
use app\util\Helper;
|
|
use app\util\ArgumentNames;
|
|
use app\util\ArgumentParser;
|
|
use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClient;
|
|
use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder;
|
|
use Google\Ads\GoogleAds\Lib\V18\GoogleAdsException;
|
|
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
|
|
use Google\Ads\GoogleAds\V18\Common\ManualCpc;
|
|
use Google\Ads\GoogleAds\V18\Enums\AdvertisingChannelTypeEnum\AdvertisingChannelType;
|
|
use Google\Ads\GoogleAds\V18\Enums\BudgetDeliveryMethodEnum\BudgetDeliveryMethod;
|
|
use Google\Ads\GoogleAds\V18\Enums\CampaignStatusEnum\CampaignStatus;
|
|
use Google\Ads\GoogleAds\V18\Errors\GoogleAdsError;
|
|
use Google\Ads\GoogleAds\V18\Resources\Campaign;
|
|
use Google\Ads\GoogleAds\V18\Resources\Campaign\NetworkSettings;
|
|
use Google\Ads\GoogleAds\V18\Resources\CampaignBudget;
|
|
use Google\Ads\GoogleAds\V18\Services\CampaignBudgetOperation;
|
|
use Google\Ads\GoogleAds\V18\Services\CampaignOperation;
|
|
use Google\Ads\GoogleAds\V18\Services\MutateCampaignsRequest;
|
|
use Google\Ads\GoogleAds\V18\Services\MutateCampaignBudgetsRequest;
|
|
use Google\ApiCore\ApiException;
|
|
|
|
class GoogleAdsCampaignService
|
|
{
|
|
private $googleAdsClient;
|
|
private $customerId;
|
|
private const NUMBER_OF_CAMPAIGNS_TO_ADD = 1;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
|
|
|
|
// OAuth2 Token Authentication
|
|
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
|
|
|
|
// Google Ads Client initialization
|
|
$this->googleAdsClient = (new GoogleAdsClientBuilder())
|
|
->fromFile()
|
|
->withOAuth2Credential($oAuth2Credential)
|
|
->build();
|
|
}
|
|
|
|
|
|
/**
|
|
* Runs the example.
|
|
*
|
|
* @param int $customerId the customer ID
|
|
* @param $options
|
|
* @return mixed
|
|
* @throws ApiException
|
|
*/
|
|
public function runAddCampaign(int $customerId, $options): mixed
|
|
{
|
|
$googleAdsClient = $this->googleAdsClient;
|
|
// Creates a single shared budget to be used by the campaigns added below.
|
|
$budgetResourceName = self::addCampaignBudget($googleAdsClient, $customerId,$options);
|
|
|
|
// Configures the campaign network options.
|
|
$networkSettings = new NetworkSettings([
|
|
'target_google_search' => true,
|
|
'target_search_network' => true,
|
|
// Enables Display Expansion on Search campaigns. See
|
|
// https://support.google.com/google-ads/answer/7193800 to learn more.
|
|
'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 #' . Helper::getPrintableDatetime(),
|
|
'name' => $options['campaign_name'].rand(10,99).' #'. Helper::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(
|
|
MutateCampaignsRequest::build($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;
|
|
$resourceNames[] = $addedCampaign->getResourceName();
|
|
}
|
|
return $resourceNames;
|
|
}
|
|
|
|
/**
|
|
* Runs the example.
|
|
*
|
|
* @param int $customerId the customer ID
|
|
* @param $options
|
|
* @return mixed
|
|
* @throws ApiException
|
|
*/
|
|
public function runAddCampaignBudget(int $customerId, $options): mixed
|
|
{
|
|
if(!$customerId){
|
|
$customerId = $this->customerId;
|
|
}
|
|
$googleAdsClient = $this->googleAdsClient;
|
|
// Creates a single shared budget to be used by the campaigns added below.
|
|
$budgetResourceName = self::addCampaignBudget($googleAdsClient, $customerId,$options);
|
|
|
|
return $budgetResourceName;
|
|
}
|
|
|
|
/**
|
|
* Creates a new campaign budget in the specified client account.
|
|
*
|
|
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
|
|
* @param int $customerId the customer ID
|
|
* @return string the resource name of the newly created budget
|
|
* @throws ApiException
|
|
*/
|
|
// [START add_campaigns]
|
|
private static function addCampaignBudget(GoogleAdsClient $googleAdsClient, int $customerId,$options)
|
|
{
|
|
|
|
// Creates a campaign budget.
|
|
$budget = new CampaignBudget([
|
|
// 'name' => 'Interplanetary Cruise Budget #' . Helper::getPrintableDatetime(),
|
|
'name' => $options['name'].rand(10,99).' #' . Helper::getPrintableDatetime(),
|
|
// 'delivery_method' => BudgetDeliveryMethod::STANDARD,
|
|
'amount_micros' => $options['amount'] * 1000000
|
|
]);
|
|
|
|
// Creates a campaign budget operation.
|
|
$campaignBudgetOperation = new CampaignBudgetOperation();
|
|
$campaignBudgetOperation->setCreate($budget);
|
|
|
|
// Issues a mutate request.
|
|
$campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient();
|
|
$response = $campaignBudgetServiceClient->mutateCampaignBudgets(
|
|
MutateCampaignBudgetsRequest::build($customerId, [$campaignBudgetOperation])
|
|
);
|
|
|
|
/** @var CampaignBudget $addedBudget */
|
|
$addedBudget = $response->getResults()[0];
|
|
// printf("Added budget named '%s'%s", $addedBudget->getResourceName(), PHP_EOL);
|
|
|
|
return $addedBudget->getResourceName();
|
|
}
|
|
// [END add_campaigns]
|
|
|
|
}
|