/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> public void Run(AdWordsUser user) { // Get the TargetingIdeaService. TargetingIdeaService targetingIdeaService = (TargetingIdeaService) user.GetService(AdWordsService.v201402.TargetingIdeaService); string keywordText = "mars cruise"; // Create selector. TargetingIdeaSelector selector = new TargetingIdeaSelector(); selector.requestType = RequestType.IDEAS; selector.ideaType = IdeaType.KEYWORD; selector.requestedAttributeTypes = new AttributeType[] { AttributeType.KEYWORD_TEXT, AttributeType.SEARCH_VOLUME, AttributeType.CATEGORY_PRODUCTS_AND_SERVICES}; // Language setting (optional). // The ID can be found in the documentation: // https://developers.google.com/adwords/api/docs/appendix/languagecodes // Note: As of v201302, only a single language parameter is allowed. LanguageSearchParameter languageParameter = new LanguageSearchParameter(); Language english = new Language(); english.id = 1000; languageParameter.languages = new Language[] {english}; // Create related to query search parameter. RelatedToQuerySearchParameter relatedToQuerySearchParameter = new RelatedToQuerySearchParameter(); relatedToQuerySearchParameter.queries = new String[] {keywordText}; selector.searchParameters = new SearchParameter[] {relatedToQuerySearchParameter, languageParameter}; // Set selector paging (required for targeting idea service). Paging paging = new Paging(); paging.startIndex = 0; paging.numberResults = 500; selector.paging = paging; int offset = 0; int pageSize = 500; TargetingIdeaPage page = new TargetingIdeaPage(); try { do { selector.paging.startIndex = offset; selector.paging.numberResults = pageSize; // Get related keywords. page = targetingIdeaService.get(selector); // Display related keywords. if (page.entries != null && page.entries.Length > 0) { int i = offset; foreach (TargetingIdea targetingIdea in page.entries) { string keyword = null; string categories = null; long averageMonthlySearches = 0; foreach (Type_AttributeMapEntry entry in targetingIdea.data) { if (entry.key == AttributeType.KEYWORD_TEXT) { keyword = (entry.value as StringAttribute).value; } if (entry.key == AttributeType.CATEGORY_PRODUCTS_AND_SERVICES) { IntegerSetAttribute categorySet = entry.value as IntegerSetAttribute; StringBuilder builder = new StringBuilder(); if (categorySet.value != null) { foreach (int value in categorySet.value) { builder.AppendFormat("{0}, ", value); } categories = builder.ToString().Trim(new char[] {',', ' '}); } } if (entry.key == AttributeType.SEARCH_VOLUME) { averageMonthlySearches = (entry.value as LongAttribute).value; } } Console.WriteLine("Keyword with text '{0}', and average monthly search volume " + "'{1}' was found with categories: {2}", keyword, averageMonthlySearches, categories); i++; } } offset += pageSize; } while (offset < page.totalNumEntries); Console.WriteLine("Number of related keywords found: {0}", page.totalNumEntries); } catch (Exception ex) { throw new System.ApplicationException("Failed to retrieve related keywords.", ex); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> public void Run(AdWordsUser user) { // Get the TargetingIdeaService. TargetingIdeaService targetingIdeaService = (TargetingIdeaService) user.GetService(AdWordsService.v201402.TargetingIdeaService); // Create seed url. string url = "mars.google.com"; // Create the selector. TargetingIdeaSelector selector = new TargetingIdeaSelector(); selector.requestType = RequestType.IDEAS; selector.ideaType = IdeaType.PLACEMENT; selector.requestedAttributeTypes = new AttributeType[] {AttributeType.CRITERION, AttributeType.PLACEMENT_TYPE}; // Create related to url search parameter. RelatedToUrlSearchParameter relatedToUrlSearchParameter = new RelatedToUrlSearchParameter(); relatedToUrlSearchParameter.urls = new string[] {url}; relatedToUrlSearchParameter.includeSubUrls = false; selector.searchParameters = new SearchParameter[] {relatedToUrlSearchParameter}; // Set selector paging. selector.paging = new Paging(); int offset = 0; int pageSize = 500; TargetingIdeaPage page = new TargetingIdeaPage(); try { do { selector.paging.startIndex = offset; selector.paging.numberResults = pageSize; // Get placement ideas. page = targetingIdeaService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = offset; foreach (TargetingIdea idea in page.entries) { foreach (Type_AttributeMapEntry entry in idea.data) { if (entry.key == AttributeType.CRITERION) { CriterionAttribute placementAttribute = entry.value as CriterionAttribute; Placement placement = (Placement) placementAttribute.value; Console.WriteLine("Related placement urls were found at '{0}'.", (placementAttribute.value as Placement).url); } } i++; } } offset += pageSize; } while (offset < page.totalNumEntries); Console.WriteLine("Number of related placements found: {0}", page.totalNumEntries); } catch (Exception ex) { throw new System.ApplicationException("Failed to retrieve related placements.", ex); } }