예제 #1
0
        /// <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);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Gets the feed mapping for a feed.
        /// </summary>
        /// <param name="user">The user that owns the feed.</param>
        /// <param name="feedId">The feed ID.</param>
        /// <param name="placeHolderType">Type of the place holder for which feed
        /// mappings should be retrieved.</param>
        /// <returns>A dictionary, with key as the feed attribute ID, and value as
        /// the set of all fields which the attribute has a mapping to.</returns>
        private Dictionary <long, HashSet <long> > GetFeedMapping(AdWordsUser user, long feedId,
                                                                  long placeHolderType)
        {
            FeedMappingService feedMappingService = (FeedMappingService)user.GetService(
                AdWordsService.v201609.FeedMappingService);
            FeedMappingPage page = feedMappingService.query(string.Format("SELECT FeedMappingId, " +
                                                                          "AttributeFieldMappings where FeedId='{0}' and PlaceholderType={1} and Status='ENABLED'",
                                                                          feedId, placeHolderType));

            Dictionary <long, HashSet <long> > attributeMappings = new Dictionary <long, HashSet <long> >();

            if (page.entries != null)
            {
                // Normally, a feed attribute is mapped only to one field. However,
                // you may map it to more than one field if needed.
                foreach (FeedMapping feedMapping in page.entries)
                {
                    foreach (AttributeFieldMapping attributeMapping in feedMapping.attributeFieldMappings)
                    {
                        if (!attributeMappings.ContainsKey(attributeMapping.feedAttributeId))
                        {
                            attributeMappings[attributeMapping.feedAttributeId] = new HashSet <long>();
                        }
                        attributeMappings[attributeMapping.feedAttributeId].Add(attributeMapping.fieldId);
                    }
                }
            }
            return(attributeMappings);
        }
        private static void createSitelinksFeedMapping(AdWordsUser user,
                                                       SitelinksDataHolder sitelinksData)
        {
            using (FeedMappingService feedMappingService =
                       (FeedMappingService)user.GetService(AdWordsService.v201806.FeedMappingService)) {
                // Map the FeedAttributeIds to the fieldId constants.
                AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping()
                {
                    feedAttributeId = sitelinksData.LinkTextFeedAttributeId,
                    fieldId         = PLACEHOLDER_FIELD_SITELINK_LINK_TEXT
                };

                AttributeFieldMapping linkFinalUrlFieldMapping = new AttributeFieldMapping()
                {
                    feedAttributeId = sitelinksData.LinkFinalUrlFeedAttributeId,
                    fieldId         = PLACEHOLDER_FIELD_SITELINK_FINAL_URL
                };

                AttributeFieldMapping line2FieldMapping = new AttributeFieldMapping()
                {
                    feedAttributeId = sitelinksData.Line2FeedAttributeId,
                    fieldId         = PLACEHOLDER_FIELD_LINE_2_TEXT
                };

                AttributeFieldMapping line3FieldMapping = new AttributeFieldMapping()
                {
                    feedAttributeId = sitelinksData.Line3FeedAttributeId,
                    fieldId         = PLACEHOLDER_FIELD_LINE_3_TEXT
                };

                // Create the FieldMapping and operation.
                FeedMappingOperation operation = new FeedMappingOperation()
                {
                    operand = new FeedMapping()
                    {
                        placeholderType        = PLACEHOLDER_SITELINKS,
                        feedId                 = sitelinksData.FeedId,
                        attributeFieldMappings = new AttributeFieldMapping[] {
                            linkTextFieldMapping, linkFinalUrlFieldMapping, line2FieldMapping, line3FieldMapping
                        }
                    },
                    @operator = Operator.ADD
                };

                // Save the field mapping.
                FeedMappingReturnValue result =
                    feedMappingService.mutate(new FeedMappingOperation[] { operation });

                foreach (FeedMapping savedFeedMapping in result.value)
                {
                    Console.WriteLine(
                        "Feed mapping with ID {0} and placeholderType {1} was saved for feed with ID {2}.",
                        savedFeedMapping.feedMappingId, savedFeedMapping.placeholderType,
                        savedFeedMapping.feedId);
                }
            }
        }
        /// <summary>
        /// Creates a new FeedMapping that indicates how the data holder's feed
        /// should be interpreted in the context of ad customizers.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="dataHolder">The data holder that contains metadata about
        /// the customizer Feed.</param>
        private static void CreateFeedMapping(AdWordsUser user, CustomizersDataHolder dataHolder)
        {
            // Get the FeedMappingService.
            FeedMappingService feedMappingService = (FeedMappingService)user.GetService(
                AdWordsService.v201406.FeedMappingService);

            FeedMapping feedMapping = new FeedMapping();

            feedMapping.feedId          = dataHolder.FeedId;
            feedMapping.placeholderType = PLACEHOLDER_AD_CUSTOMIZER;

            List <AttributeFieldMapping> attributeFieldMappings = new List <AttributeFieldMapping>();
            AttributeFieldMapping        attributeFieldMapping;

            attributeFieldMapping = new AttributeFieldMapping();
            attributeFieldMapping.feedAttributeId = dataHolder.NameFeedAttributeId;
            attributeFieldMapping.fieldId         = PLACEHOLDER_FIELD_STRING;
            attributeFieldMappings.Add(attributeFieldMapping);

            attributeFieldMapping = new AttributeFieldMapping();
            attributeFieldMapping.feedAttributeId = dataHolder.PriceFeedAttributeId;
            attributeFieldMapping.fieldId         = PLACEHOLDER_FIELD_PRICE;
            attributeFieldMappings.Add(attributeFieldMapping);

            attributeFieldMapping = new AttributeFieldMapping();
            attributeFieldMapping.feedAttributeId = dataHolder.DateFeedAttributeId;
            attributeFieldMapping.fieldId         = PLACEHOLDER_FIELD_DATE;
            attributeFieldMappings.Add(attributeFieldMapping);

            feedMapping.attributeFieldMappings = attributeFieldMappings.ToArray();

            FeedMappingOperation feedMappingOperation = new FeedMappingOperation();

            feedMappingOperation.operand   = feedMapping;
            feedMappingOperation.@operator = Operator.ADD;

            FeedMapping addedFeedMapping =
                feedMappingService.mutate(new FeedMappingOperation[] { feedMappingOperation }).value[0];

            Console.WriteLine("Feed mapping with ID {0} and placeholder type {1} was added for " +
                              "feed with ID {2}.",
                              addedFeedMapping.feedMappingId, addedFeedMapping.placeholderType,
                              addedFeedMapping.feedId);
        }
예제 #5
0
        private static void createSiteLinksFeedMapping(
            AdWordsUser user, SiteLinksDataHolder siteLinksData)
        {
            // Get the FeedItemService.
            FeedMappingService feedMappingService =
                (FeedMappingService)user.GetService(AdWordsService.v201406.FeedMappingService);

            // Map the FeedAttributeIds to the fieldId constants.
            AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping();

            linkTextFieldMapping.feedAttributeId = siteLinksData.LinkTextFeedAttributeId;
            linkTextFieldMapping.fieldId         = PLACEHOLDER_FIELD_SITELINK_LINK_TEXT;
            AttributeFieldMapping linkUrlFieldMapping = new AttributeFieldMapping();

            linkUrlFieldMapping.feedAttributeId = siteLinksData.LinkUrlFeedAttributeId;
            linkUrlFieldMapping.fieldId         = PLACEHOLDER_FIELD_SITELINK_URL;

            // Create the FieldMapping and operation.
            FeedMapping feedMapping = new FeedMapping();

            feedMapping.placeholderType        = PLACEHOLDER_SITELINKS;
            feedMapping.feedId                 = siteLinksData.SiteLinksFeedId;
            feedMapping.attributeFieldMappings =
                new AttributeFieldMapping[] { linkTextFieldMapping, linkUrlFieldMapping };
            FeedMappingOperation operation = new FeedMappingOperation();

            operation.operand   = feedMapping;
            operation.@operator = Operator.ADD;

            // Save the field mapping.
            FeedMappingReturnValue result =
                feedMappingService.mutate(new FeedMappingOperation[] { operation });

            foreach (FeedMapping savedFeedMapping in result.value)
            {
                Console.WriteLine(
                    "Feed mapping with ID {0} and placeholderType {1} was saved for feed with ID {2}.",
                    savedFeedMapping.feedMappingId, savedFeedMapping.placeholderType,
                    savedFeedMapping.feedId);
            }
        }