Ejemplo n.º 1
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (TrafficEstimatorService trafficEstimatorService =
                       (TrafficEstimatorService)user.GetService(
                           AdWordsService.v201802.TrafficEstimatorService)) {
                // Create keywords. Refer to the TrafficEstimatorService documentation for the maximum
                // number of keywords that can be passed in a single request.
                //   https://developers.google.com/adwords/api/docs/reference/latest/TrafficEstimatorService
                Keyword keyword1 = new Keyword();
                keyword1.text      = "mars cruise";
                keyword1.matchType = KeywordMatchType.BROAD;

                Keyword keyword2 = new Keyword();
                keyword2.text      = "cheap cruise";
                keyword2.matchType = KeywordMatchType.PHRASE;

                Keyword keyword3 = new Keyword();
                keyword3.text      = "cruise";
                keyword3.matchType = KeywordMatchType.EXACT;

                Keyword[] keywords = new Keyword[] { keyword1, keyword2, keyword3 };

                // Create a keyword estimate request for each keyword.
                List <KeywordEstimateRequest> keywordEstimateRequests = new List <KeywordEstimateRequest>();

                foreach (Keyword keyword in keywords)
                {
                    KeywordEstimateRequest keywordEstimateRequest = new KeywordEstimateRequest();
                    keywordEstimateRequest.keyword = keyword;
                    keywordEstimateRequests.Add(keywordEstimateRequest);
                }

                // Create negative keywords.
                Keyword negativeKeyword1 = new Keyword();
                negativeKeyword1.text      = "moon walk";
                negativeKeyword1.matchType = KeywordMatchType.BROAD;

                KeywordEstimateRequest negativeKeywordEstimateRequest = new KeywordEstimateRequest();
                negativeKeywordEstimateRequest.keyword    = negativeKeyword1;
                negativeKeywordEstimateRequest.isNegative = true;
                keywordEstimateRequests.Add(negativeKeywordEstimateRequest);

                // Create ad group estimate requests.
                AdGroupEstimateRequest adGroupEstimateRequest = new AdGroupEstimateRequest();
                adGroupEstimateRequest.keywordEstimateRequests = keywordEstimateRequests.ToArray();
                adGroupEstimateRequest.maxCpc             = new Money();
                adGroupEstimateRequest.maxCpc.microAmount = 1000000;

                // Create campaign estimate requests.
                CampaignEstimateRequest campaignEstimateRequest = new CampaignEstimateRequest();
                campaignEstimateRequest.adGroupEstimateRequests = new AdGroupEstimateRequest[] {
                    adGroupEstimateRequest
                };

                // Optional: Set additional criteria for filtering estimates.
                // See http://code.google.com/apis/adwords/docs/appendix/countrycodes.html
                // for a detailed list of country codes.
                Location countryCriterion = new Location();
                countryCriterion.id = 2840; //US

                // See http://code.google.com/apis/adwords/docs/appendix/languagecodes.html
                // for a detailed list of language codes.
                Language languageCriterion = new Language();
                languageCriterion.id = 1000; //en

                campaignEstimateRequest.criteria = new Criterion[] { countryCriterion, languageCriterion };

                try {
                    // Create the selector.
                    TrafficEstimatorSelector selector = new TrafficEstimatorSelector()
                    {
                        campaignEstimateRequests = new CampaignEstimateRequest[] { campaignEstimateRequest },

                        // Optional: Request a list of campaign level estimates segmented by platform.
                        platformEstimateRequested = true
                    };

                    // Get traffic estimates.
                    TrafficEstimatorResult result = trafficEstimatorService.get(selector);

                    // Display traffic estimates.
                    if (result != null && result.campaignEstimates != null &&
                        result.campaignEstimates.Length > 0)
                    {
                        CampaignEstimate campaignEstimate = result.campaignEstimates[0];

                        // Display the campaign level estimates segmented by platform.
                        if (campaignEstimate.platformEstimates != null)
                        {
                            foreach (PlatformCampaignEstimate platformEstimate in
                                     campaignEstimate.platformEstimates)
                            {
                                string platformMessage = string.Format("Results for the platform with ID: " +
                                                                       "{0} and name : {1}.", platformEstimate.platform.id,
                                                                       platformEstimate.platform.platformName);

                                DisplayMeanEstimates(platformMessage, platformEstimate.minEstimate,
                                                     platformEstimate.maxEstimate);
                            }
                        }

                        // Display the keyword estimates.
                        if (campaignEstimate.adGroupEstimates != null &&
                            campaignEstimate.adGroupEstimates.Length > 0)
                        {
                            AdGroupEstimate adGroupEstimate = campaignEstimate.adGroupEstimates[0];

                            if (adGroupEstimate.keywordEstimates != null)
                            {
                                for (int i = 0; i < adGroupEstimate.keywordEstimates.Length; i++)
                                {
                                    Keyword         keyword         = keywordEstimateRequests[i].keyword;
                                    KeywordEstimate keywordEstimate = adGroupEstimate.keywordEstimates[i];

                                    if (keywordEstimateRequests[i].isNegative)
                                    {
                                        continue;
                                    }
                                    string kwdMessage = string.Format("Results for the keyword with text = '{0}' " +
                                                                      "and match type = '{1}':", keyword.text, keyword.matchType);
                                    DisplayMeanEstimates(kwdMessage, keywordEstimate.min, keywordEstimate.max);
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("No traffic estimates were returned.");
                    }
                    trafficEstimatorService.Close();
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to retrieve traffic estimates.", e);
                }
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            // Get the TrafficEstimatorService.
            TrafficEstimatorService trafficEstimatorService = (TrafficEstimatorService)user.GetService(
                AdWordsService.v201409.TrafficEstimatorService);

            // Create keywords. Up to 2000 keywords can be passed in a single request.
            Keyword keyword1 = new Keyword();

            keyword1.text      = "mars cruise";
            keyword1.matchType = KeywordMatchType.BROAD;

            Keyword keyword2 = new Keyword();

            keyword2.text      = "cheap cruise";
            keyword2.matchType = KeywordMatchType.PHRASE;

            Keyword keyword3 = new Keyword();

            keyword3.text      = "cruise";
            keyword3.matchType = KeywordMatchType.EXACT;

            Keyword[] keywords = new Keyword[] { keyword1, keyword2, keyword3 };

            // Create a keyword estimate request for each keyword.
            List <KeywordEstimateRequest> keywordEstimateRequests = new List <KeywordEstimateRequest>();

            foreach (Keyword keyword in keywords)
            {
                KeywordEstimateRequest keywordEstimateRequest = new KeywordEstimateRequest();
                keywordEstimateRequest.keyword = keyword;
                keywordEstimateRequests.Add(keywordEstimateRequest);
            }

            // Create negative keywords.
            Keyword negativeKeyword1 = new Keyword();

            negativeKeyword1.text      = "moon walk";
            negativeKeyword1.matchType = KeywordMatchType.BROAD;

            KeywordEstimateRequest negativeKeywordEstimateRequest = new KeywordEstimateRequest();

            negativeKeywordEstimateRequest.keyword    = negativeKeyword1;
            negativeKeywordEstimateRequest.isNegative = true;
            keywordEstimateRequests.Add(negativeKeywordEstimateRequest);

            // Create ad group estimate requests.
            AdGroupEstimateRequest adGroupEstimateRequest = new AdGroupEstimateRequest();

            adGroupEstimateRequest.keywordEstimateRequests = keywordEstimateRequests.ToArray();
            adGroupEstimateRequest.maxCpc             = new Money();
            adGroupEstimateRequest.maxCpc.microAmount = 1000000;

            // Create campaign estimate requests.
            CampaignEstimateRequest campaignEstimateRequest = new CampaignEstimateRequest();

            campaignEstimateRequest.adGroupEstimateRequests = new AdGroupEstimateRequest[] {
                adGroupEstimateRequest
            };

            // See http://code.google.com/apis/adwords/docs/appendix/countrycodes.html
            // for a detailed list of country codes.
            Location countryCriterion = new Location();

            countryCriterion.id = 2840; //US

            // See http://code.google.com/apis/adwords/docs/appendix/languagecodes.html
            // for a detailed list of language codes.
            Language languageCriterion = new Language();

            languageCriterion.id = 1000; //en

            campaignEstimateRequest.criteria = new Criterion[] { countryCriterion, languageCriterion };

            // Create the selector.
            TrafficEstimatorSelector selector = new TrafficEstimatorSelector();

            selector.campaignEstimateRequests = new CampaignEstimateRequest[] { campaignEstimateRequest };

            try {
                // Get traffic estimates.
                TrafficEstimatorResult result = trafficEstimatorService.get(selector);

                // Display traffic estimates.
                if (result != null && result.campaignEstimates != null &&
                    result.campaignEstimates.Length > 0)
                {
                    CampaignEstimate campaignEstimate = result.campaignEstimates[0];
                    if (campaignEstimate.adGroupEstimates != null &&
                        campaignEstimate.adGroupEstimates.Length > 0)
                    {
                        AdGroupEstimate adGroupEstimate = campaignEstimate.adGroupEstimates[0];

                        if (adGroupEstimate.keywordEstimates != null)
                        {
                            for (int i = 0; i < adGroupEstimate.keywordEstimates.Length; i++)
                            {
                                Keyword         keyword         = keywordEstimateRequests[i].keyword;
                                KeywordEstimate keywordEstimate = adGroupEstimate.keywordEstimates[i];

                                if (keywordEstimateRequests[i].isNegative)
                                {
                                    continue;
                                }

                                // Find the mean of the min and max values.
                                long meanAverageCpc = (keywordEstimate.min.averageCpc.microAmount
                                                       + keywordEstimate.max.averageCpc.microAmount) / 2;
                                double meanAveragePosition = (keywordEstimate.min.averagePosition
                                                              + keywordEstimate.max.averagePosition) / 2;
                                float meanClicks = (keywordEstimate.min.clicksPerDay
                                                    + keywordEstimate.max.clicksPerDay) / 2;
                                long meanTotalCost = (keywordEstimate.min.totalCost.microAmount
                                                      + keywordEstimate.max.totalCost.microAmount) / 2;

                                Console.WriteLine("Results for the keyword with text = '{0}' and match type = " +
                                                  "'{1}':", keyword.text, keyword.matchType);
                                Console.WriteLine("  Estimated average CPC: {0}", meanAverageCpc);
                                Console.WriteLine("  Estimated ad position: {0:0.00}", meanAveragePosition);
                                Console.WriteLine("  Estimated daily clicks: {0}", meanClicks);
                                Console.WriteLine("  Estimated daily cost: {0}", meanTotalCost);
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("No traffic estimates were returned.\n");
                }
            } catch (Exception ex) {
                throw new System.ApplicationException("Failed to retrieve traffic estimates.", ex);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the traffic estimates for a list of keywords.
        /// </summary>
        /// <param name="user">The user for which keyword ideas are generated.</param>
        /// <param name="keywords">The keywords for which traffic estimates are
        ///  done.</param>
        /// <param name="matchType">Type of the keyword match to apply when making
        /// traffic estimates.</param>
        /// <param name="maxCpc">The maximum CPC to consider for the traffic.</param>
        /// <param name="campaignId">The campaign ID whose settings should be used
        /// for traffic estimation.</param>
        /// <returns>A list of traffic estimates for the keywords.</returns>
        private List <TrafficEstimate> GetTrafficEstimates(AdWordsUser user,
                                                           List <KeywordIdea> keywords, KeywordMatchType matchType, long maxCpc, long campaignId)
        {
            // Get the TrafficEstimatorService.
            TrafficEstimatorService trafficEstimatorService = (TrafficEstimatorService)user.GetService(
                AdWordsService.v201607.TrafficEstimatorService);

            List <Criterion>       trafficCriteria = GetTrafficEstimateCriteria();
            List <TrafficEstimate> retval          = new List <TrafficEstimate>();

            // Eliminate duplicate keywords in the keyword suggestion list so that
            // TrafficEstimatorService doesn't complain about them.
            Dictionary <string, KeywordIdea> uniqueEntries = new Dictionary <string, KeywordIdea>();

            for (int i = 0; i < keywords.Count; i++)
            {
                if (!uniqueEntries.ContainsKey(keywords[i].KeywordText))
                {
                    uniqueEntries[keywords[i].KeywordText] = keywords[i];
                }
            }

            keywords = new List <KeywordIdea>(uniqueEntries.Values);

            for (int i = 0; i < keywords.Count; i += Settings.TES_KEYWORDS_LIST_SIZE)
            {
                List <KeywordEstimateRequest> keywordEstimateRequests = new List <KeywordEstimateRequest>();

                for (int j = i; j < i + Settings.TES_KEYWORDS_LIST_SIZE && j < keywords.Count; j++)
                {
                    KeywordEstimateRequest keywordEstimateRequest = new KeywordEstimateRequest()
                    {
                        keyword = new Keyword()
                        {
                            text      = keywords[j].KeywordText,
                            matchType = matchType
                        }
                    };
                    keywordEstimateRequests.Add(keywordEstimateRequest);
                }

                // Create campaign estimate requests.
                CampaignEstimateRequest campaignEstimateRequest = new CampaignEstimateRequest()
                {
                    adGroupEstimateRequests = new AdGroupEstimateRequest[] {
                        new AdGroupEstimateRequest()
                        {
                            keywordEstimateRequests = keywordEstimateRequests.ToArray(),
                            maxCpc = new Money()
                            {
                                microAmount = maxCpc
                            }
                        }
                    }
                };

                campaignEstimateRequest.criteria   = trafficCriteria.ToArray();
                campaignEstimateRequest.campaignId = campaignId;

                // Create the selector.
                TrafficEstimatorSelector selector = new TrafficEstimatorSelector();
                selector.campaignEstimateRequests = new CampaignEstimateRequest[] {
                    campaignEstimateRequest
                };

                try {
                    // Get traffic estimates.
                    TrafficEstimatorResult result = trafficEstimatorService.get(selector);

                    // Display traffic estimates.
                    if (result != null && result.campaignEstimates != null &&
                        result.campaignEstimates.Length > 0)
                    {
                        CampaignEstimate campaignEstimate = result.campaignEstimates[0];
                        if (campaignEstimate.adGroupEstimates != null &&
                            campaignEstimate.adGroupEstimates.Length > 0)
                        {
                            AdGroupEstimate adGroupEstimate = campaignEstimate.adGroupEstimates[0];

                            if (adGroupEstimate.keywordEstimates != null)
                            {
                                for (int k = 0; k < adGroupEstimate.keywordEstimates.Length; k++)
                                {
                                    Keyword         keyword         = keywordEstimateRequests[k].keyword;
                                    KeywordEstimate keywordEstimate = adGroupEstimate.keywordEstimates[k];

                                    if (keywordEstimateRequests[k].isNegative)
                                    {
                                        continue;
                                    }

                                    // Find the mean of the min and max values.
                                    long   meanAverageCpc      = 0;
                                    double meanAveragePosition = 0;
                                    long   meanClicks          = 0;
                                    long   meanImpressions     = 0;
                                    long   meanTotalCost       = 0;

                                    if (keywordEstimate.min != null && keywordEstimate.max != null)
                                    {
                                        if (keywordEstimate.min.averageCpc != null &&
                                            keywordEstimate.max.averageCpc != null)
                                        {
                                            meanAverageCpc = (long)Math.Round(
                                                (double)(keywordEstimate.min.averageCpc.microAmount
                                                         + keywordEstimate.max.averageCpc.microAmount) / 2, Settings.ACCURACY,
                                                MidpointRounding.AwayFromZero);
                                        }
                                        meanAveragePosition = Math.Round((keywordEstimate.min.averagePosition
                                                                          + keywordEstimate.max.averagePosition) / 2, Settings.ACCURACY,
                                                                         MidpointRounding.AwayFromZero);

                                        meanClicks = (long)Math.Round((keywordEstimate.min.clicksPerDay +
                                                                       keywordEstimate.max.clicksPerDay) / 2, MidpointRounding.AwayFromZero);

                                        meanImpressions = (long)Math.Round((keywordEstimate.min.impressionsPerDay
                                                                            + keywordEstimate.max.impressionsPerDay) / 2,
                                                                           MidpointRounding.AwayFromZero);

                                        if (keywordEstimate.min.totalCost != null &&
                                            keywordEstimate.max.totalCost != null)
                                        {
                                            meanTotalCost = (keywordEstimate.min.totalCost.microAmount
                                                             + keywordEstimate.max.totalCost.microAmount) / 2;
                                        }
                                    }

                                    TrafficEstimate trafficEstimate = new TrafficEstimate()
                                    {
                                        Keyword         = keywords[i + k],
                                        MatchType       = keyword.matchType,
                                        Clicks          = meanClicks,
                                        Impressions     = meanImpressions,
                                        Cost            = meanTotalCost,
                                        AverageCpc      = meanAverageCpc,
                                        AveragePosition = meanAveragePosition
                                    };
                                    retval.Add(trafficEstimate);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to retrieve traffic estimates.", e);
                }
            }

            return(retval);
        }
    /// <summary>
    /// Runs the code example.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    public void Run(AdWordsUser user) {
      // Get the TrafficEstimatorService.
      TrafficEstimatorService trafficEstimatorService = (TrafficEstimatorService) user.GetService(
          AdWordsService.v201509.TrafficEstimatorService);

      // Create keywords. Up to 2000 keywords can be passed in a single request.
      Keyword keyword1 = new Keyword();
      keyword1.text = "mars cruise";
      keyword1.matchType = KeywordMatchType.BROAD;

      Keyword keyword2 = new Keyword();
      keyword2.text = "cheap cruise";
      keyword2.matchType = KeywordMatchType.PHRASE;

      Keyword keyword3 = new Keyword();
      keyword3.text = "cruise";
      keyword3.matchType = KeywordMatchType.EXACT;

      Keyword[] keywords = new Keyword[] { keyword1, keyword2, keyword3 };

      // Create a keyword estimate request for each keyword.
      List<KeywordEstimateRequest> keywordEstimateRequests = new List<KeywordEstimateRequest>();

      foreach (Keyword keyword in keywords) {
        KeywordEstimateRequest keywordEstimateRequest = new KeywordEstimateRequest();
        keywordEstimateRequest.keyword = keyword;
        keywordEstimateRequests.Add(keywordEstimateRequest);
      }

      // Create negative keywords.
      Keyword negativeKeyword1 = new Keyword();
      negativeKeyword1.text = "moon walk";
      negativeKeyword1.matchType = KeywordMatchType.BROAD;

      KeywordEstimateRequest negativeKeywordEstimateRequest = new KeywordEstimateRequest();
      negativeKeywordEstimateRequest.keyword = negativeKeyword1;
      negativeKeywordEstimateRequest.isNegative = true;
      keywordEstimateRequests.Add(negativeKeywordEstimateRequest);

      // Create ad group estimate requests.
      AdGroupEstimateRequest adGroupEstimateRequest = new AdGroupEstimateRequest();
      adGroupEstimateRequest.keywordEstimateRequests = keywordEstimateRequests.ToArray();
      adGroupEstimateRequest.maxCpc = new Money();
      adGroupEstimateRequest.maxCpc.microAmount = 1000000;

      // Create campaign estimate requests.
      CampaignEstimateRequest campaignEstimateRequest = new CampaignEstimateRequest();
      campaignEstimateRequest.adGroupEstimateRequests = new AdGroupEstimateRequest[] {
          adGroupEstimateRequest};

      // See http://code.google.com/apis/adwords/docs/appendix/countrycodes.html
      // for a detailed list of country codes.
      Location countryCriterion = new Location();
      countryCriterion.id = 2840; //US

      // See http://code.google.com/apis/adwords/docs/appendix/languagecodes.html
      // for a detailed list of language codes.
      Language languageCriterion = new Language();
      languageCriterion.id = 1000; //en

      campaignEstimateRequest.criteria = new Criterion[] { countryCriterion, languageCriterion };

      // Create the selector.
      TrafficEstimatorSelector selector = new TrafficEstimatorSelector();
      selector.campaignEstimateRequests = new CampaignEstimateRequest[] { campaignEstimateRequest };

      try {
        // Get traffic estimates.
        TrafficEstimatorResult result = trafficEstimatorService.get(selector);

        // Display traffic estimates.
        if (result != null && result.campaignEstimates != null &&
            result.campaignEstimates.Length > 0) {
          CampaignEstimate campaignEstimate = result.campaignEstimates[0];
          if (campaignEstimate.adGroupEstimates != null &&
              campaignEstimate.adGroupEstimates.Length > 0) {
            AdGroupEstimate adGroupEstimate = campaignEstimate.adGroupEstimates[0];

            if (adGroupEstimate.keywordEstimates != null) {
              for (int i = 0; i < adGroupEstimate.keywordEstimates.Length; i++) {
                Keyword keyword = keywordEstimateRequests[i].keyword;
                KeywordEstimate keywordEstimate = adGroupEstimate.keywordEstimates[i];

                if (keywordEstimateRequests[i].isNegative) {
                  continue;
                }

                // Find the mean of the min and max values.
                long meanAverageCpc = 0;
                double meanAveragePosition = 0;
                float meanClicks = 0;
                long meanTotalCost = 0;

                if (keywordEstimate.min != null && keywordEstimate.max != null) {
                  if (keywordEstimate.min.averageCpc != null &&
                      keywordEstimate.max.averageCpc != null) {
                    meanAverageCpc = (keywordEstimate.min.averageCpc.microAmount +
                        keywordEstimate.max.averageCpc.microAmount) / 2;
                  }

                  meanAveragePosition = (keywordEstimate.min.averagePosition +
                      keywordEstimate.max.averagePosition) / 2;
                  meanClicks = (keywordEstimate.min.clicksPerDay +
                      keywordEstimate.max.clicksPerDay) / 2;
                  if (keywordEstimate.min.totalCost != null &&
                      keywordEstimate.max.totalCost != null) {
                    meanTotalCost = (keywordEstimate.min.totalCost.microAmount +
                        keywordEstimate.max.totalCost.microAmount) / 2;
                  }
                }

                Console.WriteLine("Results for the keyword with text = '{0}' and match type = " +
                     "'{1}':", keyword.text, keyword.matchType);
                Console.WriteLine("  Estimated average CPC: {0}", meanAverageCpc);
                Console.WriteLine("  Estimated ad position: {0:0.00}", meanAveragePosition);
                Console.WriteLine("  Estimated daily clicks: {0}", meanClicks);
                Console.WriteLine("  Estimated daily cost: {0}", meanTotalCost);
              }
            }
          }
        } else {
          Console.WriteLine("No traffic estimates were returned.\n");
        }
      } catch (Exception e) {
        throw new System.ApplicationException("Failed to retrieve traffic estimates.", e);
      }
    }