The Google Analytics Data API gives you programmatic access to Google Analytics 4 (GA4) report data. Google Analytics 4 helps you understand how people use your web, iOS, or Android app.
This Data Connector is in Beta. For more information, contact support@treasuredata.com.
Prerequisites
Basic knowledge of Treasure Data
Basic knowledge of Google Analysis 4 (GA4)
A Google Analytics 4 account
Requirements
A Google Analytics 4 account or Service account JSON key
Authentication Method
Google User Account: OAuth | Using OAuth is the most common method. This method requires fewer setup steps. You can skip the rest of this section and go directly to TD Console. |
Google Service Account—JSON key | Using JSON key might be required for your implementation. This method requires the setup steps from the Google API console. |
Set the Google Analytics Data API for JSON Key Authentication
Add service account to the Google Analytics 4
You need the JSON key file in the previous step to add a service account to the Google Analytics 4. You must have Analyst permissions to add a user to the GA4 property being accessed via the Google Analytics Data API v1.
1. Using a text editor, open the json key
file and search for client_email
field to collect the service account email address. It might look similar to the following address: "client_email": "meg-td-service-account@central-stream-314923.iam.gserviceaccount.com".
client_email
2. Sign in to Google Analytics https://analytics.google.com.
3. Select Admin, then navigate to the desired account/property.
4. In the Account or Property pane (depending upon whether you want to add users at the account or property level), select Access Management.
5. From the Account permissions list, select +, then select Add users.
6. Enter the email address you set aside from json key. Select Viewer role or higher (Analyst, Editor, Administrator). Select Add.
Your service account now has access to use Google Analytics 4 through the Google Analytics Data APIs.
Obtain the Property ID from Google Analytics
You must have the Property ID to create import data to Treasure Data.
1. Sign in to Google Analytics https://analytics.google.com/
2. Select Admin, and navigate to the desired account/property
3. Capture or copy the Property ID, it is necessary for the creation of the Treasure Data Import Data.
Obtain the custom dimension or custom metrics name from Google Analytics
You may want to customize your data by creating custom dimensions or custom metrics.
1. Sign in to Google Analytics https://analytics.google.com.
2. Select Admin, and navigate to the desired account and select Configure.
3. Select Custom definitions.
You can obtain custom dimension or metric name by concatenating: custom{Scope}:{dimension or metric name}
For example, the previous image uses customUser:test_cs_dimension for the custom dimension name and customEvent:test_cs_metric as custom metric name.
Creating the Data Connector from the TD Console
Create a New Connection
In Treasure Data, you must create and configure the data connection prior to running your query. As part of the data connection, you provide authentication to access the integration.
1. Open TD Console.
2. Navigate to Integrations Hub > Catalog.
3. Search for and select Google Analytics Data API.
4. Select Create Authentication.
5. Choose one of the following authentication methods:
6. Now that you've selected your authentication method, enter a name for your connection.
7. Select Done.
Transfer Your Data to Treasure Data
After creating the authenticated connection, you are automatically taken to Authentications.
You must enter Dimension, Metric orCustom Dimension, Custom Metric information from Google Analytics 4. Visit https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema for list of Dimensions and Metrics
1. Search for the connection you created.
2. Select New Source.
3. Type a name for your Source in the Data Transfer field.
4. Select Next.
The Source Table dialog opens. You must provide Dimensions or Custom Dimensions, and Metric or Custom Metrics information.
5. Edit the following parameters:
Parameters | Description |
---|---|
Property ID | Your GA4 Property ID. Read section Obtain the Property ID from Google Analytics |
Report Mode | Basic or Advanced report. Visit https://developers.google.com/analytics/devguides/reporting/data/v1/advanced in case you want to build an advanced report |
Dimensions | Dimensions or Custom Dimension name. Visit https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema for the list of dimensions and read Obtain the custom dimension or custom metrics name from Google Analytics to retrive list of custom Dimensions For example:
|
Metrics | Metrics or Custom Metrics name. Visit https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema for the list of dimensions and read Obtain the custom dimension or custom metrics name from Google Analytics to retrieve list of custom Metrics For example:
|
Start Date | The date to start query report data. Support format YYYY-MM-DD, today, yesterday, NdaysAgo. The parameter value is case-sensitive For example:
|
End Date | The date to end query report data. Support format YYYY-MM-DD, today, yesterday, NdaysAgo. The parameter value is case-sensitive For example:
|
Advanced Payload | Advanced report definition in JSON format. Visit https://developers.google.com/analytics/devguides/reporting/data/v1/advanced for more detail. For example: { |
Example:
6. Select Next.
7. Select Next.
Data Preview
You can see a preview of your data before running the import by selecting Generate Preview.
Data shown in the data preview is approximated from your source. It is not the actual data that is imported.
Click Next.
Data preview is optional and you can safely skip to the next page of the dialog if you want.To preview your data, select Generate Preview. Optionally, click Next.
Verify that the data looks approximately like you expect it to.
Select Next.
Data Placement
For data placement, select the target database and table where you want your data placed and indicate how often the import should run.
Select Next. Under Storage you will create a new or select an existing database and create a new or select an existing table for where you want to place the imported data.
Select a Database > Select an existing or Create New Database.
Optionally, type a database name.
Select a Table> Select an existing or Create New Table.
Optionally, type a table name.
Choose the method for importing the data.
Append (default)-Data import results are appended to the table.
If the table does not exist, it will be created.Always Replace-Replaces the entire content of an existing table with the result output of the query. If the table does not exist, a new table is created.
Replace on New Data-Only replace the entire content of an existing table with the result output when there is new data.
Select the Timestamp-based Partition Key column.
If you want to set a different partition key seed than the default key, you can specify the long or timestamp column as the partitioning time. As a default time column, it uses upload_time with the add_time filter.Select the Timezone for your data storage.
Under Schedule, you can choose when and how often you want to run this query.
- Run once:
Select Off.
Select Scheduling Timezone.
Select Create & Run Now.
Repeat the query:
Select On.
Select the Schedule. The UI provides these four options: @hourly, @daily and @monthly or custom cron.
You can also select Delay Transfer and add a delay of execution time.
Select Scheduling Timezone.
Select Create & Run Now.
After your transfer has run, you can see the results of your transfer in Data Workbench > Databases.
Import via Workflow
You can import data from Google Analytics 4 by using td_load>: operator of workflow. If you already have created a SOURCE.
1. In the TD Console, navigate to Integrations Hub > Sources.
2. Select a connector and then select the more menu (…). Select Copy Unique ID.
3. Define a workflow task using td_load> operator
|