Skip to content

Instantly share code, notes, and snippets.

@jamesmills
Last active June 24, 2020 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamesmills/675fa4574484468a04a5 to your computer and use it in GitHub Desktop.
Save jamesmills/675fa4574484468a04a5 to your computer and use it in GitHub Desktop.
Google Adwords API. Creating a Call Extension for an AdGroup.
<?php
public function fire()
{
// See the Placeholder reference page for a list of all the placeholder types and fields.
// https://developers.google.com/adwords/api/docs/appendix/placeholders.html
define('PLACEHOLDER_SITELINKS', 1);
define('PLACEHOLDER_FIELD_SITELINK_LINK_TEXT', 1);
define('PLACEHOLDER_FIELD_SITELINK_URL', 2);
define('PLACEHOLDER_FIELD_LINE_1_TEXT', 3);
define('PLACEHOLDER_FIELD_LINE_2_TEXT', 4);
define('PLACEHOLDER_CALL', 2);
define('PLACEHOLDER_FIELD_PHONE_NUMBER', 1); // String - The advertiser's phone number to append to the ad.
define('PLACEHOLDER_FIELD_COUNTRY_CODE', 2); // String - Two character country code of the advertiser's phone number.
define('PLACEHOLDER_FIELD_CALL_ONLY', 4); // Boolean - Indicates whether the ad is call only, where the phone number is displayed and the URL will not.
// define('PLACEHOLDER_FIELD_CONVERSION_TYPE_ID', 6); // Int64 - The ID of an AdCallMetricsConversion object.
// https://developers.google.com/adwords/api/docs/reference/v201406/ConversionTrackerService.AdCallMetricsConversion
$adgroup = $this->createAdGroup(str_random(20));
$this->info('Created new AdGroup [NAME:' . $adgroup[0]->name . '] [ID:' . $adgroup[0]->id . '] [CAMPAING_NAME:' . $adgroup[0]->campaignName . ']');
$this->addCallExample($this->user, $this->campaign, $adgroup[0]->id);
// http://stackoverflow.com/questions/24889107/add-google-adwords-call-extension-to-adgroup-using-api
// http://www.slideshare.net/marcwan/adwords-api-feed-services
// https://github.com/googleads/googleads-php-lib/blob/master/examples/AdWords/v201406/AdvancedOperations/AddSitelinks.php
// https://developers.google.com/adwords/api/docs/appendix/placeholders
}
/**
* Runs the example.
*
* @param AdWordsUser $user the user to run the example with
* @param string $campaignId the ID of the campaign to add the sitelinks to
*/
protected function addCallExample(AdWordsUser $user, $campaignId, $adGroupId)
{
$callData = $this->createCallFeed($user);
$callData = $this->createCallFeedItems($user, $callData);
$this->createCallFeedMapping($user, $callData);
// $this->createCallCampaignFeed($user, $callData, $campaignId);
$this->createCallAdGroupFeed($user, $callData, $adGroupId);
}
/**
* Creates the feed that holds the call extension data.
*
* @param AdWordsUser $user the user to run the example with
*/
protected function createCallFeed(AdWordsUser $user)
{
// Map that holds IDs associated to the feeds metadata.
$callData = array();
// Get the FeedService, which loads the required classes.
$feedService = $user->GetService('FeedService');
// Create attributes.
$numberAttribute = new FeedAttribute();
$numberAttribute->type = 'STRING';
$numberAttribute->name = 'Phone Number';
$codeAttribute = new FeedAttribute();
$codeAttribute->type = 'STRING';
$codeAttribute->name = 'Country Code';
$onlyAttribute = new FeedAttribute();
$onlyAttribute->type = 'BOOLEAN';
$onlyAttribute->name = 'Call Only';
// Create the feed.
$callFeed = new Feed();
$callFeed->name = 'Call Extension 5';
$callFeed->attributes = [$numberAttribute, $codeAttribute, $onlyAttribute];
$callFeed->origin = 'USER';
// Create operation.
$operation = new FeedOperation();
$operation->operator = 'ADD';
$operation->operand = $callFeed;
$operations = array($operation);
// Add the feed.
$result = $feedService->mutate($operations);
$savedFeed = $result->value[0];
$callData['callFeedId'] = $savedFeed->id;
$savedAttributes = $savedFeed->attributes;
$callData['callNumberFeedAttributeId'] = $savedAttributes[0]->id;
$callData['callCodeFeedAttributeId'] = $savedAttributes[1]->id;
$callData['callOnlyFeedAttribute'] = $savedAttributes[2]->id;
printf(
'Feed with name "%s" and ID %d with callNumberFeedAttributeId %d'
. ", callCodeFeedAttributeId %d, callOnlyFeedAttribute %d "
. "were created.\n",
$savedFeed->name,
$savedFeed->id,
$savedAttributes[0]->id,
$savedAttributes[1]->id,
$savedAttributes[2]->id
);
return $callData;
}
/**
* Adds call extension items to the feed.
*
* @param AdWordsUser $user the user to run the example with
* @param map $callData IDs associated to created call feed metadata
*/
protected function createCallFeedItems(AdWordsUser $user, $callData)
{
// Get the FeedItemService, which loads the required classes.
$feedItemService = $user->GetService('FeedItemService');
// Create operations to add FeedItems.
$call_extension = $this->newCallFeedItemAddOperation($callData, '01132673478', 'gb', 'true');
$operations = array($call_extension);
$result = $feedItemService->mutate($operations);
$callData['callFeedItemIds'] = array();
foreach ($result->value as $feedItem) {
printf("FeedItem with feedItemId %d was added.\n", $feedItem->feedItemId);
$callData['callFeedItemIds'][] = $feedItem->feedItemId;
}
return $callData;
}
/**
* Maps the feed attributes to the call placeholders.
*
* @param AdWordsUser $user the user to run the example with
* @param map $callDate IDs associated to created call feed metadata
*/
protected function createCallFeedMapping(AdWordsUser $user, $callDate)
{
// Get the FeedMappingService, which loads the required classes.
$feedMappingService = $user->GetService('FeedMappingService');
// Map the FeedAttributeIds to the fieldId constants.
$callNumberFieldMapping = new AttributeFieldMapping();
$callNumberFieldMapping->feedAttributeId = $callDate['callNumberFeedAttributeId'];
$callNumberFieldMapping->fieldId = PLACEHOLDER_FIELD_PHONE_NUMBER;
$callCodeFieldMapping = new AttributeFieldMapping();
$callCodeFieldMapping->feedAttributeId = $callDate['callCodeFeedAttributeId'];
$callCodeFieldMapping->fieldId = PLACEHOLDER_FIELD_COUNTRY_CODE;
$callOnlyFieldMapping = new AttributeFieldMapping();
$callOnlyFieldMapping->feedAttributeId = $callDate['callOnlyFeedAttribute'];
$callOnlyFieldMapping->fieldId = PLACEHOLDER_FIELD_CALL_ONLY;
// Create the FieldMapping and operation.
$feedMapping = new FeedMapping();
$feedMapping->placeholderType = PLACEHOLDER_CALL;
$feedMapping->feedId = $callDate['callFeedId'];
$feedMapping->attributeFieldMappings = [$callNumberFieldMapping, $callCodeFieldMapping, $callOnlyFieldMapping];
$operation = new FeedMappingOperation();
$operation->operand = $feedMapping;
$operation->operator = 'ADD';
$operations = array($operation);
// Save the field mapping.
$result = $feedMappingService->mutate($operations);
foreach ($result->value as $feedMapping) {
printf(
'Feed mapping with ID %d and placeholderType %d was saved for ' .
"feed with ID %d.\n",
$feedMapping->feedMappingId,
$feedMapping->placeholderType,
$feedMapping->feedId
);
}
}
/**
* Creates the CampaignFeed associated to the feed data already populated.
*
* @param AdWordsUser $user the user to run the example with
* @param map $callData IDs associated to created call feed metadata
* @param string $campaignId the ID of the campaign to add the call extension to
*/
protected function createCallAdGroupFeed(AdWordsUser $user, $callData, $adGroupId)
{
// Get the CampaignFeedService, which loads the required classes.
$adGroupFeedService = $user->GetService('AdGroupFeedService');
$feedFunctionRequestContextOperand = new RequestContextOperand();
$feedFunctionRequestContextOperand->contextType = 'FEED_ITEM_ID';
$feedItemFunction = new FeedFunction();
$feedItemFunction->lhsOperand = array($feedFunctionRequestContextOperand);
$feedItemFunction->operator = 'IN';
$operands = array();
foreach ($callData['callFeedItemIds'] as $feedItemId) {
$constantOperand = new ConstantOperand();
$constantOperand->longValue = $feedItemId;
$constantOperand->type = 'LONG';
$operands[] = $constantOperand;
}
$feedItemFunction->rhsOperand = $operands;
# Optional: to target to a platform, define a function and 'AND' it with the feed item ID link:
$platformRequestContextOperand = new RequestContextOperand();
$platformRequestContextOperand->contextType = 'DEVICE_PLATFORM';
$platformOperand = new ConstantOperand();
$platformOperand->type = 'STRING';
$platformOperand->stringValue = 'Mobile';
$platformFunction = new FeedFunction();
$platformFunction->operator = 'EQUALS';
$platformFunction->lhsOperand = array($platformRequestContextOperand);
$platformFunction->rhsOperand = array($platformOperand);
$feedItemFunctionOperand = new FunctionOperand();
$feedItemFunctionOperand->value = $feedItemFunction;
$platformFunctionOperand = new FunctionOperand();
$platformFunctionOperand->value = $platformFunction;
$combinedFunction = new FeedFunction();
$combinedFunction->operator = 'AND';
$combinedFunction->lhsOperand = array($feedItemFunctionOperand, $platformFunctionOperand);
$adGroupFeed = new AdGroupFeed();
$adGroupFeed->feedId = $callData['callFeedId'];
$adGroupFeed->adGroupId = $adGroupId;
$adGroupFeed->matchingFunction = $combinedFunction;
// Specifying placeholder types on the CampaignFeed allows the same feed to be used for different placeholders in different Campaigns.
$adGroupFeed->placeholderTypes = array(PLACEHOLDER_CALL);
$operation = new AdGroupFeedOperation();
$operation->operand = $adGroupFeed;
$operation->operator = 'ADD';
$operations = array($operation);
$result = $adGroupFeedService->mutate($operations);
foreach ($result->value as $savedCampaignFeed) {
printf(
"Adgroup with ID %d was associated with feed with ID %d.\n",
$savedCampaignFeed->adGroupId,
$savedCampaignFeed->feedId
);
}
}
/**
* Creates the CampaignFeed associated to the feed data already populated.
*
* @param AdWordsUser $user the user to run the example with
* @param map $callData IDs associated to created call feed metadata
* @param string $campaignId the ID of the campaign to add the call extension to
*/
protected function createCallCampaignFeed(AdWordsUser $user, $callData, $campaignId)
{
// Get the CampaignFeedService, which loads the required classes.
$campaignFeedService = $user->GetService('CampaignFeedService');
$feedFunctionRequestContextOperand = new RequestContextOperand();
$feedFunctionRequestContextOperand->contextType = 'FEED_ITEM_ID';
$feedItemFunction = new FeedFunction();
$feedItemFunction->lhsOperand = array($feedFunctionRequestContextOperand);
$feedItemFunction->operator = 'IN';
$operands = array();
foreach ($callData['callFeedItemIds'] as $feedItemId) {
$constantOperand = new ConstantOperand();
$constantOperand->longValue = $feedItemId;
$constantOperand->type = 'LONG';
$operands[] = $constantOperand;
}
$feedItemFunction->rhsOperand = $operands;
# Optional: to target to a platform, define a function and 'AND' it with the feed item ID link:
$platformRequestContextOperand = new RequestContextOperand();
$platformRequestContextOperand->contextType = 'DEVICE_PLATFORM';
$platformOperand = new ConstantOperand();
$platformOperand->type = 'STRING';
$platformOperand->stringValue = 'Mobile';
$platformFunction = new FeedFunction();
$platformFunction->operator = 'EQUALS';
$platformFunction->lhsOperand = array($platformRequestContextOperand);
$platformFunction->rhsOperand = array($platformOperand);
$feedItemFunctionOperand = new FunctionOperand();
$feedItemFunctionOperand->value = $feedItemFunction;
$platformFunctionOperand = new FunctionOperand();
$platformFunctionOperand->value = $platformFunction;
$combinedFunction = new FeedFunction();
$combinedFunction->operator = 'AND';
$combinedFunction->lhsOperand = array($feedItemFunctionOperand, $platformFunctionOperand);
$campaignFeed = new CampaignFeed();
$campaignFeed->feedId = $callData['callFeedId'];
$campaignFeed->campaignId = $campaignId;
$campaignFeed->matchingFunction = $combinedFunction;
// Specifying placeholder types on the CampaignFeed allows the same feed to be used for different placeholders in different Campaigns.
$campaignFeed->placeholderTypes = array(PLACEHOLDER_CALL);
$operation = new CampaignFeedOperation();
$operation->operand = $campaignFeed;
$operation->operator = 'ADD';
$operations = array($operation);
$result = $campaignFeedService->mutate($operations);
foreach ($result->value as $savedCampaignFeed) {
printf(
"Campaign with ID %d was associated with feed with ID %d.\n",
$savedCampaignFeed->campaignId,
$savedCampaignFeed->feedId
);
}
}
/**
* Creates a callFeedItem and wraps it in an ADD operation.
*
* @param map $callData IDs associated to created call feed metadata
* @param string $number The phone number to append to the ad.
* @param string $code Two character country code of the advertiser's phone number
* @param boolean $only Indicates whether the ad is call only (phone number is displayed and the URL will not)
*/
protected function newCallFeedItemAddOperation($callData, $number, $code, $only)
{
// Create the FeedItemAttributeValues for our text values.
$callNumberAttributeValue = new FeedItemAttributeValue();
$callNumberAttributeValue->feedAttributeId = $callData['callNumberFeedAttributeId'];
$callNumberAttributeValue->stringValue = $number;
$callCodeAttributeValue = new FeedItemAttributeValue();
$callCodeAttributeValue->feedAttributeId = $callData['callCodeFeedAttributeId'];
$callCodeAttributeValue->stringValue = $code;
$callOnlyAttributeValue = new FeedItemAttributeValue();
$callOnlyAttributeValue->feedAttributeId = $callData['callOnlyFeedAttribute'];
$callOnlyAttributeValue->stringValue = $only;
// Create the feed item and operation.
$item = new FeedItem();
$item->feedId = $callData['callFeedId'];
$item->attributeValues = [$callNumberAttributeValue, $callCodeAttributeValue, $callOnlyAttributeValue];
$operation = new FeedItemOperation();
$operation->operand = $item;
$operation->operator = 'ADD';
return $operation;
}
/**
* @param $operations
*/
protected function createAdGroup($name)
{
$adGroupService = $this->user->GetService('AdGroupService');
// Create ad group.
$adGroup = new AdGroup();
$adGroup->campaignId = $this->campaign;
$adGroup->name = $name;
$adGroup->status = 'PAUSED';
// Targetting restriction settings - these setting only affect serving
// for the Display Network.
$targetingSetting = new \TargetingSetting();
// Restricting to serve ads that match your ad group placements.
$targetingSetting->details[] = new \TargetingSettingDetail('PLACEMENT', true);
// Using your ad group verticals only for bidding.
$targetingSetting->details[] = new \TargetingSettingDetail('VERTICAL', false);
$adGroup->settings[] = $targetingSetting;
// Create operation.
$operation = new \AdGroupOperation();
$operation->operand = $adGroup;
$operation->operator = 'ADD';
$operations[] = $operation;
// Make the mutate request.
$result = $adGroupService->mutate($operations);
// Display result.
$adGroups = $result->value;
foreach ($adGroups as $adGroup) {
printf(
"Ad group with name '%s' and ID '%s' was added.\n",
$adGroup->name,
$adGroup->id
);
}
return $adGroups;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment