/// <summary> /// Creates the DSA URL add operation. /// </summary> /// <param name="details">The page feed details.</param> /// <param name="url">The DSA page feed URL.</param> /// <param name="label">DSA page feed label.</param> /// <returns>The DSA URL add operation.</returns> private static FeedItemOperation CreateDsaUrlAddOperation(DSAFeedDetails details, string url, string label) { // Create the FeedItemAttributeValues for our text values. FeedItemAttributeValue urlAttributeValue = new FeedItemAttributeValue(); urlAttributeValue.feedAttributeId = details.urlAttributeId; // Optional: Add the {feeditem} valuetrack parameter to track which page feed items lead // to each click. url = url + "?id={feeditem}"; urlAttributeValue.stringValues = new string[] { url }; FeedItemAttributeValue labelAttributeValue = new FeedItemAttributeValue(); labelAttributeValue.feedAttributeId = details.labelAttributeId; labelAttributeValue.stringValues = new string[] { label }; // Create the feed item and operation. FeedItem item = new FeedItem(); item.feedId = details.feedId; item.attributeValues = new FeedItemAttributeValue[] { urlAttributeValue, labelAttributeValue }; FeedItemOperation operation = new FeedItemOperation(); operation.operand = item; operation.@operator = Operator.ADD; return(operation); }
/// <summary> /// Creates the feed mapping for DSA page feeds. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="feedDetails">The feed details.</param> private static void CreateFeedMapping(AdWordsUser user, DSAFeedDetails feedDetails) { using (FeedMappingService feedMappingService = (FeedMappingService)user.GetService(AdWordsService.v201802.FeedMappingService)) { // Map the FeedAttributeIds to the fieldId constants. AttributeFieldMapping urlFieldMapping = new AttributeFieldMapping(); urlFieldMapping.feedAttributeId = feedDetails.urlAttributeId; urlFieldMapping.fieldId = DSA_PAGE_URLS_FIELD_ID; AttributeFieldMapping labelFieldMapping = new AttributeFieldMapping(); labelFieldMapping.feedAttributeId = feedDetails.labelAttributeId; labelFieldMapping.fieldId = DSA_LABEL_FIELD_ID; // Create the FieldMapping and operation. FeedMapping feedMapping = new FeedMapping(); feedMapping.criterionType = DSA_PAGE_FEED_CRITERION_TYPE; feedMapping.feedId = feedDetails.feedId; feedMapping.attributeFieldMappings = new AttributeFieldMapping[] { urlFieldMapping, labelFieldMapping }; FeedMappingOperation operation = new FeedMappingOperation(); operation.operand = feedMapping; operation.@operator = Operator.ADD; try { // Add the field mapping. feedMappingService.mutate(new FeedMappingOperation[] { operation }); return; } catch (Exception e) { throw new System.ApplicationException("Failed to create feed mapping.", e); } } }
/// <summary> /// Creates the DSA URL add operation. /// </summary> /// <param name="details">The page feed details.</param> /// <param name="url">The DSA page feed URL.</param> /// <param name="label">DSA page feed label.</param> /// <returns>The DSA URL add operation.</returns> private static FeedItemOperation CreateDsaUrlAddOperation(DSAFeedDetails details, string url, string label) { // Create the FeedItemAttributeValues for our text values. FeedItemAttributeValue urlAttributeValue = new FeedItemAttributeValue { feedAttributeId = details.urlAttributeId, // See https://support.google.com/adwords/answer/7166527 for page feed URL recommendations // and rules. stringValues = new string[] { url } }; FeedItemAttributeValue labelAttributeValue = new FeedItemAttributeValue { feedAttributeId = details.labelAttributeId, stringValues = new string[] { label } }; // Create the feed item and operation. FeedItem item = new FeedItem { feedId = details.feedId, attributeValues = new FeedItemAttributeValue[] { urlAttributeValue, labelAttributeValue } }; FeedItemOperation operation = new FeedItemOperation { operand = item, @operator = Operator.ADD }; return(operation); }
/// <summary> /// Creates the page URLs in the DSA page feed. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="feedDetails">The feed details.</param> /// <param name="labelName">The pagefeed url label.</param> private static void CreateFeedItems(AdWordsUser user, DSAFeedDetails feedDetails, string labelName) { using (FeedItemService feedItemService = (FeedItemService)user.GetService( AdWordsService.v201802.FeedItemService)) { FeedItemOperation[] operations = new FeedItemOperation[] { CreateDsaUrlAddOperation(feedDetails, "http://www.example.com/discounts/rental-cars", labelName), CreateDsaUrlAddOperation(feedDetails, "http://www.example.com/discounts/hotel-deals", labelName), CreateDsaUrlAddOperation(feedDetails, "http://www.example.com/discounts/flight-deals", labelName), }; feedItemService.mutate(operations); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">The DSA campaign ID.</param> /// <param name="adGroupId">The DSA ad group ID.</param> public void Run(AdWordsUser user, long campaignId, long adGroupId) { string dsaPageUrlLabel = "discounts"; // Get the page feed details. This code example creates a new feed, but you can // fetch and re-use an existing feed. DSAFeedDetails feedDetails = CreateFeed(user); CreateFeedMapping(user, feedDetails); CreateFeedItems(user, feedDetails, dsaPageUrlLabel); // Associate the page feed with the campaign. UpdateCampaignDsaSetting(user, campaignId, feedDetails.feedId); // Optional: Target web pages matching the feed's label in the ad group. AddDsaTargeting(user, adGroupId, dsaPageUrlLabel); Console.WriteLine("Dynamic page feed setup is complete for campaign ID '{0}'.", campaignId); }