// These mappings are taken from the placeholders reference page:
// https://developers.google.com/adwords/api/docs/appendix/placeholders#call
public static final int PHONE_NUMBER = 1;
public static final int COUNTRY_CODE = 2;
public static final int TRACKED = 3;
public static final int CONVERSION_TYPE_ID = 6;
// Let's assume the basic objects are there
AdWordsServices adWordsServices = ...;
AdWordsSession session = ...;
// It is up to you to determine the correct feed item for the call
// extension you want to migrate.
FeedItem feedItem = ...;
// This is the feed mapping that you are using for your call extensions.
FeedMapping mapping = ...;
// We need to make a temporary map to avoid repeatedly searching through
// the AttributeFieldMappings array.
Map<Long, Integer> placeholderIdMapping = new HashMap<Long, Integer>();
AttributeFieldMapping[] attributeFieldMappings =
mapping.getAttributeFieldMappings();
for (int i = 0; i < attributeFieldMappings.length; i++){
placeholderIdMapping.put(
attributeFieldMappings[i].getFeedAttributeId(),
attributeFieldMappings[i].getFieldId()
);
}
// This map will map the placeholder ID to the value for this feed item.
// This will be useful later when populating the new ad.
Map<Integer, Object> placeholderValueMapping = new HashMap<Integer, Object>();
FeedItemAttributeValue[] attributes = feedItem.getAttributeValues();
for (int i = 0; i < attributes.length; i++){
switch (placeholderIdMapping.get(attributes[i].getFeedAttributeId())) {
case PHONE_NUMBER:
placeholderValueMapping.put(PHONE_NUMBER,
attributes[i].getStringValue());
break;
case COUNTRY_CODE:
placeholderValueMapping.put(COUNTRY_CODE,
attributes[i].getStringValue());
break;
case TRACKED:
placeholderValueMapping.put(TRACKED,
attributes[i].getBooleanValue());
break;
case CONVERSION_TYPE_ID:
placeholderValueMapping.put(CONVERSION_TYPE_ID,
attributes[i].getIntegerValue());
break;
}
}
// This is the previous text ad that you want to copy into your new
// CallOnlyAd.
TextAd previousAd = ...;
AdGroupAdServiceInterface adGroupAdService =
adWordsServices.get(session, AdGroupAdServiceInterface.class);
CallOnlyAd ad = new CallOnlyAd();
ad.setDescription1(previousAd.getDescription1());
ad.setDescription2(previousAd.getDescription2());
ad.setDisplayUrl(previousAd.getDisplayUrl());
ad.setFinalUrls(previousAd.getFinalUrls());
ad.setCountryCode((String) placeholderValueMapping.get(COUNTRY_CODE));
ad.setPhoneNumber((String) placeholderValueMapping.get(PHONE_NUMBER));
ad.setBusinessName("Some name");
ad.setDisableCallConversion(!
((Boolean)placeholderValueMapping.get(TRACKED)));
ad.setConversionTypeId((Long)placeholderValueMapping.get(CONVERSION_TYPE_ID));
ad.setPhoneNumberVerificationUrl(previousAd.getDisplayUrl());
// It is up to you to determine the correct ad group where you want
// your new ad to reside.
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAdGroupId(...);
adGroupAd.setAd(ad);
// Set up the operation to send to the API.
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.setOperand(adGroupAd);
operation.setOperator(Operator.ADD);
AdGroupAdOperation[] operations = new AdGroupAdOperation[] {operation};
// Execute the operation and fetch the results.
AdGroupAdReturnValue result = adGroupAdService.mutate(operations);