Skip to content

Commit

Permalink
feat: use trackSiteSearch of matomo with search results count
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Jun 13, 2024
1 parent f00ab59 commit 737202a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Resources/app/storefront/src/main.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
window.PluginManager.register('MatomoAnalytics', () => import('./plugin/MatomoAnalyticsPlugin'));

window.PluginManager.override('SearchWidget', () => import('./plugin/override/header/SearchWidgetPluginExtension'), '[data-search-widget]');
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import SearchWidgetPlugin from 'src/plugin/header/search-widget.plugin';

export default class SearchWidgetPluginExtension extends SearchWidgetPlugin {
init() {
super.init();

this.$emitter.subscribe('afterSuggest', event => {
let term = null;
let count = false;

const dataField = document.getElementsByClassName('matomo-search-suggest-helper')[0];
if (dataField) {
const value = JSON.parse(dataField.dataset['value']);
term = value['term'];
count = value['count'];
}

window._paq = window._paq || [];
window._paq.push(['trackSiteSearch',
term,
'suggestSearch',
count
]);
});
}
}
10 changes: 9 additions & 1 deletion src/Resources/views/storefront/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
{{ page.order.shippingTotal }},
false
]);
{% elseif activeRoute is same as('frontend.search.page') %}
window._paq.push(['trackSiteSearch',
'{{ page.searchTerm|escape('js') }}',
'searchPage',
{{ page.listing.total }}
]);
{% endif %}
{% set phpTrackingPath = config('TinectMatomo.config.phpTrackingPath') %}
Expand All @@ -66,7 +72,9 @@
if (!window.matomoCookieActive) {
window._paq.push(['disableCookies']);
}
window._paq.push(['trackPageView']);
{% if activeRoute is not same as('frontend.search.page') %}
window._paq.push(['trackPageView']);
{% endif %}
window._paq.push(['enableLinkTracking']);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% sw_extends "@Storefront/storefront/layout/header/search-suggest.html.twig" %}

{% block layout_search_suggest_result_total %}
{{ parent() }}

{% set matomoHelperData = {
term: page.searchTerm,
count: page.searchResult.total
} %}
<div class="matomo-search-suggest-helper" data-value="{{ matomoHelperData|json_encode }}"></div>
{% endblock %}

0 comments on commit 737202a

Please sign in to comment.