> For the complete documentation index, see [llms.txt](https://knowledgebase.ajaxsearchpro.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://knowledgebase.ajaxsearchpro.com/miscellaneous/other/matomo-analytics-tracking-integration.md).

# Matomo analytics tracking integration

While Matomo is not supported natively, via a few lines of custom code it is possible to enable either site search or events search tracking.

### Events integration

As per their documentation Matomo identifies and supports the Google Analytics dataLayer variable, so if the datalayer variable exists, Matomo will automatically convert the events.

To do that, we recommend a small custom code snippet for the header.

```php
add_action('wp_head', function(){
	?>
	<script>window.dataLayer = window.dataLayer || [];</script>
	<?php
}, 0);
```

After that Ajax Search Pro will detect the existing `dataLayer` and push all events to it, which Matomo should detect.

### Site Search Trigger on live search queries

If you want to track live search queries as site searches (as if the results page was viewed), then use the custom code snippet below.

```php
add_action('wp_footer', 'asp_matomo_site_search', 9999);
function asp_matomo_site_search() {
	?>
	<script>
	document.querySelectorAll(".asp_main_container").forEach(function(el){
	  el.addEventListener("asp_search_end", function(event) { 
		// event.detail carries the arguments
		const [id, instance, phrase] = event.detail;
		if ( phrase != '' && typeof _paq != 'undefined' ) {
			_paq.push(['setCustomUrl', location.origin + '/?s=' + phrase]);
			_paq.push(['trackPageView']);
		}
	  });
	});
	</script>
	<?php
}
```

After this the live search queries should be visible in the realtime window.

![](/files/A9gyQnsseISkkEYUgxxd)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://knowledgebase.ajaxsearchpro.com/miscellaneous/other/matomo-analytics-tracking-integration.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
