Exemplo n.º 1
0
        /// <summary>
        /// Gets the location search parameters.
        /// </summary>
        /// <returns>The location search parameters.</returns>
        private static LocationSearchParameter GetLocationSearchParams()
        {
            LocationSearchParameter locationParams = new LocationSearchParameter();
              List<Location> locations = new List<Location>();

              foreach (int locationCode in Settings.LOCATIONS) {
            locations.Add(new Location() {
              id = locationCode
            });
              }
              locationParams.locations = locations.ToArray();
              return locationParams;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets keyword ideas for the list of Seed keywords.
        /// </summary>
        /// <param name="user">The user for which keyword ideas are generated.
        /// </param>
        /// <param name="seedKeywords">The seed keywords for generating ideas.
        /// </param>
        /// <param name="negativeTerms">The list of keywords to exclude.</param>
        /// <returns>A list of keyword ideas.</returns>
        private List <KeywordIdea> GetKeywordIdeas(AdWordsUser user, List <string> seedKeywords,
                                                   SeedKeyword[] negativeTerms)
        {
            // Get the TargetingIdeaService.
            TargetingIdeaService targetingIdeaService =
                (TargetingIdeaService)user.GetService(AdWordsService.v201607.TargetingIdeaService);

            IdeaTextFilterSearchParameter excludedTerms = null;

            if (negativeTerms.Length > 0)
            {
                excludedTerms = GetExcludedKeywordSearchParams(negativeTerms);
            }

            LanguageSearchParameter languageParams = GetLanguageSearchParams();
            LocationSearchParameter locationParams = GetLocationSearchParams();

            List <KeywordIdea> retval = new List <KeywordIdea>();

            for (int i = 0; i < seedKeywords.Count; i += Settings.TIS_KEYWORDS_LIST_SIZE)
            {
                List <string> keywordsToSearch = new List <string>();
                for (int j = i; j < i + Settings.TIS_KEYWORDS_LIST_SIZE && j < seedKeywords.Count; j++)
                {
                    keywordsToSearch.Add(seedKeywords[j]);
                }

                // 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.AVERAGE_CPC,
                    AttributeType.COMPETITION
                };

                List <SearchParameter> paramList = new List <SearchParameter>();
                paramList.Add(new RelatedToQuerySearchParameter()
                {
                    queries = keywordsToSearch.ToArray(),
                });

                if (excludedTerms != null)
                {
                    paramList.Add(excludedTerms);
                }
                paramList.Add(locationParams);
                paramList.Add(languageParams);

                selector.searchParameters = paramList.ToArray();

                // Set selector paging (required for targeting idea service).
                Paging            paging = Paging.Default;
                TargetingIdeaPage page   = new TargetingIdeaPage();

                try {
                    do
                    {
                        // Get related keywords.
                        page = targetingIdeaService.get(selector);

                        // Display related keywords.
                        if (page.entries != null && page.entries.Length > 0)
                        {
                            foreach (TargetingIdea targetingIdea in page.entries)
                            {
                                string keyword = null;
                                long   averageMonthlySearches = 0;
                                long   averageCpc             = 0;
                                double competition            = 0f;

                                foreach (Type_AttributeMapEntry entry in targetingIdea.data)
                                {
                                    if (entry.key == AttributeType.KEYWORD_TEXT)
                                    {
                                        StringAttribute temp = (entry.value as StringAttribute);
                                        if (temp != null)
                                        {
                                            keyword = temp.value;
                                        }
                                    }

                                    if (entry.key == AttributeType.SEARCH_VOLUME)
                                    {
                                        LongAttribute temp = (entry.value as LongAttribute);
                                        if (temp != null)
                                        {
                                            averageMonthlySearches = temp.value;
                                        }
                                    }

                                    if (entry.key == AttributeType.AVERAGE_CPC)
                                    {
                                        MoneyAttribute temp = (entry.value as MoneyAttribute);
                                        if (temp != null && temp.value != null)
                                        {
                                            averageCpc = temp.value.microAmount;
                                        }
                                    }

                                    if (entry.key == AttributeType.COMPETITION)
                                    {
                                        DoubleAttribute temp = (entry.value as DoubleAttribute);
                                        if (temp != null)
                                        {
                                            competition = temp.value;
                                        }
                                    }
                                }

                                KeywordIdea keywordIdea = new KeywordIdea()
                                {
                                    KeywordText     = keyword,
                                    AverageSearches = averageMonthlySearches,
                                    AverageCpc      = averageCpc,
                                    Competition     = Math.Round(competition, Settings.ACCURACY,
                                                                 MidpointRounding.AwayFromZero)
                                };
                                retval.Add(keywordIdea);
                            }
                        }
                        selector.paging.IncreaseOffset();
                    } while (selector.paging.startIndex < page.totalNumEntries);
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to retrieve related keywords.", e);
                }
            }
            return(retval);
        }