This article can also be considered part of a series of experiences with Grav. However, the main focus I’ll be discussing is the process of tracking and analyzing site-wide searches on content websites.
Since I’ve noticed a frequent lack of knowledge regarding this topic among the brands I advise, I believe it would be beneficial to elaborate further. Let’s first clarify the meaning of “site search.”
You can find support requests related to Google Analytics here.
First, let’s recall how a URI is composed of its various components[^1].
foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment
| _____________________|__
/ \ / \
urn:example:animal:ferret:nose
Site Search
The process of searching content within a website or application. For example, if you have a content website powered by WordPress, site search queries will appear in the URL as ?s=query unless you’ve made changes to the search method. Below you can see an example form that generates such a query[^2] [^3].
<form action="/" method="get">
<label for="search">Search in <?php echo home_url( '/' ); ?></label>
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
<input type="submit" id="searchsubmit" value="<?php echo esc_attr_x( 'Search', 'submit button' ); ?>" />
</form>
In this case, our site-wide search parameter will be s. However, this is not mandatory. In different websites or applications, parameters such as q, search, term, and many others are possible. On the other hand, we can observe that the query may also be embedded in the URL path instead of the parameter—for example, instead of .../?s=search, it could be .../search/search. Let’s first examine how this can be implemented on the WordPress side.
function wp_search_url() {
if (is_search() && ! empty( $_GET['s'])) {
wp_redirect(home_url('/search/') . urlencode( get_query_var('s')));
exit();
}
}
add_action( 'template_redirect', 'wp_search_url' );
The above code matches the s parameter found in the URL to the /search/ path. In another use case, both a path and a parameter may coexist—for example, /search?q=search. In this context, I’m referring to an example from the Grav side: search/query:sorgu. As can be seen, the path uses search, followed by query with the search value taken not by = (equal sign), but by : (colon). Although these usages are defined within the gen-delims, they can be overlooked due to the fact that ? is assumed to be a delimiter. Unfortunately, Google Analytics fails to capture search terms when a colon is used to separate them. As an alternative solution, Google Analytics View settings can interpret the entire subsequent statement as if it were the search query itself, using a sitemap definition. So, what are the other possible solutions?
Virtual Page
The query can be rewritten using JavaScript and then sent to Google Analytics as if it were a page view. This approach can also be preferred when the search operation is performed via a POST method[^5].
const newURISepSign = document.location.pathname.replace('query:', '?query=');
// Usage with ga
ga('set', 'page', newURISepSign);
ga('send', 'pageview');
// Usage with gtag
gtag('config', 'UA-XXXXX-Y', {
'page_title': 'Page Title', // optional
'page_path': newURISep+Sign
});
### Filter
**Google Analytics** also provides us with the ability to manipulate data through filters. Let's now see how we can use this feature for a query setup.
Let our URL be: `https://alanadi.com/tr/search/query:sorgu`. For us, the important and editable part is `query:sorgu`. Due to the `/search/` structure, it can be designated as a category. Therefore, no additional processing is required. From within `query:sorgu`, we can capture the `sorgu` definition using [regex](../regex-regular-expression) in the "A Field -> A Output" section as the "Request URI" with the pattern `\/query:(\S+)$`. This way, the query that follows after `query:` will now be available as the `$A1` value. For the "B Field -> B Output" field, we can simply use a hyphen `-`. Once we have the query, we can pass the value `$A1` as the "Search Term" in the "Output Target -> Creator" field.
<ResponsiveImage
src="google-analytics-arama-terimi-filtre"
alt="Google Analytics - Search Term - Filter Setup"
uri="google-analytics-site-arama-izlemesi"
/>
So far, we discussed the **Site Search** activation before the URL structure. Now we can move on to see how Google Analytics defines the Site Search feature.
**Site Search** is presented as a **View** feature. However, if you wish to filter within searches, you can perform operations at the property level—covering all Views—through the **Excluded Search Terms List**. To enable the **Site Search** feature under a specific **View**, you must click the **View Settings** link under the **Administrator** section for that particular View. Then, you will be able to see the **Site Search Settings** header on the opened page.
<ResponsiveImage
src="google-analytics-site-aramasi"
alt="Google Analytics Site Search"
uri="google-analytics-site-arama-izlemesi"
/>
You must enable the **Site Search Tracking** feature in order to utilize the Site Search functionality and monitor the searches users perform on your website or app. After that, you can begin filling in the relevant fields.
The **query parameter** refers to parameters such as `?s=`, `?query=`, and similar `query` definitions as mentioned in the introduction section. Therefore, you must enter the parameter you're using—such as `s` or `query`—without any additional characters. You may provide up to five parameters, separated by commas, in this field.
**Site search categories** enable tracking of the relevant _path_ when the search operation is split by _path_.
<ResponsiveImage
src="google-analytics-site-arama-raporu"
alt="Google Analytics Site Search"
uri="google-analytics-site-arama-izlemesi"
/>
Finally, if you need the **query parameter** and **site search categories**, they can be extracted from the URLs in your reports. Select the export option available under the options section, and this will be sufficient. This selection does not prevent tracking of searches; it only ensures that the relevant definitions do not appear within the URL[^5]. After these actions, you can track searches performed by users through the **Google Analytics** `Behavior > Site Search` reports. The relevant search queries can be accessed via the [API](../api-nedir) using the `ga:searchKeyword` and `ga:searchCategory` dimensions[^6].
## Gaining Content Ideas from Site Search
Now, we can track searches performed on our website or app. This enables us to gain insights into topics of interest to our users, refine our existing content based on relevant search queries, or use these as ideas for new content. **Google Analytics** search queries and user-related information provide valuable insights in many of its reports. This allows us to differentiate between users who perform searches and those who don't, and to review our content in the context of page-search queries, including the pages searched, the target page, and the search terms.
[^1]: [Uniform Resource Identifier (URI): Generic Syntax](https://tools.ietf.org/html/rfc3986#section-3)
[^2]: [Code Reference, _get_search_form_, WordPress.org](https://developer.wordpress.org/reference/functions/get_search_form/?target=_blank&rel=noopener,noreferrer)
[^3]: [Code Reference, Creating a Search Page, WordPress.org](https://codex.wordpress.org/Creating_a_Search_Page?target=_blank&rel=noopener,noreferrer)
[^4]: [Reserved Characters](https://tools.ietf.org/html/rfc3986?target=_blank&rel=noopener,noreferrer#section-2.2)
[^5]: [Configure Site Search, Analytics Help](https://support.google.com/analytics/answer/1012264?target=_blank&rel=noopener,noreferrer)
[^6]: [Site Search - Search Terms, Core Reporting API, Google Analytics](https://developers.google.com/analytics/devguides/reporting/core/v3/common-queries?target=_blank&rel=noopener,noreferrer#site-search---search-terms)