Don't have one? Create one ↗
← All courses

Lesson 2 of 4

Site Search

① Connect your GTM container

Paste your container ID to load it into this page. It only ever runs here.

Advanced: use a specific environment

Site search is one of the most useful events to track, because it tells you in the visitors own words what they came looking for. GA4 has a built-in view_search_results event for exactly this, and the search_term parameter is what fills the Search terms report.

The form below pushes view_search_results with the query as search_term. Your job is a Custom Event trigger that listens for that event and a GA4 Event tag that forwards search_term along with it.

Goal

Catch view_search_results and fire a GA4 Event tag that maps the search_term parameter.

Build it in GTM

  1. Capture the search term as a variable

    The page pushes { event: "view_search_results", search_term }. In Variables, under User-Defined Variables, click New, choose Data Layer Variable, set the Data Layer Variable Name to search_term, name it DLV - search_term and Save.
  2. Create the Custom Event trigger

    Go to Triggers → New and choose Custom Event. In Event name type view_search_results, matching the pushed string exactly, so the trigger fires on each search.
  3. Fire a GA4 Event tag that maps search_term

    Create a GA4 Event tag with Event Name view_search_results. Under Event Parameters, add a parameter named search_term with value {{DLV - search_term}}, then fire it on your view_search_results trigger. For practice you can swap in a Custom HTML tag named Custom HTML - Test with <script></script>, but the GA4 Event tag is the real-site choice.

Debug in Tag Assistant

Copy this lesson's live URL and paste it into GTM Preview, that is the page Tag Assistant connects to. It has the clickable elements, so this page stays clean for reading.

  1. In your GTM, click Preview.
  2. Paste the live URL above and click Connect.
  3. Interact with the live page and watch your tag fire in Tag Assistant.

What you should expect to see

Run a search on the live page. In Tag Assistant a view_search_results event appears in the left rail, your tag shows under Tags Fired, and the outgoing hit carries search_term set to whatever you typed.

Verify your container

Built it? Export your container, Admin → Export Container, choose your workspace, then drop the JSON here to check it against this lesson.

Drop your container .json here

or browse · checked in your browser, nothing is uploaded

0 of 4 lessons complete