/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">Id of the camapign to which sitelinks are /// added.</param> public void Run(AdWordsUser user, long campaignId) { // Get the CampaignAdExtensionService. CampaignAdExtensionService campaignExtensionService = (CampaignAdExtensionService)user.GetService(AdWordsService.v201306. CampaignAdExtensionService); // Create the sitelinks. SitelinksExtension siteLinkExtension = new SitelinksExtension(); Sitelink siteLink1 = new Sitelink(); siteLink1.displayText = "Music"; siteLink1.destinationUrl = "http://www.example.com/music"; Sitelink siteLink2 = new Sitelink(); siteLink2.displayText = "DVDs"; siteLink2.destinationUrl = "http://www.example.com/dvds"; Sitelink siteLink3 = new Sitelink(); siteLink3.displayText = "New albums"; siteLink3.destinationUrl = "http://www.example.com/albums/new"; siteLinkExtension.sitelinks = new Sitelink[] {siteLink1, siteLink2, siteLink3}; CampaignAdExtension campaignAdExtension = new CampaignAdExtension(); campaignAdExtension.adExtension = siteLinkExtension; campaignAdExtension.campaignId = campaignId; // Create the operation. CampaignAdExtensionOperation operation = new CampaignAdExtensionOperation(); operation.@operator = Operator.ADD; operation.operand = campaignAdExtension; try { // Create the sitelinks. CampaignAdExtensionReturnValue retVal = campaignExtensionService.mutate(new CampaignAdExtensionOperation[] {operation}); // Display the results. if (retVal != null && retVal.value != null && retVal.value.Length > 0) { CampaignAdExtension campaignExtension = retVal.value[0]; Console.WriteLine("Created a campaign ad extension with id = \"{0}\" and " + "status = \"{1}\"", campaignExtension.adExtension.id, campaignExtension.status); foreach (Sitelink siteLink in (campaignExtension.adExtension as SitelinksExtension).sitelinks) { Console.WriteLine("-- Site link text is \"{0}\" and destination url is {1}", siteLink.displayText, siteLink.destinationUrl); } } else { Console.WriteLine("No sitelinks were created."); } } catch (Exception ex) { throw new System.ApplicationException("Failed to add site links.", ex); } }
/// <summary> /// Delete legacy sitelinks from a campaign. /// </summary> /// <param name="campaignExtensionService">The campaign extension service. /// </param> /// <param name="extensionToDelete">The CampaignAdExtension that holds /// legacy sitelinks.</param> private static void deleteLegacySitelinks(CampaignAdExtensionService campaignExtensionService, CampaignAdExtension extensionToDelete) { CampaignAdExtensionOperation operation = new CampaignAdExtensionOperation(); operation.@operator = Operator.REMOVE; operation.operand = extensionToDelete; campaignExtensionService.mutate(new CampaignAdExtensionOperation[] {operation}); }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">Id of the campaign to which location /// extensions are added.</param> public void Run(AdWordsUser user, long campaignId) { // Get the CampaignAdExtensionService. CampaignAdExtensionService campaignExtensionService = (CampaignAdExtensionService) user.GetService(AdWordsService.v201306. CampaignAdExtensionService); // Add location 1: 1600 Amphitheatre Pkwy, Mountain View, US. Address address1 = new Address(); address1.streetAddress = "1600 Amphitheatre Parkway"; address1.cityName = "Mountain View"; address1.provinceCode = "CA"; address1.postalCode = "94043"; address1.countryCode = "US"; // Add location 2: 38 avenue de l'Opéra, 75002 Paris, FR. Address address2 = new Address(); address2.streetAddress = "38 avenue de l'Opéra"; address2.cityName = "Paris"; address2.postalCode = "75002"; address2.countryCode = "FR"; // Get the GeoLocationService. GeoLocationService geoService = (GeoLocationService) user.GetService(AdWordsService.v201306.GeoLocationService); // Create the selector. GeoLocationSelector selector = new GeoLocationSelector(); selector.addresses = new Address[] {address1, address2}; // Retrieve the locations. GeoLocation[] locations = geoService.get(selector); List<CampaignAdExtensionOperation> operations = new List<CampaignAdExtensionOperation>(); // Phone numbers for US and FR offices. string[] phoneNumbers = new string[] {"(650) 253-0000", "(0)1 42 68 53 00"}; int index = 0; // Create a location extension for each geo location returned by the // server. foreach (GeoLocation location in locations) { LocationExtension locationExtension = new LocationExtension(); locationExtension.address = location.address; locationExtension.geoPoint = location.geoPoint; locationExtension.encodedLocation = location.encodedLocation; locationExtension.source = LocationExtensionSource.ADWORDS_FRONTEND; // Optional: Set the company name. locationExtension.companyName = "ACME Inc."; // Optional: Set the phone number. locationExtension.phoneNumber = phoneNumbers[index]; index++; CampaignAdExtension extension = new CampaignAdExtension(); extension.campaignId = campaignId; extension.status = CampaignAdExtensionStatus.ACTIVE; extension.adExtension = locationExtension; CampaignAdExtensionOperation operation = new CampaignAdExtensionOperation(); operation.@operator = Operator.ADD; operation.operand = extension; operations.Add(operation); } try { CampaignAdExtensionReturnValue retVal = campaignExtensionService.mutate(operations.ToArray()); // Display the results. if (retVal != null && retVal.value != null && retVal.value.Length > 0) { foreach (CampaignAdExtension campaignExtension in retVal.value) { Console.WriteLine("Created a location extension with id = \"{0}\" and " + "status = \"{1}\"", campaignExtension.adExtension.id, campaignExtension.status); } } else { Console.WriteLine("No location extensions were created."); } } catch (Exception ex) { throw new System.ApplicationException("Failed to add location extension.", ex); } }
/// <summary> /// Creates a campaign ad extension for running further tests. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">The campaign id for which extension is /// created.</param> /// <returns>The campaign ad extension id.</returns> public long CreateLocationExtension(AdWordsUser user, long campaignId) { CampaignAdExtensionService campaignExtensionService = (CampaignAdExtensionService) user.GetService(AdWordsService.v201306. CampaignAdExtensionService); CampaignAdExtensionOperation operation = new CampaignAdExtensionOperation(); operation.@operator = Operator.ADD; CampaignAdExtension extension = new CampaignAdExtension(); extension.campaignId = campaignId; extension.status = CampaignAdExtensionStatus.ACTIVE; Address address = new Address(); address.streetAddress = "1600 Amphitheatre Pkwy, Mountain View"; address.countryCode = "US"; GeoLocation location = GetLocationForAddress(user, address); LocationExtension locationExtension = new LocationExtension(); // Note: Do not populate an address directly. Instead, use // GeoLocationService to obtain the location of an address, // and use the address as per the location it returns. locationExtension.address = location.address; locationExtension.geoPoint = location.geoPoint; locationExtension.encodedLocation = location.encodedLocation; locationExtension.source = LocationExtensionSource.ADWORDS_FRONTEND; extension.adExtension = locationExtension; operation.operand = extension; CampaignAdExtensionReturnValue retVal = campaignExtensionService.mutate(new CampaignAdExtensionOperation[] {operation}); return retVal.value[0].adExtension.id; }
/// <summary> /// Creates the legacy sitelinks. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">The campaign id.</param> /// <returns>The list of legacy sitelinks.</returns> public Sitelink[] CreateLegacySitelinks(AdWordsUser user, long campaignId) { // Get the CampaignAdExtensionService. CampaignAdExtensionService campaignExtensionService = (CampaignAdExtensionService) user.GetService(AdWordsService.v201306. CampaignAdExtensionService); // Create the sitelinks. SitelinksExtension siteLinkExtension = new SitelinksExtension(); Sitelink siteLink1 = new Sitelink(); siteLink1.displayText = "Music"; siteLink1.destinationUrl = "http://www.example.com/music"; Sitelink siteLink2 = new Sitelink(); siteLink2.displayText = "DVDs"; siteLink2.destinationUrl = "http://www.example.com/dvds"; Sitelink siteLink3 = new Sitelink(); siteLink3.displayText = "New albums"; siteLink3.destinationUrl = "http://www.example.com/albums/new"; siteLinkExtension.sitelinks = new Sitelink[] {siteLink1, siteLink2, siteLink3}; CampaignAdExtension campaignAdExtension = new CampaignAdExtension(); campaignAdExtension.adExtension = siteLinkExtension; campaignAdExtension.campaignId = campaignId; // Create the operation. CampaignAdExtensionOperation operation = new CampaignAdExtensionOperation(); operation.@operator = Operator.ADD; operation.operand = campaignAdExtension; // Create the sitelinks. CampaignAdExtensionReturnValue retVal = campaignExtensionService.mutate(new CampaignAdExtensionOperation[] {operation}); return (retVal.value[0].adExtension as SitelinksExtension).sitelinks; }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">Id of the campaign from which sitelinks are /// deleted.</param> public void Run(AdWordsUser user, long campaignId) { // Get the CampaignAdExtensionService. CampaignAdExtensionService campaignExtensionService = (CampaignAdExtensionService)user.GetService(AdWordsService.v201306. CampaignAdExtensionService); long siteLinkExtensionId = -1; // Create the selector. Selector selector = new Selector(); selector.fields = new string[] {"AdExtensionId", "Status"}; // Filter the results for specified campaign id. Predicate campaignPredicate = new Predicate(); campaignPredicate.@operator = PredicateOperator.EQUALS; campaignPredicate.field = "CampaignId"; campaignPredicate.values = new string[] {campaignId.ToString()}; // Filter the results for active campaign ad extensions. Predicate statusPredicate = new Predicate(); statusPredicate.@operator = PredicateOperator.EQUALS; statusPredicate.field = "Status"; statusPredicate.values = new string[] {CampaignAdExtensionStatus.ACTIVE.ToString()}; // Filter for sitelinks ad extension type. Predicate typePredicate = new Predicate(); typePredicate.@operator = PredicateOperator.EQUALS; typePredicate.field = "AdExtensionType"; typePredicate.values = new string[] {"SITELINKS_EXTENSION"}; selector.predicates = new Predicate[] {campaignPredicate, statusPredicate, typePredicate}; // Get the campaign ad extension containing sitelinks. CampaignAdExtensionPage page = campaignExtensionService.get(selector); if (page != null && page.entries != null && page.entries.Length > 0) { siteLinkExtensionId = page.entries[0].adExtension.id; } // There are no site link extensions in this campaign. if (siteLinkExtensionId == -1) { return; } CampaignAdExtension campaignAdExtension = new CampaignAdExtension(); campaignAdExtension.campaignId = campaignId; campaignAdExtension.adExtension = new AdExtension(); campaignAdExtension.adExtension.id = siteLinkExtensionId; CampaignAdExtensionOperation operation = new CampaignAdExtensionOperation(); operation.@operator = Operator.REMOVE; operation.operand = campaignAdExtension; try { CampaignAdExtensionReturnValue retVal = campaignExtensionService.mutate(new CampaignAdExtensionOperation[] {operation}); if (retVal != null && retVal.value != null && retVal.value.Length > 0) { CampaignAdExtension campaignExtension = retVal.value[0]; Console.WriteLine("Deleted a campaign ad extension with id = \"{0}\" and " + "status = \"{1}\"", campaignExtension.adExtension.id, campaignExtension.status); foreach (Sitelink siteLink in (campaignExtension.adExtension as SitelinksExtension).sitelinks) { Console.WriteLine("-- Site link text is \"{0}\" and destination url is {1}", siteLink.displayText, siteLink.destinationUrl); } } else { Console.WriteLine("No site links were deleted."); } } catch (Exception ex) { throw new System.ApplicationException("Failed to delete site links.", ex); } }