/// <summary> /// Creates a campaign feed, which tells Google Ads which campaigns to use the provided /// data with. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The customer ID for which the call is made.</param> /// <param name="campaignId">The campaign to receive the feed.</param> /// <param name="feed">The feed to connect to the campaign.</param> private void CreateCampaignFeed(GoogleAdsClient client, long customerId, long campaignId, Feed feed) { CampaignFeedServiceClient campaignFeedServiceClient = client.GetService(Services.V4.CampaignFeedService); // Fetch the Feed Item IDs and collapse them into a single comma-separated string. List <long> feedItemIds = feed.Attributes.Select(attr => attr.Id.Value).ToList(); string aggregatedFeedItemIds = string.Join(",", feedItemIds); CampaignFeed campaignFeed = new CampaignFeed() { Feed = feed.ResourceName, Campaign = ResourceNames.Campaign(customerId, campaignId), MatchingFunction = new MatchingFunction() { FunctionString = $"AND(IN(FEED_ITEM_ID,{{ {aggregatedFeedItemIds} }})," + "EQUALS(CONTEXT.DEVICE,'Mobile'))" } }; campaignFeed.PlaceholderTypes.Add(PlaceholderTypeEnum.Types.PlaceholderType.Sitelink); CampaignFeedOperation operation = new CampaignFeedOperation() { Create = campaignFeed }; MutateCampaignFeedsResponse response = campaignFeedServiceClient.MutateCampaignFeeds( customerId.ToString(), new[] { operation }); Console.WriteLine($"Created campaign feed '{response.Results.First().ResourceName}'"); }
/// <summary> /// Deletes a campaign feed. /// </summary> /// <param name="user">The user.</param> /// <param name="campaignFeed">The campaign feed.</param> /// <returns></returns> private CampaignFeed DeleteCampaignFeed(AdWordsUser user, CampaignFeed campaignFeed) { CampaignFeedService campaignFeedService = (CampaignFeedService) user.GetService( AdWordsService.v201509.CampaignFeedService); CampaignFeedOperation operation = new CampaignFeedOperation() { operand = campaignFeed, @operator = Operator.REMOVE }; return campaignFeedService.mutate(new CampaignFeedOperation[] { operation }).value[0]; }
private static void CreateSitelinksCampaignFeed(AdWordsUser user, SitelinksDataHolder sitelinksData, long campaignId) { using (CampaignFeedService campaignFeedService = (CampaignFeedService)user.GetService(AdWordsService.v201809.CampaignFeedService)) { // Construct a matching function that associates the sitelink feeditems // to the campaign, and set the device preference to Mobile. See the // matching function guide at // https://developers.google.com/adwords/api/docs/guides/feed-matching-functions // for more details. string matchingFunctionString = string.Format(@" AND( IN(FEED_ITEM_ID, {{{0}}}), EQUALS(CONTEXT.DEVICE, 'Mobile') )", string.Join(",", sitelinksData.FeedItemIds)); CampaignFeed campaignFeed = new CampaignFeed() { feedId = sitelinksData.FeedId, campaignId = campaignId, matchingFunction = new Function() { functionString = matchingFunctionString }, // Specifying placeholder types on the CampaignFeed allows the same feed // to be used for different placeholders in different Campaigns. placeholderTypes = new int[] { PLACEHOLDER_SITELINKS } }; CampaignFeedOperation operation = new CampaignFeedOperation() { operand = campaignFeed, @operator = Operator.ADD }; CampaignFeedReturnValue result = campaignFeedService.mutate( new CampaignFeedOperation[] { operation }); foreach (CampaignFeed savedCampaignFeed in result.value) { Console.WriteLine("Campaign with ID {0} was associated with feed with ID {1}", savedCampaignFeed.campaignId, savedCampaignFeed.feedId); } } }
/// <summary>Snippet for MutateCampaignFeeds</summary> /// <remarks> /// This snippet has been automatically generated for illustrative purposes only. /// It may require modifications to work in your environment. /// </remarks> public void MutateCampaignFeeds() { // Create client CampaignFeedServiceClient campaignFeedServiceClient = CampaignFeedServiceClient.Create(); // Initialize request argument(s) string customerId = ""; IEnumerable <CampaignFeedOperation> operations = new CampaignFeedOperation[] { new CampaignFeedOperation(), }; // Make the request MutateCampaignFeedsResponse response = campaignFeedServiceClient.MutateCampaignFeeds(customerId, operations); }
/// <summary> /// Deletes a campaign feed. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignFeed">The campaign feed.</param> /// <returns></returns> private CampaignFeed DeleteCampaignFeed(AdWordsUser user, CampaignFeed campaignFeed) { using (CampaignFeedService campaignFeedService = (CampaignFeedService)user.GetService( AdWordsService.v201710.CampaignFeedService)) { CampaignFeedOperation operation = new CampaignFeedOperation() { operand = campaignFeed, @operator = Operator.REMOVE }; CampaignFeed retval = campaignFeedService.mutate( new CampaignFeedOperation[] { operation }).value[0]; return(retval); } }
/// <summary>Snippet for MutateCampaignFeedsAsync</summary> public async Task MutateCampaignFeedsAsync() { // Snippet: MutateCampaignFeedsAsync(string, IEnumerable<CampaignFeedOperation>, CallSettings) // Additional: MutateCampaignFeedsAsync(string, IEnumerable<CampaignFeedOperation>, CancellationToken) // Create client CampaignFeedServiceClient campaignFeedServiceClient = await CampaignFeedServiceClient.CreateAsync(); // Initialize request argument(s) string customerId = ""; IEnumerable <CampaignFeedOperation> operations = new CampaignFeedOperation[] { new CampaignFeedOperation(), }; // Make the request MutateCampaignFeedsResponse response = await campaignFeedServiceClient.MutateCampaignFeedsAsync(customerId, operations); // End snippet }
/// <summary> /// Creates the campaign feed. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The customer ID for which the call is made.</param> /// <param name="campaignId">The campaign ID for which the affiliate location extensions /// are added.</param> /// <param name="feedMapping">The affliate location extension feedmapping for /// <paramref name="feedResourceName"/></param> /// <param name="feedResourceName">The feed resource name.</param> /// <param name="chainId">The retail chain ID.</param> private static void CreateCampaignFeed(GoogleAdsClient client, long customerId, long campaignId, FeedMapping feedMapping, string feedResourceName, long chainId) { // Get the CampaignFeedService. CampaignFeedServiceClient campaignFeedService = client.GetService( Services.V5.CampaignFeedService); long attributeIdForChainId = GetAttributeIdForChainId(feedMapping); string feedId = FeedName.Parse(feedResourceName).FeedId; string matchingFunction = $"IN(FeedAttribute[{feedId}, {attributeIdForChainId}], {chainId})"; // Adds a CampaignFeed that associates the feed with this campaign for // the AFFILIATE_LOCATION placeholder type. CampaignFeed campaignFeed = new CampaignFeed() { Feed = feedResourceName, PlaceholderTypes = { PlaceholderType.AffiliateLocation }, MatchingFunction = new MatchingFunction() { FunctionString = matchingFunction }, Campaign = ResourceNames.Campaign(customerId, campaignId), }; CampaignFeedOperation operation = new CampaignFeedOperation() { Create = campaignFeed }; MutateCampaignFeedsResponse campaignFeedsResponse = campaignFeedService.MutateCampaignFeeds( customerId.ToString(), new[] { operation }); // Displays the result. string addedCampaignFeed = campaignFeedsResponse.Results[0].ResourceName; Console.WriteLine($"Campaign feed created with resource name: {addedCampaignFeed}."); return; }
private static void createSiteLinksCampaignFeed(AdWordsUser user, SiteLinksDataHolder siteLinksData, long campaignId) { // Get the CampaignFeedService. CampaignFeedService campaignFeedService = (CampaignFeedService)user.GetService(AdWordsService.v201406.CampaignFeedService); // Map the feed item ids to the campaign using an IN operation. RequestContextOperand feedItemRequestContextOperand = new RequestContextOperand(); feedItemRequestContextOperand.contextType = RequestContextOperandContextType.FEED_ITEM_ID; List <FunctionArgumentOperand> feedItemOperands = new List <FunctionArgumentOperand>(); foreach (long feedItemId in siteLinksData.SiteLinkFeedItemIds) { ConstantOperand feedItemOperand = new ConstantOperand(); feedItemOperand.longValue = feedItemId; feedItemOperand.type = ConstantOperandConstantType.LONG; feedItemOperands.Add(feedItemOperand); } Function feedItemfunction = new Function(); feedItemfunction.lhsOperand = new FunctionArgumentOperand[] { feedItemRequestContextOperand }; feedItemfunction.@operator = FunctionOperator.IN; feedItemfunction.rhsOperand = feedItemOperands.ToArray(); // Optional: to target to a platform, define a function and 'AND' it with // the feed item ID link: RequestContextOperand platformRequestContextOperand = new RequestContextOperand(); platformRequestContextOperand.contextType = RequestContextOperandContextType.DEVICE_PLATFORM; ConstantOperand platformOperand = new ConstantOperand(); platformOperand.stringValue = "Mobile"; platformOperand.type = ConstantOperandConstantType.STRING; Function platformFunction = new Function(); platformFunction.lhsOperand = new FunctionArgumentOperand[] { platformRequestContextOperand }; platformFunction.@operator = FunctionOperator.EQUALS; platformFunction.rhsOperand = new FunctionArgumentOperand[] { platformOperand }; // Combine the two functions using an AND operation. FunctionOperand feedItemFunctionOperand = new FunctionOperand(); feedItemFunctionOperand.value = feedItemfunction; FunctionOperand platformFunctionOperand = new FunctionOperand(); platformFunctionOperand.value = platformFunction; Function combinedFunction = new Function(); combinedFunction.@operator = FunctionOperator.AND; combinedFunction.lhsOperand = new FunctionArgumentOperand[] { feedItemFunctionOperand, platformFunctionOperand }; CampaignFeed campaignFeed = new CampaignFeed(); campaignFeed.feedId = siteLinksData.SiteLinksFeedId; 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 = new int[] { PLACEHOLDER_SITELINKS }; CampaignFeedOperation operation = new CampaignFeedOperation(); operation.operand = campaignFeed; operation.@operator = Operator.ADD; CampaignFeedReturnValue result = campaignFeedService.mutate(new CampaignFeedOperation[] { operation }); foreach (CampaignFeed savedCampaignFeed in result.value) { Console.WriteLine("Campaign with ID {0} was associated with feed with ID {1}", savedCampaignFeed.campaignId, savedCampaignFeed.feedId); } }
private static void createSitelinksCampaignFeed(AdWordsUser user, SitelinksDataHolder sitelinksData, long campaignId) { // Get the CampaignFeedService. CampaignFeedService campaignFeedService = (CampaignFeedService) user.GetService(AdWordsService.v201509.CampaignFeedService); // Construct a matching function that associates the sitelink feeditems // to the campaign, and set the device preference to Mobile. See the // matching function guide at // https://developers.google.com/adwords/api/docs/guides/feed-matching-functions // for more details. string matchingFunctionString = string.Format(@" AND( IN(FEED_ITEM_ID, {{{0}}}), EQUALS(CONTEXT.DEVICE, 'Mobile') )", string.Join(",", sitelinksData.FeedItemIds)); CampaignFeed campaignFeed = new CampaignFeed() { feedId = sitelinksData.FeedId, campaignId = campaignId, matchingFunction = new Function() { functionString = matchingFunctionString }, // Specifying placeholder types on the CampaignFeed allows the same feed // to be used for different placeholders in different Campaigns. placeholderTypes = new int[] { PLACEHOLDER_SITELINKS } }; CampaignFeedOperation operation = new CampaignFeedOperation(); operation.operand = campaignFeed; operation.@operator = Operator.ADD; CampaignFeedReturnValue result = campaignFeedService.mutate(new CampaignFeedOperation[] {operation}); foreach (CampaignFeed savedCampaignFeed in result.value) { Console.WriteLine("Campaign with ID {0} was associated with feed with ID {1}", savedCampaignFeed.campaignId, savedCampaignFeed.feedId); } }