Skip to main content

google_analytics_api_ga4/
lib.rs

1#![doc = include_str!("../README.md")]
2
3mod batch_run_pivot_reports;
4mod batch_run_reports;
5mod check_compatibility;
6mod error;
7mod get_metadata;
8mod http;
9mod run_pivot_reports;
10mod run_realtime_report;
11mod run_report;
12pub mod types;
13
14pub use batch_run_pivot_reports::BatchRunPivotReportsResponse;
15pub use batch_run_reports::BatchRunReportsResponse;
16pub use check_compatibility::*;
17pub use error::*;
18pub use get_metadata::*;
19pub use run_pivot_reports::*;
20pub use run_realtime_report::*;
21pub use run_report::*;
22
23use crate::batch_run_pivot_reports::BatchRunPivotReportsRequestBody;
24use crate::batch_run_reports::BatchRunReportsRequestBody;
25use crate::http::HttpClient;
26use crate::types::{RunPivotReportResponse, RunReportResponse};
27
28const ENDPOINT: &str = "https://analyticsdata.googleapis.com/v1beta";
29
30/// Entry point for the Google Analytics 4 (GA4) Data API v1beta.
31///
32/// Each method takes an OAuth2 access token (`&str`) and a numeric GA4
33/// property ID (e.g. `"123456789"`).
34pub struct AnalyticsDataApi {}
35
36impl AnalyticsDataApi {
37    /// Runs multiple pivot reports in a single API request.
38    ///
39    /// <https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/batchRunPivotReports>
40    pub async fn batch_run_pivot_reports(
41        token: &str,
42        property: &str,
43        requests: Vec<RunPivotReportRequest>,
44    ) -> Result<BatchRunPivotReportsResponse, GoogleApiError> {
45        HttpClient::post(
46            token,
47            format!("{}/properties/{}:batchRunPivotReports", ENDPOINT, property).as_str(),
48            BatchRunPivotReportsRequestBody { requests },
49        )
50        .await
51    }
52    /// Runs up to 5 reports in a single API request.
53    ///
54    /// <https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/batchRunReports>
55    pub async fn batch_run_reports(
56        token: &str,
57        property: &str,
58        requests: Vec<RunReportRequest>,
59    ) -> Result<BatchRunReportsResponse, GoogleApiError> {
60        HttpClient::post(
61            token,
62            format!("{}/properties/{}:batchRunReports", ENDPOINT, property).as_str(),
63            BatchRunReportsRequestBody { requests },
64        )
65        .await
66    }
67    /// Checks which dimensions and metrics can be combined in a report.
68    ///
69    /// <https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/checkCompatibility>
70    pub async fn check_compatibility(
71        token: &str,
72        property: &str,
73        request: CheckCompatibilityRequest,
74    ) -> Result<CheckCompatibilityResponse, GoogleApiError> {
75        HttpClient::post(
76            token,
77            format!("{}/properties/{}:checkCompatibility", ENDPOINT, property).as_str(),
78            request,
79        )
80        .await
81    }
82    /// Lists all dimensions and metrics available on a property, including custom definitions.
83    ///
84    /// <https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/getMetadata>
85    pub async fn get_metadata(
86        token: &str,
87        property: &str,
88    ) -> Result<GetMetadataResponse, GoogleApiError> {
89        HttpClient::get(
90            token,
91            format!("{}/properties/{}/metadata", ENDPOINT, property).as_str(),
92        )
93        .await
94    }
95    /// Runs a pivot-table style report.
96    ///
97    /// <https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runPivotReport>
98    pub async fn run_pivot_report(
99        token: &str,
100        property: &str,
101        request: RunPivotReportRequest,
102    ) -> Result<RunPivotReportResponse, GoogleApiError> {
103        HttpClient::post(
104            token,
105            format!("{}/properties/{}:runPivotReport", ENDPOINT, property).as_str(),
106            request,
107        )
108        .await
109    }
110    /// Returns realtime event data for the last 30 minutes (e.g. active users).
111    ///
112    /// <https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport>
113    pub async fn run_realtime_report(
114        token: &str,
115        property: &str,
116        request: RunRealtimeReportRequest,
117    ) -> Result<RunReportResponse, GoogleApiError> {
118        HttpClient::post(
119            token,
120            format!("{}/properties/{}:runRealtimeReport", ENDPOINT, property).as_str(),
121            request,
122        )
123        .await
124    }
125    /// Runs a report of dimensions and metrics over a date range.
126    ///
127    /// <https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport>
128    pub async fn run_report(
129        token: &str,
130        property: &str,
131        request: RunReportRequest,
132    ) -> Result<RunReportResponse, GoogleApiError> {
133        HttpClient::post(
134            token,
135            format!("{}/properties/{}:runReport", ENDPOINT, property).as_str(),
136            request,
137        )
138        .await
139    }
140}