GA4 Admin API: How To Automate GA4 Accounts Management

Table of Contents

You might have heard of the term API before, maybe you are even a professional who works daily with APIs. Today we are not going to talk about one of the most useful APIs available for GA4: GA4 Admin API.

Important

You can find most of the concepts we will talk about in Google’s own documentation on how to use the admin API

If you are familiar with the concept of an API, feel free to move to the section about setting up a service account.

What is an API?

2 Humans can use a language – English – to communicate with each other, platforms (apps, websites…) interact using an API (Application Programming Interface).

For instance, GA4 has its own API that allows you to access your data. You can ask for a specific metric, dimension, and setting. if you follow the documentation on how to make the request, you will get back the data you asked for.

What is GA4’s Admin API?

If you want to access the settings of your accounts: properties, key events (AKA conversions), data streams and other settings. You will need to do this manually by going to each account in the admin section and making any changes.

This is fine if you are managing a few accounts. But imagine if you are like me managing your client s (agencies) and the client’s clients accounts (final client). You guessed it right: things can get messy very quickly.

That’s what programming is all about: automating mind-numbing tasks (among many very cool things!)

How To Configure The GA4 Admin API?

The process is simple, and we can break down to 3 stages:

  1. Create a Google Cloud project and enable the APIs
  2. Set up a service account that allows us to access GA4 data automatically
  3. Add the service account address to GA4 and make a simple request

Step #1: Configure Cloud Project and admin API

First, Browse to Google Cloud, and create an account by clicking on START FREE,

you will need to create a new project by clicking on NEW PROJECT.

Feel free to name the project anything you like.

Step #2: Enable the admin API

We have a Google cloud project, it’s time to enable our GA4 Admin API. Make sure you are on the project you created. Click on APIs & Services.

After clicking on Enable APIs & Services, search for GA4 Admin API and enable it.

Step #3: Create a service account

ℹ️ What is a service account?

The service account allows us to access GA4 without using the UI. This is done via a unique key we will download.

Search for Service Accounts and click on create service account.

You can name the access key whatever you want. The description is optional as well.

Give your service account an owner or editor role.

Skip step 3️⃣ and click on Done. Finally, we are going to create and download the key. First, click on your service account email.

Next, navigate to the keys tab and click on generate key

Select JSON as the file download format.

Step #4: Add service account email to GA4 account

The last step is to give the service account access to our ga4 account. Copy the address that looks like this:

random_name@apis-427908.iam.gserviceaccount.com
SQL

Go To GA4 account and add the email address.

We are done, In the next section we are going to test if our API request is working.

How to make an API request using GA4 Admin API?

I will open VS Code and start preparing my set-up. First, let’s create a folder with the access key

I will use Python notebook to make the request.

Step #1: Installing Necessary Libraries

Before you start coding, you need to install the required libraries that allow your code to communicate with the GA4 Admin API.

# 1️⃣ This command installs the Google Analytics Admin library, which provides functions to interact with GA4.
%pip install google-analytics-admin
Python

Step #2: Set Up Authentication

You need to authenticate your service account to access the API securely. This involves using a JSON key file that contains your credentials.

# 2️⃣ we tell our code where to find the credentials (the key.json file) needed to access Google Analytics securely.

from google.oauth2.service_account import Credentials
import os

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'key.json'
Python

Step #3: Create a Client for the API

Next, you create a client that allows you to make requests to the GA4 Admin API.

# 3️⃣ This line initializes the client, which acts like a bridge between your code and Google Analytics, allowing you to send requests and receive data.

from google.analytics.admin import AnalyticsAdminServiceClient 
client = AnalyticsAdminServiceClient()
Python

Step #4: List Accounts

Now, let’s test the API by getting the account name programmatically

# 4️⃣  This section fetches all accounts associated with your service account. It stores the main account's ID and name for later use. The print statement shows which account is the main one.

accounts_info = client.list_accounts()
account_list = []
account_id = ''
parent_account = ''

for account in accounts_info:
    account_list.append(account.name.split('/')[-1])
    account_id = account_list[0]
    parent_account = account.name

print(f'Main account: {account_id}')
Python

Step #5: List Properties Under the Account

Once you have the account ID, you can list all properties (websites or apps) linked to that account.

# 5️⃣ Here, we request a list of properties under our main account. We collect their names, time zones, and currencies for further analysis.

from google.analytics.admin_v1alpha.types import ListPropertiesRequest

filter_expression = f"parent:accounts/{account_id}"
properties_request = ListPropertiesRequest(filter=filter_expression)
properties = client.list_properties(properties_request)

properties_names = []
properties_time_zones = []
properties_currencies = []

for property in properties:
    properties_names.append(property.display_name)
    properties_time_zones.append(property.time_zone)
    properties_currencies.append(property.currency_code)
Python

Step #6: Create a DataFrame for Easy Viewing

Finally, Once you have the account ID, you can list all properties (websites or apps) linked to that account.

# 6️⃣ This section organizes the collected data into a table format (DataFrame), making it easier to read and analyze.

import pandas as pd
df = pd.DataFrame({
    'Name': properties_names,
    'Currency': properties_currencies,
    'Timezone': properties_time_zones
})

df
Python

How to create a New GA4 Property using GA4 Admin API?

If you want to create a new property in your GA4 account, you can do so with the following code:

# ℹ️ This part defines a new property called "API Test" and sends a request to create it under your main account. You specify details like time zone and currency here.

from google.analytics.admin_v1alpha.types import CreatePropertyRequest, Property

new_property = Property(
    display_name="API Test",  
    parent=parent_account,
    time_zone="America/New_York",       
    currency_code="USD"
)

request = CreatePropertyRequest(property=new_property)
response = client.create_property(request=request)
Python

Finally, when we go back to our GA4 account, you can see that our property API test was created

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Go Further