Пример #1
0
    /// <summary>
    /// Runs the code example.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    /// <param name="campaignId">Id of the campaign to which sitelinks will
    /// be added.</param>
    public void Run(AdWordsUser user, long campaignId) {
      // Get the CampaignExtensionSettingService.
      CampaignExtensionSettingService campaignExtensionSettingService =
          (CampaignExtensionSettingService) user.GetService(
               AdWordsService.v201409.CampaignExtensionSettingService);

      CustomerService customerService = (CustomerService) user.GetService(
          AdWordsService.v201409.CustomerService);
      Customer customer = customerService.get();
      
      // Create your sitelinks.
      SitelinkFeedItem sitelink1 = new SitelinkFeedItem() {
        sitelinkText = "Store Hours",
        sitelinkFinalUrls = new string[] { "http://www.example.com/storehours" }
      };

      // Show the Thanksgiving specials link only from 20 - 27 Nov.
      SitelinkFeedItem sitelink2 = new SitelinkFeedItem() {
        sitelinkText = "Thanksgiving Specials",
        sitelinkFinalUrls = new string[] { "http://www.example.com/thanksgiving" },
        startTime = string.Format("{0}1120 000000 {1}", DateTime.Now.Year, customer.dateTimeZone),
        endTime = string.Format("{0}1127 235959 {1}", DateTime.Now.Year, customer.dateTimeZone)
      };

      // Show the wifi details primarily for high end mobile users.
      SitelinkFeedItem sitelink3 = new SitelinkFeedItem() {
        sitelinkText = "Wifi available",
        sitelinkFinalUrls = new string[] { "http://www.example.com/mobile/wifi" },
        devicePreference = new FeedItemDevicePreference() {
          // See https://developers.google.com/adwords/api/docs/appendix/platforms
          // for device criteria IDs.
          devicePreference = 30001
        }
      };

      // Show the happy hours link only during Mon - Fri 6PM to 9PM.
      SitelinkFeedItem sitelink4 = new SitelinkFeedItem() {
        sitelinkText = "Happy hours",
        sitelinkFinalUrls = new string[] { "http://www.example.com/happyhours" },
        scheduling = new FeedItemSchedule[] {
            new FeedItemSchedule() {
                dayOfWeek = AdWords.v201409.DayOfWeek.MONDAY,
                startHour = 18,
                startMinute = MinuteOfHour.ZERO,
                endHour = 21,
                endMinute = MinuteOfHour.ZERO
            },
            new FeedItemSchedule() {
                dayOfWeek = AdWords.v201409.DayOfWeek.TUESDAY,
                startHour = 18,
                startMinute = MinuteOfHour.ZERO,
                endHour = 21,
                endMinute = MinuteOfHour.ZERO
            },
            new FeedItemSchedule() {
                dayOfWeek = AdWords.v201409.DayOfWeek.WEDNESDAY,
                startHour = 18,
                startMinute = MinuteOfHour.ZERO,
                endHour = 21,
                endMinute = MinuteOfHour.ZERO
            },
            new FeedItemSchedule() {
                dayOfWeek = AdWords.v201409.DayOfWeek.THURSDAY,
                startHour = 18,
                startMinute = MinuteOfHour.ZERO,
                endHour = 21,
                endMinute = MinuteOfHour.ZERO
            },
            new FeedItemSchedule() {
                dayOfWeek = AdWords.v201409.DayOfWeek.FRIDAY,
                startHour = 18,
                startMinute = MinuteOfHour.ZERO,
                endHour = 21,
                endMinute = MinuteOfHour.ZERO
            }
        }
      };

      // Create your campaign extension settings. This associates the sitelinks
      // to your campaign.
      CampaignExtensionSetting campaignExtensionSetting = new CampaignExtensionSetting();
      campaignExtensionSetting.campaignId = campaignId;
      campaignExtensionSetting.extensionType = FeedType.SITELINK;
      campaignExtensionSetting.extensionSetting = new ExtensionSetting() {
        extensions = new ExtensionFeedItem[] {
            sitelink1, sitelink2, sitelink3, sitelink4
        }
      };

      CampaignExtensionSettingOperation operation = new CampaignExtensionSettingOperation() {
        operand = campaignExtensionSetting,
        @operator = Operator.ADD
      };

      try {
        // Add the extensions.
        CampaignExtensionSettingReturnValue retVal = campaignExtensionSettingService.mutate(
            new CampaignExtensionSettingOperation[] { operation });

        // Display the results.
        if (retVal.value != null && retVal.value.Length > 0) {
          CampaignExtensionSetting newExtensionSetting = retVal.value[0];
          Console.WriteLine("Extension setting with type = {0} was added to campaign ID {1}.",
              newExtensionSetting.extensionType, newExtensionSetting.campaignId);
        } else {
          Console.WriteLine("No extension settings were created.");
        }
      } catch (Exception ex) {
        throw new System.ApplicationException("Failed to create extension settings.", ex);
      }
    }
    /// <summary>
    /// Creates the extension setting fo a list of feed items.
    /// </summary>
    /// <param name="user">The user for which extension settings are created.
    /// </param>
    /// <param name="feedItems">The list of all feed items.</param>
    /// <param name="campaignId">ID of the campaign to which extension settings
    /// are added.</param>
    /// <param name="feedItemIds">IDs of the feed items for which extension
    /// settings should be created.</param>
    /// <param name="platformRestrictions">The platform restrictions for the
    /// extension setting.</param>
    private static void CreateExtensionSetting(AdWordsUser user, Dictionary<long,
        SiteLinkFromFeed> feedItems, long campaignId, List<long> feedItemIds,
        ExtensionSettingPlatform platformRestrictions) {
      CampaignExtensionSetting extensionSetting = new CampaignExtensionSetting() {
        campaignId = campaignId,
        extensionType = FeedType.SITELINK,
        extensionSetting = new ExtensionSetting() {
        }
      };

      List<ExtensionFeedItem> extensionFeedItems = new List<ExtensionFeedItem>();

      foreach (long feedItemId in feedItemIds) {
        SiteLinkFromFeed feedItem = feedItems[feedItemId];
        SitelinkFeedItem newFeedItem = new SitelinkFeedItem() {
          sitelinkText = feedItem.Text,
          sitelinkUrl = feedItem.Url,
          sitelinkFinalUrls = feedItem.FinalUrls,
          sitelinkFinalMobileUrls = feedItem.FinalMobileUrls,
          sitelinkTrackingUrlTemplate = feedItem.TrackingUrlTemplate,
          sitelinkLine2 = feedItem.Line2,
          sitelinkLine3 = feedItem.Line3,
          scheduling = feedItem.Scheduling
        };

        extensionFeedItems.Add(newFeedItem);
      }
      extensionSetting.extensionSetting.extensions = extensionFeedItems.ToArray();
      extensionSetting.extensionSetting.platformRestrictions = platformRestrictions;
      extensionSetting.extensionType = FeedType.SITELINK;

      CampaignExtensionSettingService campaignExtensionSettingService =
          (CampaignExtensionSettingService) user.GetService(
              AdWordsService.v201409.CampaignExtensionSettingService);
      CampaignExtensionSettingOperation operation = new CampaignExtensionSettingOperation() {
        operand = extensionSetting,
        @operator = Operator.ADD
      };

      campaignExtensionSettingService.mutate(new CampaignExtensionSettingOperation[] { operation });
      return;
    }