|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.ads.googleads.examples.planning; |
| 16 | + |
| 17 | +import com.beust.jcommander.Parameter; |
| 18 | +import com.google.ads.googleads.examples.utils.ArgumentNames; |
| 19 | +import com.google.ads.googleads.examples.utils.CodeSampleParams; |
| 20 | +import com.google.ads.googleads.lib.GoogleAdsClient; |
| 21 | +import com.google.ads.googleads.v11.common.MonthlySearchVolume; |
| 22 | +import com.google.ads.googleads.v11.errors.GoogleAdsError; |
| 23 | +import com.google.ads.googleads.v11.errors.GoogleAdsException; |
| 24 | +import com.google.ads.googleads.v11.services.GenerateHistoricalMetricsResponse; |
| 25 | +import com.google.ads.googleads.v11.services.KeywordPlanKeywordHistoricalMetrics; |
| 26 | +import com.google.ads.googleads.v11.services.KeywordPlanServiceClient; |
| 27 | +import com.google.ads.googleads.v11.utils.ResourceNames; |
| 28 | +import com.google.protobuf.ProtocolStringList; |
| 29 | +import java.io.FileNotFoundException; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +/** |
| 34 | + * Generates historical metrics for a keyword plan. To create a keyword plan, run the {@link |
| 35 | + * AddKeywordPlan} example. |
| 36 | + */ |
| 37 | +public class GenerateHistoricalMetrics { |
| 38 | + |
| 39 | + private static class GenerateHistoricalMetricsParams extends CodeSampleParams { |
| 40 | + |
| 41 | + @Parameter( |
| 42 | + names = ArgumentNames.CUSTOMER_ID, |
| 43 | + description = "The customer in which to create a new keyword plan.") |
| 44 | + public Long customerId; |
| 45 | + |
| 46 | + @Parameter( |
| 47 | + names = ArgumentNames.KEYWORD_PLAN_ID, |
| 48 | + description = "The keyword plan ID for which to generate metrics.") |
| 49 | + public Long keywordPlanId; |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + GenerateHistoricalMetricsParams params = new GenerateHistoricalMetricsParams(); |
| 54 | + if (!params.parseArguments(args)) { |
| 55 | + |
| 56 | + // Optional, specify the customer ID for which the call is made. |
| 57 | + params.customerId = Long.valueOf("INSERT_CUSTOMER_ID"); |
| 58 | + |
| 59 | + params.keywordPlanId = Long.valueOf("INSERT_KEYWORD_PLAN_ID"); |
| 60 | + } |
| 61 | + GoogleAdsClient googleAdsClient = null; |
| 62 | + try { |
| 63 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 64 | + } catch (FileNotFoundException fnfe) { |
| 65 | + System.err.printf( |
| 66 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 67 | + System.exit(1); |
| 68 | + } catch (IOException ioe) { |
| 69 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 70 | + System.exit(1); |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + new GenerateHistoricalMetrics() |
| 75 | + .runExample(googleAdsClient, params.customerId, params.keywordPlanId); |
| 76 | + } catch (GoogleAdsException gae) { |
| 77 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 78 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 79 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 80 | + // GoogleAdsException. |
| 81 | + System.err.printf( |
| 82 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 83 | + gae.getRequestId()); |
| 84 | + int i = 0; |
| 85 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 86 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 87 | + } |
| 88 | + System.exit(1); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Runs the code example. |
| 94 | + * |
| 95 | + * @param googleAdsClient the Google Ads API client. |
| 96 | + * @param customerId the client customer ID. |
| 97 | + * @param planId the plan ID. |
| 98 | + */ |
| 99 | + // [START generate_historical_metrics] |
| 100 | + private void runExample(GoogleAdsClient googleAdsClient, Long customerId, Long planId) { |
| 101 | + String resourceName = ResourceNames.keywordPlan(customerId, planId); |
| 102 | + |
| 103 | + try (KeywordPlanServiceClient keywordPlanServiceClient = |
| 104 | + googleAdsClient.getLatestVersion().createKeywordPlanServiceClient()) { |
| 105 | + GenerateHistoricalMetricsResponse response = |
| 106 | + keywordPlanServiceClient.generateHistoricalMetrics(resourceName); |
| 107 | + |
| 108 | + for (KeywordPlanKeywordHistoricalMetrics metric : response.getMetricsList()) { |
| 109 | + // These metrics include those for both the search query and any close variants included in |
| 110 | + // the response. |
| 111 | + |
| 112 | + List<String> variants = metric.getCloseVariantsList(); |
| 113 | + String variantsList = |
| 114 | + (variants.isEmpty()) |
| 115 | + ? "" |
| 116 | + : String.format(" (and the following variants: %s)", String.join(", ", variants)); |
| 117 | + |
| 118 | + System.out.printf( |
| 119 | + "The search query '%s'%s generated the following historical metrics:%n", |
| 120 | + metric.getSearchQuery(), variantsList); |
| 121 | + |
| 122 | + // Approximate number of monthly searches on this query averaged for the past 12 months. |
| 123 | + System.out.printf( |
| 124 | + "\tApproximate monthly searches: %d.%n", |
| 125 | + metric.getKeywordMetrics().getAvgMonthlySearches()); |
| 126 | + |
| 127 | + // The competition level for this search query. |
| 128 | + System.out.printf( |
| 129 | + "\tCompetition level: %s.%n", metric.getKeywordMetrics().getCompetition().name()); |
| 130 | + |
| 131 | + // The competition index for the query in the range [0, 100]. This shows how competitive ad |
| 132 | + // placement is for a keyword. The level of competition from 0-100 is determined by the |
| 133 | + // number of ad slots filled divided by the total number of ad slots available. If not |
| 134 | + // enough data is available, None will be returned. |
| 135 | + if (metric.getKeywordMetrics().hasCompetitionIndex()) { |
| 136 | + System.out.printf( |
| 137 | + "\tCompetition index: %d.%n", metric.getKeywordMetrics().getCompetitionIndex()); |
| 138 | + } |
| 139 | + |
| 140 | + // Top of page bid low range (20th percentile) in micros for the keyword. |
| 141 | + if (metric.getKeywordMetrics().hasLowTopOfPageBidMicros()) { |
| 142 | + System.out.printf( |
| 143 | + "\tTop of page bid low range %d.%n", |
| 144 | + metric.getKeywordMetrics().getLowTopOfPageBidMicros()); |
| 145 | + } |
| 146 | + |
| 147 | + // Top of page bid high range (80th percentile) in micros for the keyword. |
| 148 | + if (metric.getKeywordMetrics().hasHighTopOfPageBidMicros()) { |
| 149 | + System.out.printf( |
| 150 | + "\tTop of page bid high range %d.%n", |
| 151 | + metric.getKeywordMetrics().getHighTopOfPageBidMicros()); |
| 152 | + } |
| 153 | + |
| 154 | + // Approximate number of searches on this query for the past twelve months. |
| 155 | + for (MonthlySearchVolume month : metric.getKeywordMetrics().getMonthlySearchVolumesList()) { |
| 156 | + System.out.printf( |
| 157 | + "\tApproximately %d searches in %s, %d.%n", |
| 158 | + month.getMonthlySearches(), month.getMonth().name(), month.getYear()); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + // [END generate_historical_metrics] |
| 164 | +} |
0 commit comments