Follow me on LinkedIn - AI, GA4, BigQuery

Through the GA4 site search tracking (also known as ‘Internal Site Search Tracking’) feature, you can determine the search terms used by your website visitors to find information on your website:

website search

Once you have implemented site search tracking in your GA4 property, you can see the list of keywords (called ‘Search terms’) used by your website visitors to find information on your website:

search terms

The ‘view_search_results’ event

Once you have enabled/setup site search tracking in your GA4 property, whenever a user inputs a query and clicks on the search button on your website, GA4 fires the ‘view_search_results’ event.

You can see the ‘view_search_results’ event in your GA4 property via the ‘Events’ report:

the ‘view search results event

You should check how the ‘view_search_results’ events are being recorded in the GA4 BigQuery data table and what event parameters are being passed along with this event type.

Check how the ‘view search results events are being recorded

Take a close look at the various event parameters which are sent along with the ‘view_search_results’ events:

  1. search_term
  2. ignore_referrer
  3. engagement_time_msec
  4. session_engaged
  5. ga_session_id
  6. page_referrer
  7. engaged_session_event
  8. unique_search_term
  9. content_group
  10. page_location
  11. batch_ordering_id
  12. batch_page_id
  13. ga_session_number
  14. page_title

While creating a text prompt, we will refer to these event parameters (esp., ‘search_term’).

Creating Site Search Tracking report in GA4 BigQuery.

Here is how you can create an ‘Internal Site Search’ report in GA4 BigQuery to track what users are searching on your website.

Internal Site Search Tracking report

Step-1: Make sure that the ‘Site Search’ tracking is set up and that you have collected at least a couple of days of ‘view_search_results’ event data in your GA4 property and BigQuery data set.

Step-2: Navigate to your GA4 BigQuery data table and run a query to check how the ‘view_search_results’ events are being recorded and what event parameters are being passed along with this event type. This will give you a good idea of what site search term data to extract.

Step-3: Calculate the values of the ‘Search Term’ column by extracting the values from the ‘search_term’ event parameter for each ‘event_name’ top-level field where ‘event_name’ is ‘view_search_results’.

Step-4: Calculate the values of the ‘Event Count’ column by counting the total number of ‘view_search_results’ events for each ‘search_term’ event parameter.

Step-5: Sort the table in descending order for ‘Event Count’.

Step-6: The top row of the data table should show ‘Total’ under the ‘Search Term’ column and total number of ‘Event Count’ across all ‘search_term’ under the ‘Event Count’ column.

Once you understand the logic, creating site search tracking report is easy.

The SQL below is automatically generated via a text prompt in ChatGPT.

-- Internal Site Search Tracking.

WITH
  search_data AS (
    SELECT
      COALESCE(
        (
          SELECT ep.value.string_value
          FROM UNNEST(event_params) AS ep
          WHERE ep.key = 'search_term'
        ),
        '(not set)') AS search_term,
      COUNT(*) AS event_count
    FROM
      `dbrt-ga4.analytics_207472454.events_*`
    WHERE
      _TABLE_SUFFIX BETWEEN '20241001' AND '20241031'
      AND event_name = 'view_search_results'
    GROUP BY
      search_term
  ),
  total_row AS (
    SELECT
      'Total' AS search_term,
      SUM(event_count) AS event_count
    FROM
      search_data
  )
SELECT *
FROM total_row
UNION ALL
SELECT *
FROM search_data
ORDER BY
  event_count DESC;

If you want to learn to automate SQL generation via text prompts in ChatGPT, enrol in my GA4 BigQuery course.

Instead of teaching you SQL, I teach you to create effective text prompts in chatGPT, which will automatically create your desired SQL.

The focus is entirely on teaching the underlying logic (instead of the actual SQL). So that you can scale across ‘N’ SQL use cases and generate consistent outputs repeatedly with 100% accuracy.

[No prior knowledge of BigQuery or SQL required.]

  1. GA4 BigQuery Attribution Tutorial.
  2. How to backfill GA4 data in BigQuery.
  3. How to send data from Google Search Console to BigQuery.
  4. Google Advanced Consent Mode and GA4 BigQuery Export.
  5. Google Analytics 4 BigQuery Tutorial for Beginners to Advanced.
  6. Prompt Engineering for GA4 BigQuery SQL Generation.
  7. How to create a new BigQuery project.
  8. How to create a new Google Cloud Platform account.
  9. How to overcome GA4 BigQuery Export limit.
  10. BigQuery Cost Optimization Best Practices.
  11. event_timestamp vs user_first_touch_timestamp GA4 BigQuery.
  12. GA4 BigQuery Video Tracking Report.
  13. Counting GA4 Sessions in BigQuery? Watch for These Caveats.
  14. Calculating User Paths in GA4 BigQuery.
  15. Calculating Conversion Paths in GA4 BigQuery.
  16. Don’t Aggregate Unconsented Events in GA4 BigQuery.
  17. How to track file downloads in GA4 BigQuery.
  18. How to create GA4 Content Group Report in BigQuery.
  19. How to create GA4 Site Search Tracking report in BigQuery.
  20. How to track outbound / external links in GA4 BigQuery data table.