SDKs overview

The Google Analytics API provides developer tools, including Software Development Kits (SDKs) and client libraries, to facilitate programmatic access to its reporting data. These resources allow developers to integrate Google Analytics functionality into their applications, automate data retrieval, and build custom analytics solutions. The primary focus of the APIs and their associated SDKs is to enable access to the metrics and dimensions collected by Google Analytics, particularly for Google Analytics 4 (GA4) properties, which utilize an event-based data model Google Analytics Data API reference.

Developers commonly use these SDKs and libraries to:

  • Retrieve custom reports based on specific metrics and dimensions.
  • Automate the export of analytics data for dashboards or other data processing systems.
  • Integrate analytics insights directly into business intelligence tools.
  • Build applications that display real-time or historical user behavior data.

The SDKs abstract the complexities of direct HTTP requests, authentication, and response parsing, offering language-specific interfaces for interacting with the Google Analytics Data API. Authentication for these APIs typically relies on OAuth 2.0 for secure access to user data Google Analytics developer guides.

Official SDKs by language

Google provides official client libraries for several programming languages, designed to simplify interactions with the Google Analytics Data API. These libraries handle aspects such as authentication, request serialization, and response deserialization, allowing developers to focus on data manipulation.

Language Package/Library Name Maturity Description
Python google-analytics-data Stable Official client library for Python, providing an idiomatic interface to the Google Analytics Data API.
Java google-api-services-analyticsdata Stable Google API Client Library for Java, including support for the Analytics Data API.
Node.js @google-analytics/data Stable Official Node.js client library for interacting with the Google Analytics Data API.
PHP google/apiclient Stable Google API Client Library for PHP, supporting the Analytics Data API.
Ruby google-apis-analyticsdata_v1beta Stable Google API Client Library for Ruby, offering access to the Analytics Data API.
C# (.NET) Google.Apis.AnalyticsData.v1beta Stable Google APIs Client Library for .NET, with specific components for Analytics Data.
JavaScript (Web) gtag.js / analytics.js Stable Client-side libraries for collecting data from websites, not for API reporting.
Android Google Analytics for Firebase SDK Stable SDK for collecting analytics data from Android applications, integrated with Firebase.
iOS Google Analytics for Firebase SDK Stable SDK for collecting analytics data from iOS applications, integrated with Firebase.

Installation

Installation methods vary by programming language and ecosystem. The following provides common installation commands for the primary official client libraries:

Python Installation

Install the official Google Analytics Data API client library using pip:

pip install google-analytics-data

For more detailed instructions, refer to the Python client library quickstart.

Java Installation

For Java projects, you typically add the Google Analytics Data API client library as a dependency in your build tool (e.g., Maven or Gradle). For Maven, add the following to your pom.xml:

<dependency>
    <groupId>com.google.analytics</groupId>
    <artifactId>google-analytics-data</artifactId>
    <version>YOUR_VERSION</version>
</dependency>

Check the Java client library documentation for the latest version and Gradle instructions.

Node.js Installation

Install the Node.js client library using npm or yarn:

npm install @google-analytics/data
# OR
yarn add @google-analytics/data

Further details are available in the Node.js client library quickstart.

PHP Installation

For PHP projects, use Composer to install the Google API Client Library:

composer require google/apiclient:^2.0

Refer to the PHP client library quickstart for specific setup and usage.

Ruby Installation

Install the Ruby client library using Bundler or RubyGems:

gem install google-apis-analyticsdata_v1beta

Consult the Ruby client library quickstart for complete instructions.

C# (.NET) Installation

Install the .NET client library via NuGet Package Manager:

Install-Package Google.Apis.AnalyticsData.v1beta

More information can be found in the C# client library quickstart.

Mobile SDKs Installation (Android & iOS)

For Android and iOS applications, Google Analytics functionality is typically integrated via the Google Analytics for Firebase SDK. Installation involves adding Firebase to your project and then including the Analytics dependency:

Android (Gradle)

Add the dependency to your app-level build.gradle file:

dependencies {
    // Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:32.8.0')

    // Add the dependency for the Google Analytics library
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-analytics'
}

iOS (CocoaPods)

Add the dependency to your Podfile:

pod 'Firebase/Analytics'

Then run pod install. For detailed setup, refer to the Firebase Analytics iOS documentation.

Quickstart example

This Python example demonstrates how to use the google-analytics-data client library to run a simple report against a Google Analytics 4 property. Before running, ensure you have authenticated and set up credentials, typically via a service account or user account with OAuth 2.0, as described in the Google Analytics Data API quickstart documentation.

import os
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import RunReportRequest, DateRange, Dimension, Metric

# Replace with your GA4 Property ID
PROPERTY_ID = os.environ.get('GA4_PROPERTY_ID', 'YOUR_GA4_PROPERTY_ID')

def run_sample_report():
    """Runs a sample report on a Google Analytics 4 property."""
    client = BetaAnalyticsDataClient()

    request = RunReportRequest(
        property=f"properties/{PROPERTY_ID}",
        date_ranges=[
            DateRange(start_date="2024-01-01", end_date="today"),
        ],
        dimensions=[
            Dimension(name="city"),
        ],
        metrics=[
            Metric(name="activeUsers"),
        ],
    )

    response = client.run_report(request)

    print("Report result:")
    print(f"Row count: {len(response.rows)}")
    print("Dimension Headers:")
    for dimension_header in response.dimension_headers:
        print(f"  {dimension_header.name}")
    print("Metric Headers:")
    for metric_header in response.metric_headers:
        print(f"  {metric_header.name}")

    print("Report Rows:")
    for row in response.rows:
        dimension_values = ", ".join([d.value for d in row.dimension_values])
        metric_values = ", ".join([m.value for m in row.metric_values])
        print(f"  {dimension_values}: {metric_values}")

if __name__ == '__main__':
    # Ensure GA4_PROPERTY_ID environment variable is set or replace placeholder
    if PROPERTY_ID == 'YOUR_GA4_PROPERTY_ID':
        print("Please set the GA4_PROPERTY_ID environment variable or replace 'YOUR_GA4_PROPERTY_ID' in the script.")
    else:
        run_sample_report()

This script initializes the Analytics Data client, constructs a RunReportRequest to fetch active users by city, and then prints the results. The PROPERTY_ID should be your Google Analytics 4 property ID, which can be found in your Google Analytics admin interface.

Community libraries

While Google provides official client libraries, the broader developer community also contributes tools and wrappers that can simplify specific use cases or offer alternative interfaces. These community-driven projects are not officially supported by Google but can be valuable for certain development scenarios.

  • Google API Client Libraries (Generic): Many community projects leverage the more generic Google API Client Libraries available for various languages, which provide a foundation for interacting with any Google API, including Analytics. These often require more manual configuration for Analytics-specific calls but offer flexibility.
  • Data Visualization Libraries: There are numerous JavaScript libraries (e.g., D3.js, Chart.js) and Python libraries (e.g., Matplotlib, Seaborn) that, while not specific to the Google Analytics API, are frequently used in conjunction with data retrieved from the API to create custom visualizations and dashboards. Developers often use these to present Analytics data in a more digestible format.
  • Framework-Specific Integrations: Some web frameworks or CMS platforms may have community-maintained plugins or modules that abstract the Google Analytics API for specific purposes, such as embedding custom reports directly into a WordPress site or a Django application. These often simplify the process for developers working within those ecosystems.

When considering community libraries, it is important to evaluate their maintenance status, documentation, and community support, as they do not come with the same level of official backing as Google's own SDKs. For critical applications, relying on official client libraries is generally recommended due to ongoing support and compatibility with API updates Google Cloud Client Libraries overview.