Skip to content

Commit

Permalink
Change matomo site search tracking to use trackSiteSearch (#1772)
Browse files Browse the repository at this point in the history
Changed matomo site search tracking to use trackSiteSearch
  • Loading branch information
Sandravaphilips authored Mar 18, 2024
1 parent d362a9e commit a1c56e1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
28 changes: 23 additions & 5 deletions peachjam/js/components/FindDocuments/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ export default {
// load state from URL
const params = new URLSearchParams(window.location.search);
// skip the first event if there's a query, because the page load will already have sent it
this.q = (params.get('q') || '').trim();
this.q = params.get('q') || '';
this.page = parseInt(params.get('page')) || this.page;
this.ordering = params.get('ordering') || this.ordering;
Expand Down Expand Up @@ -622,27 +622,28 @@ export default {
scrollToElement(this.$refs['search-box']);
try {
const params = this.generateSearchParams().toString();
const url = `/search/api/documents/?${params}`;
const params = this.generateSearchParams();
const url = `/search/api/documents/?${params.toString()}`;
if (pushState) {
window.history.pushState(
null,
'',
document.location.pathname + '?' + this.serialiseState()
);
analytics.trackPageView();
}
const response = await fetch(url);
// check that the search state hasn't changed since we sent the request
if (params === this.generateSearchParams().toString()) {
if (params.toString() === this.generateSearchParams().toString()) {
if (response.ok) {
this.error = null;
this.searchInfo = await response.json();
if (this.searchInfo.count === 0) {
this.clearAllFilters();
}
this.formatFacets();
this.trackSearch(params);
} else {
this.error = response.statusText;
}
Expand All @@ -656,6 +657,23 @@ export default {
}
},
trackSearch (params) {
const keywords = [];
const facets = [];
const fields = this.facets.map(facet => facet.name).concat(['date__range', 'date__gte', 'date__lte']);
[...new Set(params.keys())].forEach((key) => {
if (key.startsWith('search')) {
const s = key === 'search' ? '' : (key.substring(8) + '=');
keywords.push(s + params.get(key).trim());
} else if (fields.includes(key)) {
facets.push(`${key}=${params.getAll(key).join(',')}`);
}
});
analytics.trackSiteSearch(keywords.join('; '), facets.join('; '), this.searchInfo.count);
},
async explain (item) {
const params = this.generateSearchParams();
params.set('index', item._index);
Expand Down
5 changes: 5 additions & 0 deletions peachjam/js/components/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export class Analytics {
this.paq.push(['trackPageView']);
this.gtag('event', 'page_view');
}

trackSiteSearch (keyword: string, category: string, searchCount: number) {
this.paq.push(['trackSiteSearch', keyword, category, searchCount]);
this.gtag('event', 'site_search', { keyword, category, searchCount });
}
}

const analytics = new Analytics();
Expand Down

0 comments on commit a1c56e1

Please sign in to comment.