Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change matomo site search tracking to use trackSiteSearch #1772

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 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,25 @@ export default {
}
},

trackSearch (params) {
const keywords = [];
const facets = [];
const fields = this.facets.map(facet => facet.name).concat(['date__range', 'date__gte', 'date__lte']);

let currentKey = '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better way of doing this is iterating over the keys only, not the key/value pairs. Then you don't need to de-dup like this.

params.forEach((value, key) => {
if (key.startsWith('search')) {
const s = key === 'search' ? '' : (key.substring(8) + '=');
keywords.push(s + value.trim());
} else if (fields.includes(key) && key !== currentKey) {
currentKey = 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
Loading