Skip to content

Commit

Permalink
Merge pull request #4032 from 10up/fix/issue-3925
Browse files Browse the repository at this point in the history
Refactor of triggerAutosuggestEvent with adjustments for the dataLayer.push() call
  • Loading branch information
felipeelia authored Dec 10, 2024
2 parents 26e42e2 + 08398ef commit 9b9ad02
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions assets/js/autosuggest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,42 @@ function triggerAutosuggestEvent(detail) {
const event = new CustomEvent('ep-autosuggest-click', { detail });
window.dispatchEvent(event);

/**
* Check if window.gtag was already defined, otherwise
* try to use window.dataLayer.push, available by default
* for Tag Manager users.
*/
let epGtag = null;
if (typeof window?.gtag === 'function') {
epGtag = window.gtag;
} else if (typeof window?.dataLayer?.push === 'function') {
epGtag = window.dataLayer.push;
if (parseInt(epas.triggerAnalytics, 10) !== 1) {
return;
}

if (detail.searchTerm && parseInt(epas.triggerAnalytics, 10) === 1 && epGtag) {
const action = `click - ${detail.searchTerm}`;
// eslint-disable-next-line no-undef
epGtag('event', action, {
event_category: 'EP :: Autosuggest',
event_label: detail.url,
transport_type: 'beacon',
});
if (!detail.searchTerm) {
return;
}

const action = `click - ${detail.searchTerm}`;
try {
/**
* Check if window.gtag was already defined, otherwise
* try to use window.dataLayer.push, available by default
* for Tag Manager users.
*/
if (typeof window?.gtag === 'function') {
window.gtag('event', action, {
ep_autosuggest_search_term: detail.searchTerm,
ep_autosuggest_clicked_url: detail.url,
event_category: 'EP :: Autosuggest',
event_label: detail.url,
transport_type: 'beacon',
});
return;
}
if (typeof window?.dataLayer?.push === 'function') {
window.dataLayer.push({
event: 'ep_autosuggest_click',
ep_autosuggest_search_term: detail.searchTerm,
ep_autosuggest_clicked_url: detail.url,
event_category: 'EP :: Autosuggest',
event_label: detail.url,
});
}
} catch (error) {
// When Ad blocks are enabled, the call above will fail
}
}

Expand Down

0 comments on commit 9b9ad02

Please sign in to comment.