# Google Analytics Data API Import Integration 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. ## 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 1. Open the Google API Console: [https://console.developers.google.com](https://console.developers.google.com/) 2. Sign in to the Google account you want to access through the API. 3. Select **NEW PROJECT.** ![](/assets/image2022-11-15_6-37-19.1d9cdaebe8090e09b1da6073069882026705ec395ef6deb1b1ada7e3c067a1a0.759549ad.png) 1. Name your project. ![](/assets/image2021-5-25_16-14-48.b277cc70b88e6cca1cf43c8b6277e2ad38c87f7861b31ff3f50724e5474a74a7.759549ad.png) 1. Select **Create.** 2. Enable the Google Analytics Data API v1. Navigate to the **APIs & Services** for your project, then select **+ ENABLE APIS & SERVICES** ![](/assets/screen-shot-2022-10-19-at-20.32.20.38ea4a098a00052ae67fb6a76c218161d1e01a686362218ff4b172b2feb73e8b.759549ad.png) 1. From the **Library** page, type to search for the Google Analytics Data API, then press enter. ![](/assets/screen-shot-2022-10-19-at-20.33.02.54f2917ef84f473ce46a858ceade8c8340fe1badd221d2faae972e007c3e32fc.759549ad.png) 1. Choose the Google Analytics Data API. ![](/assets/screen-shot-2022-10-19-at-20.33.12.e98c9679f15c3292b1d4fb542c7d63b53ece5f2ed725a6edb6474c00328fa516.759549ad.png) 1. Select **Enable**. ![](/assets/screen-shot-2022-10-19-at-20.33.22.da6d836e8c60e71fdff08b380f107d0889f179cb9222aa269818983f11dc4ff0.759549ad.png) 1. Access Service account to create a new service account. 2. From APIs & Services, select Credentials, then Service account. 3. Complete the fields in step 1 to create the service account. You can skip the two optional parameters. ![](/assets/image2022-10-20_0-38-14.1bc15d376a3e4cf569554405c7b5feb5ec195bebc8b363188cd09a2536db2820.759549ad.png) 1. Select **Done**. 2. Select the **Actions** tab. ![](/assets/image2021-5-25_16-51-41.4b8f0dc8cd93d274f022a59ed545e18352a68f9941d97ceaea954252837e48b7.759549ad.png) 1. Select******keys**.**** ![](/assets/image2021-5-25_16-52-18.2b10ae21626168ccdc1c23da9c0396c3bb4946b8063e24e410a3a180b6f50534.759549ad.png) 1. From the******Add key****** pulldown, select **Create new key.** ![](/assets/image2021-5-25_16-53-15.52025b8089dcd1c2c9134556c42fd3223bcc22df770a3fc678f0a48f57c01fb7.759549ad.png) 1. Choose the JSON key type to download to your local machine and select **Create.** ![](/assets/image2021-5-25_16-53-0.3f20e0e81ec0d84fd9780fd45a93c2155b2e55bada3a3c66778b6017b0413d38.759549ad.png) 1. A notice appears that your private key is saved to your computer. Keep the file in a secure place. ![](/assets/image2021-5-25_16-55-24.36e587cb336613c205f95729aae7fa9c86927e847a118a8f28a09631885b87c9.759549ad.png) 1. Edit the configuration file for the integration using the private key information. Your file might look like this: ```json { "type": "service_account", "project_id": "central-stream-314923", "private_key_id": "94d03bf7dd9c05bc122c695d1aa13f2a8a28f88e", "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w**********************AoIBAQCNhAICLr/dozCQ\nTW9ZNMNJ6RF+fVqhd0FUbw0VBIwy6BWu/LuaocJrzl2DHChAl0PNvGCDUAObBTRz\nbUT/HOu47q**********7ENGK\nOir9VChG+Qubq25bAtOq/yTVEPJgnj*******AGVjojVnK4f\n2YtW6ti7xBPwFBF1RPY56yTDeVQVko+KK3x+LFS+lTj1+jBBjvedWHrpQQfRHqV/\nVtXyKyDybQlnfAOucMHzMxjQVLN4f9D7JVxCe52Wp7RaALCIdkKDqN/ffkNMF9QT\nCjffudeTAgMBAAECggEAFMQnS0yy6QI2cSZ7zXpZofHqmEYq04DdfFdjcw8cx6eY\n7vm1Seas0g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STqVjIaJg0zYFvQ5oow5zgu1lk\n46nxtfQm3U58lILErGsmClxcOZR2nO7kvm0PJMUgENADGhP5pqE+8w+e4JC45Ojw\nX7X+hhL/a7pu2Un9O/rXZVM=\n-----END PRIVATE KEY-----\n", "client_email": "meg-td-service-account@central-stream-314923.iam.gserviceaccount.com", "client_id": "117460147437348814027", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/meg-td-service-account%40central-stream-314923.iam.gserviceaccount.com" } ``` #### 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".` 2. Sign in to Google Analytics [https://analytics.google.com](https://analytics.google.com/). 3. Select **Admin**, then navigate to the desired account/property. ![](/assets/image2022-10-20_0-3-20.33484ce23dcec6c026ac92cd9cd418ea02036b0e6c8cbad52444a2b7181ea49b.759549ad.png) 1. In the Account or Property pane (depending upon whether you want to add users at the account or property level), select **Access Management**. 2. From the Account permissions list, select +, then select **Add users**. ![](/assets/image2022-10-20_0-16-4.8a374944e07227e2f202b1f90373e6b7e235931f8bb479baa17e78b819012685.759549ad.png) 1. 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. ![](/assets/screen-shot-2022-11-02-at-14.27.39.6c9e128cef8a77c7f83eec3565a691d638aa66dada2a979f815185fc44f24506.759549ad.png) ### 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/](https://analytics.google.com/) 2. Select Admin, and navigate to the desired account/property ![](/assets/image2022-10-20_0-48-49.0da1b657da69c340e15b36018f5541dffb2162166b6456874c2bf776bcac1a51.759549ad.png) 1. 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](https://analytics.google.com/). 2. Select **Admin**, and navigate to the desired account and select **Configure.** ![](/assets/image2022-11-15_6-32-24.3e578598d0c4edacd76cdb9e0c4fb82a00f499cc1a7c814943d4d7a3b1d4403b.759549ad.png) 1. Select **Custom definitions.** ![](/assets/image2022-11-15_6-33-25.e80a2313a0b9bba8a013035991f8071772e34a46a227fc88f472fee661687c92.759549ad.png) ![](/assets/image2022-10-20_15-12-42.b653a4c50ff3405790ec44ec0de2504f739cf31817468e4980fe3117b4d4ff61.759549ad.png) 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 Authentication 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. ![](/assets/image2022-10-20_15-20-38.547a5488ddc0fbe9bbdd998dc3827d25a113e04f27a2b5a7f7b7b81678c65783.759549ad.png) 1. Select **Create Authentication**. 2. Choose one of the following authentication methods: JSON Key Enter the JSON key information. Make sure that you include the private key of the service account. Make sure that the entire JSON key information is in brackets {…}. 1. Locate and open the JSON file that you downloaded from the Google Cloud Platform in your favorite text editor.  For example: ![](/assets/image2021-6-3_9-45-59.7ade25ea5d865bb97e721985bd653ee35d1628a5632c2a834438e3cdd3b55642.759549ad.png) 2. Copy and past json key into text box. 3. Select **Continue**. ![](/assets/image2022-10-20_15-25-16.75cd110ca4e91a7adad1ed51ae931eef957491f3c0f865f3d89ce522ddf925b9.759549ad.png) 4. Now that you've selected your authentication method, enter a name for your connection. 5. Select **Done.** OAuth Content for Tab 2 ![](/assets/image2022-10-20_15-22-20.05db5f25bfd03f5b4c75d28fab7ae8333716493b54f177e618c4dd1df31dcff2.759549ad.png) 1. Select **OAuth**. 2. If you know the OAuth connection, type it, otherwise you can make the selection to connect a new account. 3. If you chose to connect a new account, you are taken through a series of screens where you select the account to link and specify that you are OK granting access to that account by Treasure Data.  You are then returned to Authentications in TD Console. 4. Search and select **Google Analytics Data API**. ![](/assets/image2022-10-20_15-20-38.547a5488ddc0fbe9bbdd998dc3827d25a113e04f27a2b5a7f7b7b81678c65783.759549ad.png) 5. Select your account to show in the Authentication connection field. 6. Select **Continue**. 1. Now that you've selected your authentication method, enter a name for your connection. 2. Select **Done.** ### Create a Source After creating the authenticated connection, you are automatically taken to Authentications. You must enter Dimension, Metric or Custom Dimension, Custom Metric information from Google Analytics 4. Visit [https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema](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. Select **Next**. 4. The Source Table dialog opens.  You must provide Dimensions or Custom Dimensions, and Metric or Custom Metrics information. ![](/assets/screen-shot-2022-11-02-at-14.36.21.6b55b3b30720d37ed1fba62579c67f393a0755b6591c5943177dd707f802aabe.759549ad.png) ![](/assets/screen-shot-2022-11-02-at-14.34.52.dc5b47fb5d00a4745b17c2631283363880c4b31dfb6472e95ad6608fea91a487.759549ad.png) 1. 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](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](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: - audienceId - customUser:test_cs_dimension | | Metrics | Metrics or Custom Metrics name. Visit [https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema](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: - promotionViews - promotionClicks - customEvent:test_cs_metric | | 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:  - 2022-03-12 - today - yesterday - 4daysAgo | | 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:  - 2022-03-12 - today - yesterday - 4daysAgo | | Advanced Payload | Advanced report definition in JSON format. Visit [https://developers.google.com/analytics/devguides/reporting/data/v1/advanced](https://developers.google.com/analytics/devguides/reporting/data/v1/advanced) for more detail. For example:`{ "metrics": [ { "name": "cohortActiveUsers" } ], "dimensions": [ { "name": "cohort" }, { "name": "cohortNthWeek" }, { "name": "country" } ], "cohortSpec": { "cohorts": [ { "dimension": "firstSessionDate", "dateRange": { "startDate": "2022-10-04", "endDate": "2022-10-10" } } ] } }` | Example: ![](/assets/screen-shot-2022-11-02-at-14.39.29.f813a578482cc4756d36f4b503a5a69676590d4e7c51b0a77fb7f07288607339.759549ad.png)![](/assets/screen-shot-2022-11-02-at-14.40.35.df7e3a8613d9b86214c6675da7a542ecd5cbcb51bc1d24b2261257b66eb36dac.759549ad.png) 1. Select **Next**. ![](/assets/image2022-10-20_16-3-50.4ba82660399f46382dacbc82bcf5ae2d3976d39575410ed6ceb60a5b8d75ee0b.759549ad.png) 1. Select **Next**. #### Data Preview Data preview is optional, and you can safely click **Next** to go to the next page of the dialog if you would like. 1. Display a preview of your data before running the import by selecting **Generate Preview**. The data shown in the data preview is approximated from your source. It is not the actual data that is imported. 2. Verify that the data looks approximately like you expect it to. ![](/assets/snippet-data-preview-2024-02-09.27dc5fd8772fca4f7f44ab28c00476ae1894744fe1e75d06932628929cc7bff1.4e139be3.png) 3. 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. 1. 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. ![](/assets/dataplacement.20cdd6c14bacea9f716bed4b7443652bd67752f2de981c664364d0236b8eaa79.7705d4c2.png) 2. Select a **Database** > **Select** **an existing** or **Create New Database**. 3. Optionally, type a database name. 4. Select a **Table**> **Select an existing** or **Create New Table**. 5. Optionally, type a table name. 6. 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. 1. 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. 2. Select the **Timezone** for your data storage. 3. Under **Schedule**, you can choose when and how often you want to run this query. #### Run once 1. Select **Off**. 2. Select **Scheduling Timezone**. 3. Select **Create & Run Now**. #### Repeat Regularly 1. Select **On**. 2. Select the **Schedule**. The UI provides these four options: *@hourly*, *@daily* and *@monthly* or custom *cron*. 3. You can also select **Delay Transfer** and add a delay of execution time. 4. Select **Scheduling Timezone**. 5. 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**. ![](/assets/image2022-10-20_20-21-58.cd1a737fe30c468f5a6a2439f109c652c482420d9548beb6cd43af604dd1e68d.759549ad.png) 1. Select a connector and then select the more menu (…). Select **Copy Unique ID.** Define a workflow task using td_load> operator | | | --- | | `+load:` `td_load>: unique_id_of_your_source` `database: ${td.dest_db}` `Table: ${td.dest_table}` | Run a workflow