|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package recaptcha; |
| 18 | + |
| 19 | +// [START recaptcha_enterprise_get_metrics_site_key] |
| 20 | + |
| 21 | +import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient; |
| 22 | +import com.google.recaptchaenterprise.v1.GetMetricsRequest; |
| 23 | +import com.google.recaptchaenterprise.v1.Metrics; |
| 24 | +import com.google.recaptchaenterprise.v1.MetricsName; |
| 25 | +import com.google.recaptchaenterprise.v1.ScoreMetrics; |
| 26 | +import java.io.IOException; |
| 27 | + |
| 28 | +public class GetMetrics { |
| 29 | + |
| 30 | + public static void main(String[] args) throws IOException { |
| 31 | + String projectId = "project-id"; |
| 32 | + String recaptchaSiteKey = "recaptcha-site-key"; |
| 33 | + |
| 34 | + getMetrics(projectId, recaptchaSiteKey); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get metrics specific to a recaptcha site key. E.g: score bucket count for a key or number of |
| 39 | + * times the checkbox key failed/ passed etc., |
| 40 | + * |
| 41 | + * @param projectId: Google Cloud Project Id. |
| 42 | + * @param recaptchaSiteKey: Specify the site key to get metrics. |
| 43 | + */ |
| 44 | + public static void getMetrics(String projectId, String recaptchaSiteKey) throws IOException { |
| 45 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 46 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 47 | + // the `client.close()` method on the client to safely |
| 48 | + // clean up any remaining background resources. |
| 49 | + try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) { |
| 50 | + |
| 51 | + GetMetricsRequest getMetricsRequest = |
| 52 | + GetMetricsRequest.newBuilder() |
| 53 | + .setName(MetricsName.of(projectId, recaptchaSiteKey).toString()) |
| 54 | + .build(); |
| 55 | + |
| 56 | + Metrics response = client.getMetrics(getMetricsRequest); |
| 57 | + |
| 58 | + // Retrieve the metrics you want from the key. |
| 59 | + // If the site key is checkbox type: then use response.getChallengeMetricsList() instead of |
| 60 | + // response.getScoreMetricsList() |
| 61 | + for (ScoreMetrics scoreMetrics : response.getScoreMetricsList()) { |
| 62 | + // Each ScoreMetrics is in the granularity of one day. |
| 63 | + int scoreBucketCount = scoreMetrics.getOverallMetrics().getScoreBucketsCount(); |
| 64 | + System.out.println(scoreBucketCount); |
| 65 | + } |
| 66 | + System.out.printf("Retrieved the bucket count for score based key: %s", recaptchaSiteKey); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +// [END recaptcha_enterprise_get_metrics_site_key] |
0 commit comments