# Connect Google Analytics 4

Connecting a Google Analytics 4 property tags your public PiGi booking pages, reports each paid online booking to GA4 as a purchase, and shows your visitors, traffic sources and ad spend on Marketing → Analytics. You need a GA4 property, its Measurement ID (G-…) and property ID, and an OAuth client from your own Google Cloud project.

Source: https://help.pigi.io/guides/integrations/marketing-and-sales/google-analytics

## Quick answers

**What does PiGi send to Google Analytics?**
A page view for every page of your public PiGi site, a `begin_checkout` event when a
customer reaches the payment step of the booking wizard, and a `purchase` event — with
the booking reference, value and currency — when an online booking is confirmed. The
purchase is sent from PiGi's server, so it still counts if the customer closes the tab or
blocks scripts.

**Where do I find my Measurement ID?**
In Google Analytics: **Admin → Data collection and modification → Data streams**, click
your web stream, and copy the **Measurement ID** that starts with `G-`.

**What is the difference between the property ID and the Measurement ID?**
The **GA4 property ID** is digits only, from **Admin → Property settings**; PiGi uses it to
read your reports. The **Measurement ID** starts with `G-` and tags your pages. PiGi needs
both.

**Do I need the Measurement Protocol API secret?**
It is optional but recommended. Without it PiGi cannot report a paid booking to GA4 as a
purchase. Create it under your web stream → **Measurement Protocol API secrets** →
**Create**.

**Do I need to mark purchase as a key event?**
No. Google marks `purchase` as a key event by default on web properties.

**Why does Marketing → Analytics say Google Ads isn't linked?**
Ad cost, clicks and return on ad spend only reach PiGi through GA4's link to Google Ads.
Link them in GA4 under **Admin → Product links → Google Ads links**. Nothing changes in
PiGi.

**Are my walk-in and desk bookings sent to Google?**
No. Only bookings made on your public site in a browser that GA4 tagged are reported.

**Does it track staff using the admin app?**
No. The tag is only on your public booking site.

## What this does

Google Analytics 4 measures who visits your booking site and which campaigns produce
bookings. PiGi tags the public pages with your Measurement ID, reports confirmed online
bookings as purchases, and reads the property back to show visitors, channels, landing
pages and ad spend on **Marketing → Analytics** beside your booking revenue. GA4 itself is
free; Google's own terms apply.

## Before you start

- The **admin** role in PiGi.
- A Google Analytics account with permission to create a property and data stream.
- Access to the Google Cloud console to create an OAuth client.

## Set up Google Analytics and Google Cloud

1. In Google Analytics, go to **Admin → Create → Property**, and follow the steps.
2. Under **Admin → Data collection and modification → Data streams**, click **Add
   stream → Web**, enter your booking site address (your custom domain, or
   `<your-slug>.pigi.io`) and click **Create stream**.
3. Copy the **Measurement ID** (`G-…`) from **Stream details**.
4. On the same stream, open **Measurement Protocol API secrets**, click **Create**, and
   copy the **Secret value**.
5. Copy the numeric **Property ID** from **Admin → Property settings**.
6. In the Google Cloud console, enable the **Google Analytics Data API** on your project
   (**APIs & Services → Library**).
7. Set up the consent screen under **Google Auth platform → Branding**. For an External
   app, set the publishing status on **Audience** to In production; in Testing, Google's
   access expires after 7 days.
8. Go to **Google Auth platform → Clients → Create Client → Web application**. Leave
   **Authorized redirect URIs** for now and click **Create**. Copy the client ID and secret.
   For every screen in detail, see [Get Google OAuth credentials](google-oauth-client.md).

## Connect it to PiGi

1. Open **Settings → Communication → Integrations**, click the **Google Analytics 4**
   tile and then **Connect Google Analytics 4**.
2. Click **Add credentials** and fill in **OAuth client ID**, **OAuth client secret**,
   **GA4 property ID**, **Measurement ID** and **Measurement Protocol API secret**. Press
   **Save**.
3. Click **Google authorisation**. Copy the redirect URI shown at the bottom.
4. Back in Google Cloud, edit the OAuth client, add that URI under **Authorized redirect
   URIs** exactly as shown, and save.
5. In PiGi, click **Connect Google** and allow access with a Google account that can
   read the property. The dialog then shows **Authorised**.

## Test it

1. Open your public booking site in a private window.
2. In GA4, open the Realtime report and confirm your visit appears.
3. Make and pay for a test booking, then look for the `purchase` event in Realtime.
4. Open **Marketing → Analytics** in PiGi to see the website figures.

## Troubleshooting

- **"Google turned the authorisation down."** The redirect URI on the OAuth client does
  not exactly match the one in the dialog. A trailing slash counts as different.
- **"Google did not return a refresh token…"** Remove PiGi from your Google account
  permissions, then connect again.
- **"No property ID saved yet."** Add the **GA4 property ID** under **Update credentials**.
- **"This integration has no OAuth client ID or secret yet."** Save both before pressing
  **Connect Google**.
- **Marketing → Analytics says "Google Analytics isn't connected".** Authorisation is
  missing or the property ID is missing.
- **"No conversions are being measured yet."** No purchase has reached GA4 yet. Check the
  API secret is saved and make a test booking.

## Known limitations

- An OAuth client is required even if you only want the page tag.
- **Test connection** reports that testing isn't supported for this provider yet.
- PiGi loads the tag without asking visitors for cookie consent first. Check what your
  local privacy law requires.
- Only bookings made on the tagged public site are reported as purchases.

## Sources

- [Google Analytics: Set up Analytics for a website](https://support.google.com/analytics/answer/9304153)
- [Google Analytics: Property ID](https://developers.google.com/analytics/devguides/reporting/data/v1/property-id)
- [Google Analytics: Send Measurement Protocol events](https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events)
- [Google Analytics: Mark events as key events](https://support.google.com/analytics/answer/13128484?hl=en)
- [Google: Create access credentials](https://developers.google.com/workspace/guides/create-credentials)
- [Google: Configure the OAuth consent screen](https://developers.google.com/workspace/guides/configure-oauth-consent)
- [Google: Using OAuth 2.0 (refresh token expiration)](https://developers.google.com/identity/protocols/oauth2)
