reCAPTCHA サイトキーの指標を取得する

reCAPTCHA サイトキーに固有の指標を取得します。

コードサンプル

Java

reCAPTCHA Enterprise への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。


import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
import com.google.recaptchaenterprise.v1.GetMetricsRequest;
import com.google.recaptchaenterprise.v1.Metrics;
import com.google.recaptchaenterprise.v1.MetricsName;
import com.google.recaptchaenterprise.v1.ScoreMetrics;
import java.io.IOException;

public class GetMetrics {

  public static void main(String[] args) throws IOException {
    String projectId = "project-id";
    String recaptchaSiteKey = "recaptcha-site-key";

    getMetrics(projectId, recaptchaSiteKey);
  }

  /**
   * Get metrics specific to a recaptcha site key. E.g: score bucket count for a key or number of
   * times the checkbox key failed/ passed etc.,
   *
   * @param projectId: Google Cloud Project Id.
   * @param recaptchaSiteKey: Specify the site key to get metrics.
   */
  public static void getMetrics(String projectId, String recaptchaSiteKey) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `client.close()` method on the client to safely
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {

      GetMetricsRequest getMetricsRequest =
          GetMetricsRequest.newBuilder()
              .setName(MetricsName.of(projectId, recaptchaSiteKey).toString())
              .build();

      Metrics response = client.getMetrics(getMetricsRequest);

      // Retrieve the metrics you want from the key.
      // If the site key is checkbox type: then use response.getChallengeMetricsList() instead of
      // response.getScoreMetricsList()
      for (ScoreMetrics scoreMetrics : response.getScoreMetricsList()) {
        // Each ScoreMetrics is in the granularity of one day.
        int scoreBucketCount = scoreMetrics.getOverallMetrics().getScoreBucketsCount();
        System.out.println(scoreBucketCount);
      }
      System.out.printf("Retrieved the bucket count for score based key: %s", recaptchaSiteKey);
    }
  }
}

Python

reCAPTCHA Enterprise への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

from google.cloud import recaptchaenterprise_v1

def get_metrics(project_id: str, recaptcha_site_key: str) -> None:
    """Get metrics specific to a recaptcha site key.
        E.g: score bucket count for a key or number of
        times the checkbox key failed/ passed etc.,
    Args:
    project_id: Google Cloud Project ID.
    recaptcha_site_key: Specify the site key to get metrics.
    """

    client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

    metrics_name = f"projects/{project_id}/keys/{recaptcha_site_key}/metrics"
    request = recaptchaenterprise_v1.GetMetricsRequest()
    request.name = metrics_name

    response = client.get_metrics(request)

    # Retrieve the metrics you want from the key.
    # If the site key is checkbox type: then use response.challenge_metrics
    # instead of response.score_metrics
    for day_metric in response.score_metrics:
        # Each 'day_metric' is in the granularity of one day.
        score_bucket_count = day_metric.overall_metrics.score_buckets
        print(score_bucket_count)

    print(f"Retrieved the bucket count for score based key: {recaptcha_site_key}")

Ruby

reCAPTCHA Enterprise への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

require "google/cloud/recaptcha_enterprise"

#  Get metrics specific to a recaptcha site key.
#  E.g: score bucket count for a key or number of
#  times the checkbox key failed/ passed etc.,
#
# @param project_id [String] GCloud Project ID.
# @param site_key [String] Site key to be updated.
# @return [void]
def get_metrics_site_key project_id:, site_key:
  # Create the reCAPTCHA client.
  client = ::Google::Cloud::RecaptchaEnterprise.recaptcha_enterprise_service

  response = client.get_metrics name: "projects/#{project_id}/keys/#{site_key}/metrics"

  # Retrieve the metrics you want from the key.
  # If the site key is checkbox type: then use response.challenge_metrics
  # instead of response.score_metrics
  puts "Retrieved the bucket count for score based key: #{site_key}"
  response.score_metrics.each do |day_metric|
    # Each 'day_metric' is in the granularity of one day.
    score_bucket_count = day_metric.overall_metrics.score_buckets
    puts score_bucket_count.inspect
  end
end

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。