# Analytics: GA4 and Google Tag Manager

Source: https://help.ruuster.com/idx/guide-analytics

> The four GA4 events the recipe components send, their fields, a ready Google Tag Manager container to import step by step, and how your lead route receives the same campaign ids with each lead.

The recipe components send four GA4 recommended events to `window.dataLayer`: `view_item_list`,
`view_item`, `search` and `generate_lead`. Your own Google Tag Manager container sends them on to
GA4. This page lists every field, gives you a container you can import in a few minutes, and shows
how your lead route receives the same campaign ids with each lead.

The recipes push events to the data layer and leave tag loading to your site. You load Google Tag
Manager once, with your own container ID, as in the layout below.

## The four events

| Event | Sent by | When | Fields |
|---|---|---|---|
| `view_item_list` | `ListingGrid` | Once, when the grid is shown | `ecommerce.item_list_name`, `ecommerce.items[]` |
| `view_item` | `ListingDetail` | Once, when the listing page is shown | `ecommerce.currency` (`USD`), `ecommerce.value` (the price), `ecommerce.items[]` (one item) |
| `search` | `SearchBox` | When the visitor picks a place | `search_term` (the place's name, for example "Powder Springs, GA") |
| `generate_lead` | `TourForm`, `ContactForm`, `RegistrationForm` | After your lead route answers with a 2xx status | `lead_source`, `form_name`, `listing_id`, `listing_name`, `listing_price`, and the campaign ids |

Each item in `items[]` has these fields:

| Field | Value |
|---|---|
| `item_id` | The MLS number (the listing's API id when the board sends no MLS number) |
| `item_name` | The address. For a listing whose address is hidden, "Home in" and the city, such as "Home in Powder Springs" |
| `price` | The price, or the sold price on a sold listing |
| `item_category` | The property type |
| `item_category2` | The home type (house, condo, townhome, co-op) |
| `item_brand` | The board's name |
| `index` | The card's position in the grid, from 0 |
| `item_list_name` | The grid's list name |

`generate_lead` fields:

| Field | Value |
|---|---|
| `lead_source` | `tour`, `contact` or `registration` |
| `form_name` | `tour_form`, `contact_form` or `registration_form` |
| `listing_id`, `listing_name`, `listing_price` | The listing the visitor asked about, named the same way as `item_id`, `item_name` and `price` |
| `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content` | The UTM tags of the page the visitor arrived on |
| `gclid`, `fbclid` | The Google Ads and Meta click ids of that page |

Before each `view_item_list` and `view_item`, the recipes push `{ ecommerce: null }`, as Google
recommends, so items from one event never carry into the next.

## Step 1: load your Google Tag Manager container

Set `NEXT_PUBLIC_GTM_ID` to your container ID (it starts with `GTM-`) and load the container once in
your root layout:

<p class="code-title"><code>packages/ruuster-idx-recipes/snippets/layout.tsx</code></p>

```tsx
// app/layout.tsx: load the recipes' stylesheet with your brand kit once, and your own Google Tag Manager container.
import type { ReactNode } from "react";
import Script from "next/script";
import { createClient } from "@ruuster/idx";
import { RecipesStyles } from "@ruuster/idx-recipes";

const idx = createClient({ secretKey: process.env.IDX_SECRET_KEY, baseUrl: process.env.IDX_BASE_URL || undefined, next: { revalidate: 3600 } });
const GTM_ID = process.env.NEXT_PUBLIC_GTM_ID;

export default async function RootLayout({ children }: { children: ReactNode }) {
  // #region theme
  // themeVars is your Ruuster brand kit as --rw-* CSS variables. Without it the recipes use the default look.
  const meta = await idx.meta();
  const themeVars = meta.ok ? meta.data.themeVars : undefined;
  // #endregion
  return (
    <html lang="en">
      <head>
        <RecipesStyles themeVars={themeVars} />
      </head>
      <body>
        {GTM_ID ? (
          <Script id="gtm" strategy="afterInteractive">
            {`(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','${GTM_ID}');`}
          </Script>
        ) : null}
        {children}
      </body>
    </html>
  );
}
```

## Step 2: import the ready container

The file below has everything GA4 needs for the four events: a constant for your measurement ID,
a Data Layer variable for each field, a Custom Event trigger for each event, a GA4 Event tag for each
event, and the Google tag. Save it as `ruuster-idx-gtm-container.json`.

<p class="code-title"><code>gtm/ruuster-idx-gtm-container.json</code></p>

```json
{
  "exportFormatVersion": 2,
  "exportTime": "2026-11-04 00:00:00",
  "containerVersion": {
    "accountId": "0",
    "containerId": "0",
    "path": "accounts/0/containers/0/versions/0",
    "containerVersionId": "0",
    "container": {
      "accountId": "0",
      "containerId": "0",
      "path": "accounts/0/containers/0",
      "name": "Ruuster Listings API recipes",
      "publicId": "GTM-XXXXXXX",
      "usageContext": [
        "WEB"
      ]
    },
    "tag": [
      {
        "accountId": "0",
        "containerId": "0",
        "tagId": "200",
        "name": "GA4 - Google tag",
        "type": "googtag",
        "parameter": [
          {
            "type": "TEMPLATE",
            "key": "tagId",
            "value": "{{GA4 Measurement ID}}"
          }
        ],
        "firingTriggerId": [
          "2147479553"
        ],
        "tagFiringOption": "ONCE_PER_EVENT"
      },
      {
        "accountId": "0",
        "containerId": "0",
        "tagId": "201",
        "name": "GA4 - view_item_list",
        "type": "gaawe",
        "parameter": [
          {
            "type": "TEMPLATE",
            "key": "eventName",
            "value": "view_item_list"
          },
          {
            "type": "TEMPLATE",
            "key": "measurementIdOverride",
            "value": "{{GA4 Measurement ID}}"
          },
          {
            "type": "BOOLEAN",
            "key": "sendEcommerceData",
            "value": "true"
          },
          {
            "type": "TEMPLATE",
            "key": "getEcommerceDataFrom",
            "value": "dataLayer"
          }
        ],
        "firingTriggerId": [
          "100"
        ],
        "tagFiringOption": "ONCE_PER_EVENT"
      },
      {
        "accountId": "0",
        "containerId": "0",
        "tagId": "202",
        "name": "GA4 - view_item",
        "type": "gaawe",
        "parameter": [
          {
            "type": "TEMPLATE",
            "key": "eventName",
            "value": "view_item"
          },
          {
            "type": "TEMPLATE",
            "key": "measurementIdOverride",
            "value": "{{GA4 Measurement ID}}"
          },
          {
            "type": "BOOLEAN",
            "key": "sendEcommerceData",
            "value": "true"
          },
          {
            "type": "TEMPLATE",
            "key": "getEcommerceDataFrom",
            "value": "dataLayer"
          }
        ],
        "firingTriggerId": [
          "101"
        ],
        "tagFiringOption": "ONCE_PER_EVENT"
      },
      {
        "accountId": "0",
        "containerId": "0",
        "tagId": "203",
        "name": "GA4 - search",
        "type": "gaawe",
        "parameter": [
          {
            "type": "TEMPLATE",
            "key": "eventName",
            "value": "search"
          },
          {
            "type": "TEMPLATE",
            "key": "measurementIdOverride",
            "value": "{{GA4 Measurement ID}}"
          },
          {
            "type": "LIST",
            "key": "eventSettingsTable",
            "list": [
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "search_term"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - search_term}}"
                  }
                ]
              }
            ]
          }
        ],
        "firingTriggerId": [
          "102"
        ],
        "tagFiringOption": "ONCE_PER_EVENT"
      },
      {
        "accountId": "0",
        "containerId": "0",
        "tagId": "204",
        "name": "GA4 - generate_lead",
        "type": "gaawe",
        "parameter": [
          {
            "type": "TEMPLATE",
            "key": "eventName",
            "value": "generate_lead"
          },
          {
            "type": "TEMPLATE",
            "key": "measurementIdOverride",
            "value": "{{GA4 Measurement ID}}"
          },
          {
            "type": "LIST",
            "key": "eventSettingsTable",
            "list": [
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "lead_source"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - lead_source}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "form_name"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - form_name}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "listing_id"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - listing_id}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "listing_name"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - listing_name}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "listing_price"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - listing_price}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "utm_source"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - utm_source}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "utm_medium"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - utm_medium}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "utm_campaign"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - utm_campaign}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "utm_term"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - utm_term}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "utm_content"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - utm_content}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "gclid"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - gclid}}"
                  }
                ]
              },
              {
                "type": "MAP",
                "map": [
                  {
                    "type": "TEMPLATE",
                    "key": "parameter",
                    "value": "fbclid"
                  },
                  {
                    "type": "TEMPLATE",
                    "key": "parameterValue",
                    "value": "{{DLV - fbclid}}"
                  }
                ]
              }
            ]
          }
        ],
        "firingTriggerId": [
          "103"
        ],
        "tagFiringOption": "ONCE_PER_EVENT"
      }
    ],
    "trigger": [
      {
        "accountId": "0",
        "containerId": "0",
        "triggerId": "100",
        "name": "CE - view_item_list",
        "type": "CUSTOM_EVENT",
        "customEventFilter": [
          {
            "type": "EQUALS",
            "parameter": [
              {
                "type": "TEMPLATE",
                "key": "arg0",
                "value": "{{_event}}"
              },
              {
                "type": "TEMPLATE",
                "key": "arg1",
                "value": "view_item_list"
              }
            ]
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "triggerId": "101",
        "name": "CE - view_item",
        "type": "CUSTOM_EVENT",
        "customEventFilter": [
          {
            "type": "EQUALS",
            "parameter": [
              {
                "type": "TEMPLATE",
                "key": "arg0",
                "value": "{{_event}}"
              },
              {
                "type": "TEMPLATE",
                "key": "arg1",
                "value": "view_item"
              }
            ]
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "triggerId": "102",
        "name": "CE - search",
        "type": "CUSTOM_EVENT",
        "customEventFilter": [
          {
            "type": "EQUALS",
            "parameter": [
              {
                "type": "TEMPLATE",
                "key": "arg0",
                "value": "{{_event}}"
              },
              {
                "type": "TEMPLATE",
                "key": "arg1",
                "value": "search"
              }
            ]
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "triggerId": "103",
        "name": "CE - generate_lead",
        "type": "CUSTOM_EVENT",
        "customEventFilter": [
          {
            "type": "EQUALS",
            "parameter": [
              {
                "type": "TEMPLATE",
                "key": "arg0",
                "value": "{{_event}}"
              },
              {
                "type": "TEMPLATE",
                "key": "arg1",
                "value": "generate_lead"
              }
            ]
          }
        ]
      }
    ],
    "variable": [
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "1",
        "name": "GA4 Measurement ID",
        "type": "c",
        "parameter": [
          {
            "type": "TEMPLATE",
            "key": "value",
            "value": "G-XXXXXXXXXX"
          }
        ],
        "notes": "Replace G-XXXXXXXXXX with your GA4 measurement ID (GA4 Admin, Data streams, your web stream)."
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "2",
        "name": "DLV - search_term",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "search_term"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "3",
        "name": "DLV - lead_source",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "lead_source"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "4",
        "name": "DLV - form_name",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "form_name"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "5",
        "name": "DLV - listing_id",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "listing_id"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "6",
        "name": "DLV - listing_name",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "listing_name"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "7",
        "name": "DLV - listing_price",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "listing_price"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "8",
        "name": "DLV - utm_source",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "utm_source"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "9",
        "name": "DLV - utm_medium",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "utm_medium"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "10",
        "name": "DLV - utm_campaign",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "utm_campaign"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "11",
        "name": "DLV - utm_term",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "utm_term"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "12",
        "name": "DLV - utm_content",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "utm_content"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "13",
        "name": "DLV - gclid",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "gclid"
          }
        ]
      },
      {
        "accountId": "0",
        "containerId": "0",
        "variableId": "14",
        "name": "DLV - fbclid",
        "type": "v",
        "parameter": [
          {
            "type": "INTEGER",
            "key": "dataLayerVersion",
            "value": "2"
          },
          {
            "type": "BOOLEAN",
            "key": "setDefaultValue",
            "value": "false"
          },
          {
            "type": "TEMPLATE",
            "key": "name",
            "value": "fbclid"
          }
        ]
      }
    ],
    "builtInVariable": [
      {
        "accountId": "0",
        "containerId": "0",
        "type": "EVENT",
        "name": "Event"
      }
    ]
  }
}
```

1. Open [Google Tag Manager](https://tagmanager.google.com) and pick your container.
2. Go to **Admin**, then **Import Container**.
3. Under **Select file to import**, choose `ruuster-idx-gtm-container.json`.
4. Under **Choose workspace**, pick **Existing** and your **Default Workspace**.
5. Under **Choose an import option**, pick **Merge**, then **Rename conflicting tags, triggers, and
   variables**. Your own tags stay as they are.
6. Select **Confirm**.

## Step 3: add your GA4 measurement ID

1. In GA4, go to **Admin**, **Data streams**, pick your web stream, and copy the **Measurement ID**
   (it starts with `G-`).
2. In Google Tag Manager, go to **Variables**, open **GA4 Measurement ID**, replace `G-XXXXXXXXXX`
   with your ID, and save.
3. If your container already has a Google tag for the same measurement ID, pause **GA4 - Google tag**
   so the page view is counted once.

## Step 4: test before you publish

1. Select **Preview** in Google Tag Manager and enter your site's address. Tag Assistant opens your
   site in a new tab.
2. Search for a place with the search box. Tag Assistant shows a `search` event and the
   **GA4 - search** tag firing.
3. Open a search results page and a listing page. You see `view_item_list` and `view_item`, each with
   its items.
4. Send a test lead from a tour form with your own name and email and `?utm_source=test` on the page
   address. You see `generate_lead` with `utm_source` set to `test`, and the lead arrives in your CRM.
5. In GA4, open **Admin**, then **DebugView**, and check that the four events arrive.

## Step 5: publish and set up GA4

1. Select **Submit**, then **Publish** in Google Tag Manager.
2. In GA4, go to **Admin**, **Events**, and mark `generate_lead` as a key event, so leads count as
   conversions.
3. To report on the lead fields, go to **Admin**, **Custom definitions**, and create an event-scoped
   custom dimension for each one you want, such as `lead_source` and `listing_id`.

## The same campaign ids in your lead route

The forms send each lead to your own route as a `LeadSubmission`, and its `attribution` holds the same
campaign ids that `generate_lead` carries: `utmSource`, `utmMedium`, `utmCampaign`, `utmTerm`,
`utmContent`, `gclid` and `fbclid`. The forms read them from the address of the first page the visitor
opened, and keep them for the rest of the visit, so a lead sent three pages later still has them.

In `lib/crm.ts` your route reads them from `lead.attribution`. The Follow Up Boss, Sierra Interactive and Sure
Send routes on [Send leads and activity to your CRM](https://help.ruuster.com/idx/crm-overview) send
the person, the request and the listing to your CRM. To match a lead with its campaign, log
`lead.attribution` in your route next to the CRM's answer, and compare it with the
`generate_lead` conversions in GA4 and in your ad accounts.

## Your own events

For pages you build yourself, push the same events with `pushDataLayer` or `Ga4Event` from
`@ruuster/idx-recipes`. [Recipe components](https://help.ruuster.com/idx/recipe-components)
describes both.
