> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hightouch.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Google Analytics

> Send events directly to Google Analytics from your data warehouse

## Supported syncing

| Type | Description | Supported Sync Modes | API Reference |
| - | - | - | - |
| **Events** | Sync events to GA4 via the Measurement Protocol API | Insert | [Measurement Protocol Reference](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=firebase) |
| **Anonymous Audiences** | Sync events and user properties to GA4 to create anonymous audiences | Upsert | [Audiences reference](https://support.google.com/analytics/answer/12799087?hl=en) |

For more information about sync modes, refer to the [sync modes](https://docs.hightouch.com/syncs/types-and-modes.md#sync-modes) docs.

## Connect to Google Analytics

Go to the [**Destinations** overview page](https://app.hightouch.com/destinations?from=docs) and click the **Add destination** button. Select **Google Analytics** and click **Continue**. You can then authenticate Hightouch to **Google Analytics** by entering a Google Analytics **API Secret**.

To generate an API secret, navigate to **Admin > Data Streams > choose your stream > Measurement Protocol > Create**. If you don't have any existing data streams you need to [create one](https://support.google.com/analytics/answer/9304153?sjid=1280583774983537157-NA#stream) first.

If you are syncing anonymous audiences, you must additionally authenticate via OAuth to grant Hightouch the `analytics.edit` scope. This is needed to create new audiences in GA4.

## Sync configuration

Once you've set up your Google Analytics destination and have a [model](https://docs.hightouch.com/getting-started/concepts.md#models) to pull data from, you can set up your sync configuration to begin syncing data. Go to the [**Syncs** overview page](https://app.hightouch.com/syncs?from=docs) and click the **Add sync** button to begin. Then, select the relevant model and the Google Analytics destination you want to sync to.

### Syncing events

Hightouch supports sending following Google Analytics event types:

* [**Firebase App Events**](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=firebase)
* [**Gtag.js Events**](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#payload)

Firebase app events require an associated **Firebase app ID** (`firebase_app_id`) and **app instance ID** (`app_instance_id`). You can find the `firebase_app_id` in the Firebase console in **Project Settings > General > Your Apps > App ID**. You can retrieve the `app_instance_id` through the [Firebase SDK](https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics#public-taskstring-getappinstanceid).

If you are having issues syncing your Firebase app events, check out [Google's troubleshooting docs](https://developers.google.com/analytics/devguides/collection/protocol/ga4/troubleshooting?client_type=firebase).

> [!TIP]
>
> The `app_instance_id` isn't the same as your `firebase_app_id`. The `firebase_app_id` identifies your app uniquely, whereas `app_instance_id` identifies a single installation of the app uniquely.

Gtag.js events require an associated **measurement ID** (`measurement_id`) and **client ID** (`client_id`). You can find the `measurement_id` in the Google Analytics UI in **Admin > Data Streams > choose your stream > Measurement ID**. You can retrieve the `client_id` using [these examples](https://developers.google.com/tag-platform/gtagjs/reference#get) from Google.

If you are having issues syncing your Gtag.js events, check out [Google's troubleshooting docs](https://developers.google.com/analytics/devguides/collection/protocol/ga4/troubleshooting?client_type=gtag).

> [!TIP]
>
> All users of your website have the same `measurement_id`, but the `client_id` value is different for every user of your app.

Refer to Google Analytics [required parameters](https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?client_type=firebase#required_parameters) for more information.

> [!WARNING]
>
> To ensure syncs send each event, your event model must use a truly unique [primary key](https://docs.hightouch.com/getting-started/concepts.md#unique-primary-key-requirement). See the [events syncs](https://docs.hightouch.com/syncs/types-and-modes.md#event-syncs) documentation for more information.

#### Measurement ID

Your **measurement ID** in Google Analytics is an identifier for a web data stream. It typically starts with `UA`, `MO`, or `G` followed by 10-20 characters, for example `UA-12345678-9`. You can find your measurement ID by following the **Find your Measurement ID** link in [Google's docs](https://support.google.com/analytics/answer/12270356?hl=en#:~:text=A%20Measurement%20ID%20is%20an,same%20as%20your%20destination%20ID).

You can also use the steps outlined in the preceding section.

#### Event name

Google Analtyics requries an event name for all events. You can either provide a static value or select to **use a column** from your model data. Hightouch syncs the static or column value as the `events[].name` parameter Google Analytics requires. See [Google's events reference](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events) for all event name options.

#### Event timestamp

Optionally, you can decide to select a model column that contains your event timestamps. Timestamps should be in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format or [Unix timestamps](https://en.wikipedia.org/wiki/Unix_time) in milliseconds, which are automatically converted to the format required by Google Analytics.

> [!TIP]
>
> If this is selection is empty, Google Analytics creates an event timestamp by using the time the event arrives at their server.

#### Field mapping

Field mapping is where you select which event parameters you want to send to Google Analytics. You can map data from any of your model columns to event parameters in Google Analytics. Ensure your model column data type matches the data type of the parameter you want to sync to.

Different event types require different parameters. For example, `purchase` events require the following parameters:

* `currency` (string)
* `value` (number)
* `transaction_id` (string)
* `items` (array with additional [required parameters](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#purchase_item))

![Field mapping in the Hightouch UI](https://mintcdn.com/hightouch-3c037897/L_vQfA0JQEpeGUUo/images/destinations/destination-googleanalytics-field-mapping.png?fit=max\&auto=format\&n=L_vQfA0JQEpeGUUo\&q=85\&s=832f84e3df692fc9e584b05756204cca)

You can use Hightouch's [advanced mapper](https://docs.hightouch.com/syncs/mapping-data.md#advanced-mapper) to map [column](https://docs.hightouch.com/syncs/mapping-data.md#column-values), [variable](https://docs.hightouch.com/syncs/mapping-data.md#variable-values), and [static](https://docs.hightouch.com/syncs/mapping-data.md#static-values) values, for example, "USD" for `currency`.

See [Google's events reference](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events) for the suggested parameters for each event.

### Syncing audiences

Create anonymous audiences based on user properties in GA4.

Hightouch creates anonymous audiences in GA4 by first creating a new [custom dimension](https://support.google.com/analytics/answer/14240153?hl=en) based on the name of your audience. The custom dimension will be in the format `HT_<audience name>`.

Once the dimension is created, Hightouch creates an audience using the new custom dimension as a filter. Hightouch will send events with the custom dimension value set to `1` in user properties, and filter based on that value. Anonymous users with the custom dimension property set will be included in your anonymous audience.

When a record is removed from your audience, the custom audience value is set to `-1` and therefore removed from your audience.

#### Audience population

There may be a lag between syncing events and user properties to GA4 and seeing audiences being populated.
GA4 recommends sending an event with user properties to see your audience populated with the least lag time.

## Tips and troubleshooting

To see the exact API requests and responses from each sync run, use the [live debugger](https://docs.hightouch.com/syncs/debugger.md). To get notified by Slack, PagerDuty, SMS, or email when a sync fails, set up [sync alerts](https://docs.hightouch.com/syncs/alerting.md). If you run into an issue this page doesn't cover, [contact support](#pylon-chat).

### Validating events

According to the [Google Analytics documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/validating-events?client_type=gtag), the Google Analytics Measurement Protocol for Google Analytics 4 does not return HTTP error codes, even if an event is malformed or missing required parameters. To ensure your events are valid, you should test them against the Measurement Protocol Validation Server before deploying them to production.

You can run syncs using the Measurement Protocol Validation Server, if selected in your Hightouch sync config. Alternatively, before running your sync in Hightouch, use this [tool from Google Analytics](https://ga-dev-tools.google/ga4/event-builder/) to validate events and make sure you're not missing any parameters.

If you don't validate the event and are missing parameters in the [field mapping](#field-mapping) section, the sync will look healthy in Hightouch because the response from GA4 won't contain errors, but you won't see events in GA4.
