Commit 16a0ac69 authored by Tim Zallmann's avatar Tim Zallmann

Merge branch '241055-snowplow-for-search-autocomplete-suggestions' into 'master'

Add snowplow events for clicking search suggestions

Closes #241055

See merge request gitlab-org/gitlab!40822
parents 7c74a2f0 193b1d27
import { slugify } from '~/lib/utils/text_utility';
const renderersByType = {
divider(element) {
element.classList.add('divider');
......@@ -95,15 +97,22 @@ function checkSelected(data, options) {
return options.parent.querySelector(`input[name='${options.fieldName}']`) == null;
}
function createLink(url, selected, options) {
function createLink(data, selected, options, index) {
const link = document.createElement('a');
link.href = url;
link.href = getPropertyWithDefault(data, options, 'url', '#');
if (options.icon) {
link.classList.add('d-flex', 'align-items-center');
}
if (options.trackSuggestionClickedLabel) {
link.setAttribute('data-track-event', 'click_text');
link.setAttribute('data-track-label', options.trackSuggestionClickedLabel);
link.setAttribute('data-track-value', index);
link.setAttribute('data-track-property', slugify(data.category || 'no-category'));
}
link.classList.toggle('is-active', selected);
return link;
......@@ -123,8 +132,7 @@ function assignTextToLink(el, data, options) {
function renderLink(row, data, { options, group, index }) {
const selected = checkSelected(data, options);
const url = getPropertyWithDefault(data, options, 'url', '#');
const link = createLink(url, selected, options);
const link = createLink(data, selected, options, index);
assignTextToLink(link, data, options);
......
......@@ -135,6 +135,7 @@ export class SearchAutocomplete {
data: this.getData.bind(this),
selectable: true,
clicked: this.onClick.bind(this),
trackSuggestionClickedLabel: 'search_autocomplete_suggestion',
});
}
......
......@@ -313,6 +313,42 @@ describe('deprecatedJQueryDropdown', () => {
expect(li.childNodes.length).toEqual(1);
expect(li.textContent).toEqual(text);
});
describe('with a trackSuggestionsClickedLabel', () => {
it('it includes data-track attributes', () => {
const dropdown = dropdownWithOptions({
trackSuggestionClickedLabel: 'some_value_for_label',
});
const item = {
id: 'some-element-id',
text: 'the link text',
url: 'http://example.com',
category: 'Suggestion category',
};
const li = dropdown.renderItem(item, null, 3);
const link = li.querySelector('a');
expect(link).toHaveAttr('data-track-event', 'click_text');
expect(link).toHaveAttr('data-track-label', 'some_value_for_label');
expect(link).toHaveAttr('data-track-value', '3');
expect(link).toHaveAttr('data-track-property', 'suggestion-category');
});
it('it defaults property to no_category when category not provided', () => {
const dropdown = dropdownWithOptions({
trackSuggestionClickedLabel: 'some_value_for_label',
});
const item = {
id: 'some-element-id',
text: 'the link text',
url: 'http://example.com',
};
const li = dropdown.renderItem(item);
const link = li.querySelector('a');
expect(link).toHaveAttr('data-track-property', 'no-category');
});
});
});
it('should keep selected item after selecting a second time', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment