Feed with name 'MyFirstFeed556' and ID 32350825 with nameAttributeId Name 1 and priceAttributeId Price 2 and dateAttributeId Date 3 and inventoryId inventory 4 was created.
FeedItem with feedItemId 2803996854 was added.
FeedItem with feedItemId 2803996857 was added.
Feed mapping with ID 35313777 and placeholderType 10 was saved for feed with ID 32350825.
The matching function is EQUALS(FeedAttribute[32350825,1],"HUL")
Campaign with ID 321641246 was associated with feed with ID 32350825.
The saved campaignFeed from google is {"feedId":32350825,"campaignId":321641246,"matchingFunction":{"operator":{"value":"EQUALS"},"lhsOperand":[{"functionArgumentOperandType":"FeedAttributeOperand","feedId":32350825,"feedAttributeId":1}],"rhsOperand":[{"functionArgumentOperandType":"ConstantOperand","type":{"value":"STRING"},"unit":{"value":"NONE"},"longValue":null,"booleanValue":null,"doubleValue":null,"stringValue":"HUL"}],"functionString":"EQUALS(FeedAttribute[32350825,1],\"HUL\")"},"placeholderTypes":[10],"status":{"value":"ENABLED"}}
Ad with ID 77578950686 created.package feedService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.v201509.cm.AdGroupAd;
import com.google.api.ads.adwords.axis.v201509.cm.AdGroupAdOperation;
import com.google.api.ads.adwords.axis.v201509.cm.AdGroupAdReturnValue;
import com.google.api.ads.adwords.axis.v201509.cm.AdGroupAdServiceInterface;
import com.google.api.ads.adwords.axis.v201509.cm.AttributeFieldMapping;
import com.google.api.ads.adwords.axis.v201509.cm.CampaignFeed;
import com.google.api.ads.adwords.axis.v201509.cm.CampaignFeedOperation;
import com.google.api.ads.adwords.axis.v201509.cm.CampaignFeedReturnValue;
import com.google.api.ads.adwords.axis.v201509.cm.CampaignFeedServiceInterface;
import com.google.api.ads.adwords.axis.v201509.cm.Feed;
import com.google.api.ads.adwords.axis.v201509.cm.FeedAttribute;
import com.google.api.ads.adwords.axis.v201509.cm.FeedAttributeType;
import com.google.api.ads.adwords.axis.v201509.cm.FeedItem;
import com.google.api.ads.adwords.axis.v201509.cm.FeedItemAttributeValue;
import com.google.api.ads.adwords.axis.v201509.cm.FeedItemOperation;
import com.google.api.ads.adwords.axis.v201509.cm.FeedItemReturnValue;
import com.google.api.ads.adwords.axis.v201509.cm.FeedItemServiceInterface;
import com.google.api.ads.adwords.axis.v201509.cm.FeedMapping;
import com.google.api.ads.adwords.axis.v201509.cm.FeedMappingOperation;
import com.google.api.ads.adwords.axis.v201509.cm.FeedMappingReturnValue;
import com.google.api.ads.adwords.axis.v201509.cm.FeedMappingServiceInterface;
import com.google.api.ads.adwords.axis.v201509.cm.FeedOperation;
import com.google.api.ads.adwords.axis.v201509.cm.FeedOrigin;
import com.google.api.ads.adwords.axis.v201509.cm.FeedReturnValue;
import com.google.api.ads.adwords.axis.v201509.cm.FeedServiceInterface;
import com.google.api.ads.adwords.axis.v201509.cm.Function;
import com.google.api.ads.adwords.axis.v201509.cm.Operator;
import com.google.api.ads.adwords.axis.v201509.cm.TextAd;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
import com.google.api.ads.common.lib.exception.OAuthException;
import com.google.api.ads.common.lib.exception.ValidationException;
import com.google.api.client.auth.oauth2.Credential;
public class createCustomAds
{
private static AdWordsSession session;
private static AdWordsServices adWordsServices;
private static FeedServiceInterface feedService;
private static FeedItemServiceInterface feedItemService;
private static FeedMappingServiceInterface feedMappingService;
private static CampaignFeedServiceInterface campaignFeedService;
private static AdGroupAdServiceInterface adGroupAdService;
public static void main(String args[])
throws Exception
{
authenticator(
"8228023-m4aebescu.apps.googleusercontent.com", //clientId
"0e_XxougIVa6m", //clientSecret
"1/K4BUFieYZzR4gOqaili-jIRRPUoTe7Jo", //refreshToken
"V_3cYXomg9g", //developerToken
"offlineGooglePLAUploader", //userAgentName
"9918951314" //clientAccountId
);
String feedName = "MyFirstFeed556";
Long campaignId = 321641246L;
Long adGroupId = 23297006006L;
Long feedId = createAdCustmizationFeed(feedName);
FeedItemOperation operations[] = new FeedItemOperation[2];
/*
* I am inserting two rows in feed.
* Where "String 5" in Name
* 786 is Price
* 20140601 000000 is Date
* 45 is Inventory
*/
operations[0] =
newSiteLinkFeedItemAddOperation(feedId, "HUL", "786", "20140630 000000", 45L);
operations[1] =
newSiteLinkFeedItemAddOperation(feedId, "ITC", "786", "20140601 000000", 60L);
createAdCustomizationFeedItems(operations);
createAdCustomizationFeedMapping(feedId);
createAdCustomizationCampaignFeed(campaignId, feedId);
/*
* Where Name is parameter which needs to be taken from feed
*/
createAd(campaignId, adGroupId, feedName, "Name");
}
private static Long createAdCustmizationFeed(String feedName) throws Exception
{
// Create attributes.
FeedAttribute nameAttribute = new FeedAttribute();
/*
* String would be header type of first column and Link test would be header Name;
*/
nameAttribute.setType(FeedAttributeType.STRING);
nameAttribute.setName("Name");
FeedAttribute priceAttribute = new FeedAttribute();
priceAttribute.setType(FeedAttributeType.STRING);
priceAttribute.setName("Price");
FeedAttribute dateAttribute = new FeedAttribute();
dateAttribute.setType(FeedAttributeType.DATE_TIME);
dateAttribute.setName("Date");
FeedAttribute inventoryAttribute = new FeedAttribute();
inventoryAttribute.setName("inventory");
inventoryAttribute.setType(FeedAttributeType.INT64);
// Create the feed.
Feed siteLinksFeed = new Feed();
siteLinksFeed.setName(feedName);
siteLinksFeed.setAttributes(
new FeedAttribute[] {nameAttribute, priceAttribute, dateAttribute, inventoryAttribute});
siteLinksFeed.setOrigin(FeedOrigin.USER);
// Create operation.
FeedOperation operation = new FeedOperation();
operation.setOperand(siteLinksFeed);
operation.setOperator(Operator.ADD);
// Add the feed.
System.out.println("Making a google call");
FeedReturnValue result = feedService.mutate(new FeedOperation[] {operation});
Feed savedFeed = result.getValue()[0];
FeedAttribute[] savedAttributes = savedFeed.getAttributes();
System.out.printf("Feed with name '%s' and ID %d with nameAttributeId %s %d"
+ " and priceAttributeId %s %d and dateAttributeId %s %d and inventoryId %s %d"
+ " was created.\n",
savedFeed.getName(),
savedFeed.getId(),
savedAttributes[0].getName(),
savedAttributes[0].getId(),
savedAttributes[1].getName(),
savedAttributes[1].getId(),
savedAttributes[2].getName(),
savedAttributes[2].getId(),
savedAttributes[3].getName(),
savedAttributes[3].getId());
return savedFeed.getId();
}
private static void createAdCustomizationFeedItems(FeedItemOperation[] operations) throws Exception {
FeedItemReturnValue result = feedItemService.mutate(operations);
for (FeedItem item : result.getValue()) {
System.out.printf("FeedItem with feedItemId %d was added.\n", item.getFeedItemId());
}
}
private static FeedItemOperation newSiteLinkFeedItemAddOperation(
Long feedId, String name, String price, String date, Long inventory
)
{
FeedItemAttributeValue nameItemAttribute = new FeedItemAttributeValue();
/*
* Hardcoding 1. Its a column number
*/
nameItemAttribute.setFeedAttributeId(1L);
nameItemAttribute.setStringValue(name);
FeedItemAttributeValue priceItemAttribute = new FeedItemAttributeValue();
priceItemAttribute.setFeedAttributeId(2L);
priceItemAttribute.setStringValue(price);
FeedItemAttributeValue dateItemAttribute = new FeedItemAttributeValue();
dateItemAttribute.setFeedAttributeId(3L);
dateItemAttribute.setStringValue(date);
FeedItemAttributeValue inventoryttribute = new FeedItemAttributeValue();
inventoryttribute.setFeedAttributeId(4L);
inventoryttribute.setIntegerValue(inventory);
// Create the feed item and operation.
FeedItem item = new FeedItem();
item.setFeedId(feedId);
item.setAttributeValues(new FeedItemAttributeValue[] {nameItemAttribute,
priceItemAttribute, dateItemAttribute, inventoryttribute});
FeedItemOperation operation = new FeedItemOperation();
operation.setOperand(item);
operation.setOperator(Operator.ADD);
return operation;
}
/*
* Take this value from google
* LInk is https://developers.google.com/adwords/api/docs/appendix/placeholders#ad-customizers
*/
private static final int PLACEHOLDER_AD_CUSTOMIZERS = 10;
private static final int PLACEHOLOLDER_FIELD_INVENTORY_COUNT = 1;
private static final int PLACEHOLDER_FIELD_PRICE = 3;
private static final int PLACEHOLDER_FIELD_DATE = 4;
private static final int PLACEHOLDER_FIELD_NAME = 5;
private static void createAdCustomizationFeedMapping(Long feedId) throws Exception {
// Get the FeedItemService.
feedMappingService =
adWordsServices.get(session, FeedMappingServiceInterface.class);
// Map the FeedAttributeIds to the fieldId constants.
AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping();
linkTextFieldMapping.setFeedAttributeId(1L);
linkTextFieldMapping.setFieldId(PLACEHOLDER_FIELD_NAME);
AttributeFieldMapping linkFinalUrlFieldMapping = new AttributeFieldMapping();
linkFinalUrlFieldMapping.setFeedAttributeId(2L);
linkFinalUrlFieldMapping.setFieldId(PLACEHOLDER_FIELD_PRICE);
AttributeFieldMapping line1FieldMapping = new AttributeFieldMapping();
line1FieldMapping.setFeedAttributeId(3L);
line1FieldMapping.setFieldId(PLACEHOLDER_FIELD_DATE);
AttributeFieldMapping inventory = new AttributeFieldMapping();
inventory.setFeedAttributeId(4L);
inventory.setFieldId(PLACEHOLOLDER_FIELD_INVENTORY_COUNT);
// Create the FeedMapping and operation.
FeedMapping feedMapping = new FeedMapping();
feedMapping.setPlaceholderType(PLACEHOLDER_AD_CUSTOMIZERS);
feedMapping.setFeedId(feedId);
feedMapping.setAttributeFieldMappings(new AttributeFieldMapping[] {linkTextFieldMapping,
linkFinalUrlFieldMapping, inventory});
FeedMappingOperation operation = new FeedMappingOperation();
operation.setOperand(feedMapping);
operation.setOperator(Operator.ADD);
// Save the field mapping.
FeedMappingReturnValue result =
feedMappingService.mutate(new FeedMappingOperation[] {operation});
for (FeedMapping savedFeedMapping : result.getValue()) {
System.out.printf(
"Feed mapping with ID %d and placeholderType %d was saved for feed with ID %d.\n",
savedFeedMapping.getFeedMappingId(), savedFeedMapping.getPlaceholderType(),
savedFeedMapping.getFeedId());
}
}
private static void createAdCustomizationCampaignFeed(Long campaignId, Long feedId) throws Exception {
//String matchingFunctionString = String.format("IDENTITY(TRUE)");
//String matchingFunctionString = "CONTAINS_ANY(FeedAttribute[" + feedId + ",1],{\"String 5\"})";
String matchingFunctionString = "EQUALS(FeedAttribute[" + feedId + ",1],\"HUL\")";
System.out.println("The matching function is " + matchingFunctionString);
CampaignFeed campaignFeed = new CampaignFeed();
Function matchingFunction = new Function();
matchingFunction.setFunctionString(matchingFunctionString);
campaignFeed.setMatchingFunction(matchingFunction);
campaignFeed.setFeedId(feedId);
campaignFeed.setCampaignId(campaignId);
campaignFeed.setPlaceholderTypes(new int[] {PLACEHOLDER_AD_CUSTOMIZERS});
CampaignFeedOperation operation = new CampaignFeedOperation();
operation.setOperand(campaignFeed);
operation.setOperator(Operator.ADD);
CampaignFeedReturnValue result =
campaignFeedService.mutate(new CampaignFeedOperation[] {operation});
for (CampaignFeed savedCampaignFeed : result.getValue()) {
System.out.printf("Campaign with ID %d was associated with feed with ID %d.\n",
savedCampaignFeed.getCampaignId(), savedCampaignFeed.getFeedId());
}
ObjectMapper objectMapper = new ObjectMapper();
System.out.println("The saved campaignFeed from google is " + objectMapper.writeValueAsString(result.getValue(0)));
}
private static void createAd(Long campaignId, Long adGroupId, String feedName, String parameterName)
throws Exception
{
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAdGroupId(adGroupId);
TextAd ad = new TextAd();
ad.setHeadline("headLine{=" + feedName + "." + parameterName + "}");
ad.setDescription1("description1{=" + feedName + "." + parameterName + "}");
ad.setDescription2("description2{=" + feedName + "." + parameterName + "}");
ad.setDisplayUrl("www.sokrati.com");
ad.setFinalUrls(new String[] {"http://www.sokrati.com"});
adGroupAd.setAd(ad);
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.setOperand(adGroupAd);
operation.setOperator(Operator.ADD);
AdGroupAdReturnValue result =
adGroupAdService.mutate(new AdGroupAdOperation[] {operation});
for (AdGroupAd adGRoupAd : result.getValue()) {
System.out.printf("Ad with ID %d created.\n",
adGRoupAd.getAd().getId());
}
}
/*
* To create adwords session from tokens and credentials
*/
public static void authenticator(
String clientId, String clientSecret,
String refreshToken, String developerToken,
String userAgentName, String clientAccountId
)
throws OAuthException, ValidationException
{
Credential oAuth2Credential =
new OfflineCredentials.Builder().
forApi(Api.ADWORDS).
withClientSecrets(clientId, clientSecret).
withRefreshToken(refreshToken).
build().generateCredential();
if(null != oAuth2Credential)
{
session = new AdWordsSession.Builder().
withDeveloperToken(developerToken).
withUserAgent(userAgentName).
withOAuth2Credential(oAuth2Credential).
build();
if(null != session)
{
session.setClientCustomerId(clientAccountId);
serviceInitializer();
}
}
}
/*
* To initianlize various services
*/
private static void serviceInitializer()
{
adWordsServices = new AdWordsServices();
feedService =
adWordsServices.get(session, FeedServiceInterface.class);
feedItemService =
adWordsServices.get(session, FeedItemServiceInterface.class);
feedMappingService =
adWordsServices.get(session, FeedMappingServiceInterface.class);
campaignFeedService =
adWordsServices.get(session, CampaignFeedServiceInterface.class);
adGroupAdService =
adWordsServices.get(session, AdGroupAdServiceInterface.class);
}
}
Hi,
-- I want to try ad customizer. I created feed and added 2 feedItems. One feedItem has Name as ITC and another Name as HUL. Now, I am mapping campaign to feed with matching function as EQUALS(FeedAttribute[32350825,1],"HUL"). Then I created a new ad with headline as 'headline{=MyFrstFeed556.Name}'
I was anticipating one ad with headline 'headlineHUL' but its generating two ads with both HUL and ITC.
I am attaching sample code(with changed credentials. The campaignId, adGroupId and accountId is not changed.), output of code, snapshot of feed and snapshot of two ads.
I have tried matching function with EQUALS, CONTAINS_ANY , but it didn't help. Can you check why matching function is not working?
Am I passing wrong matching function?
Also, I would like to know how to check and update matching function on UI.
Thanks,
Kainesh
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+:
https://googleadsdeveloper.blogspot.com/
https://plus.google.com/+GoogleAdsDevelopers/posts
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to adwords-api@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups "AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-api+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/adwords-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-api/35277e64-260c-43f4-b82c-01a0a78f1b95%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



No comments:
Post a Comment