# Track Searches With No Results in GA4

> Sends a search_no_results event with the search term to Google Analytics 4 when a site search finds nothing, showing you what visitors want but cannot find.

- Type: PHP
- Runs: Frontend only
- Category: [Analytics & Tracking](https://wpide.com/library/category/analytics/)
- Tags: google-analytics, ga4, search, content
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/analytics-ga4-search-no-results/

## Code

```php
add_action( 'wp_footer', function () {
    global $wp_query;

    if ( ! is_search() || $wp_query->found_posts > 0 ) {
        return;
    }
    ?>
<script>
if (typeof gtag === 'function') {
    gtag('event', 'search_no_results', { search_term: <?php echo wp_json_encode( mb_substr( get_search_query( false ), 0, 100 ) ); ?> });
}
</script>
    <?php
} );
```

## Notes

Needs the GA4 tag on the page. Register search_term as an event-scoped custom dimension to report on terms.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Track Searches With No Results in GA4" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
