Matomo analytics tracking integration

Integrating with Matomo analytics

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.

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.

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.

Last updated

Copyright Ernest Marcinko