Skip to content

Commit

Permalink
Merge pull request #3835 from 10up/fix/autosuggest-epio-ga
Browse files Browse the repository at this point in the history
Autosuggest - Google Analytics and EP.io
  • Loading branch information
felipeelia authored Feb 15, 2024
2 parents 38f2105 + cfd5b68 commit 114be13
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
20 changes: 14 additions & 6 deletions assets/js/autosuggest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,22 @@ function triggerAutosuggestEvent(detail) {
const event = new CustomEvent('ep-autosuggest-click', { detail });
window.dispatchEvent(event);

if (
detail.searchTerm &&
parseInt(epas.triggerAnalytics, 10) === 1 &&
typeof gtag === 'function'
) {
/**
* 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 (detail.searchTerm && parseInt(epas.triggerAnalytics, 10) === 1 && epGtag) {
const action = `click - ${detail.searchTerm}`;
// eslint-disable-next-line no-undef
gtag('event', action, {
epGtag('event', action, {
event_category: 'EP :: Autosuggest',
event_label: detail.url,
transport_type: 'beacon',
Expand Down
20 changes: 11 additions & 9 deletions includes/classes/Feature/Autosuggest/Autosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -901,15 +901,17 @@ protected function set_settings_schema() {

$this->maybe_add_epio_settings_schema();

$set_in_wp_config = defined( 'EP_AUTOSUGGEST_ENDPOINT' ) && EP_AUTOSUGGEST_ENDPOINT;

$this->settings_schema[] = [
'disabled' => $set_in_wp_config,
'help' => $set_in_wp_config ? __( 'This address will be exposed to the public.', 'elasticpress' ) : '',
'key' => 'endpoint_url',
'label' => __( 'Endpoint URL', 'elasticpress' ),
'type' => 'url',
];
if ( ! Utils\is_epio() ) {
$set_in_wp_config = defined( 'EP_AUTOSUGGEST_ENDPOINT' ) && EP_AUTOSUGGEST_ENDPOINT;

$this->settings_schema[] = [
'disabled' => $set_in_wp_config,
'help' => $set_in_wp_config ? __( 'This address will be exposed to the public.', 'elasticpress' ) : '',
'key' => 'endpoint_url',
'label' => __( 'Endpoint URL', 'elasticpress' ),
'type' => 'url',
];
}
}

/**
Expand Down

0 comments on commit 114be13

Please sign in to comment.