Problem/Motivation

The google_tag Search API integration (\Drupal\google_tag\EventSubscriber\SearchApiSubscriber) listens to SearchApiEvents::PROCESSING_RESULTS and pushes a GA4 search event to the data layer every time the event fires:

public function onSearch(ProcessingResultsEvent $event) {
  $keys = $event->getResults()->getQuery()->getKeys();
  if ($keys !== NULL) {
    ...
    $this->collector->addEvent('search', [
      'search_term' => implode(' ', $keys),
    ]);
  }
}

EventCollector::addEvent() always appends to its events array:

public function addEvent(string $name, array $contexts = []): void {
  $event = $this->createEventInstance($name, $contexts);
  if ($event === NULL) {
    return;
  }
  $this->events[] = $event;
}

But, a single Drupal Views page can render several displays (e.g. a default page display plus multiple attachment block displays) that each execute their own query against the same Search API index with the same search keys. Each of those queries dispatches SearchApiEvents::PROCESSING_RESULTS again, so onSearch() runs once per display, and the search GA4 event gets pushed to the data layer once per display, not once per actual user search.

This was causing the `search` event, on one of our websites, to be fired 5 times per page request, consecutively , and all with the exact same parameters :p

Steps to reproduce

  1. Install and configure google_tag with the search event enabled.
  2. Create a Search API View with a default page display, and add one or more additional displays (attachment or block) to the same View that render together on the same page.
  3. Perform a search and inspect the dataLayer array in the browser.
  4. The same search event is pushed multipled times (once per display), instead of once per user search.

Proposed resolution

Guard SearchApiSubscriber::onSearch() so it only pushes the search event once per request, regardless of how many displays for the same search view are in the page.

Remaining tasks

Make sure event won't fire more than once on the same request.

Issue fork google_tag-3612054

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

gueguerreiro created an issue. See original summary.

gueguerreiro’s picture

Assigned: gueguerreiro » Unassigned
Status: Active » Needs review
StatusFileSize
new1.44 KB

I opened a merge request with the fix, for review.

I'm also attaching a local patch, if it's easier to use for composer.json like that instead.

japerry made their first commit to this issue’s fork.

japerry’s picture

Status: Needs review » Fixed

Confirmed the bug on 2.0.x. Looking at MR !142, the per-request flag also drops a later search on the same page with different keys (e.g. a header search block plus a product search), and which one gets picked depends on render order. I rebased it onto 2.0.x and pushed a follow up commit:

  • The guard is keyed by the search term, so identical searches are pushed once and distinct searches are each still pushed.
  • Added SearchApiSearchEventTest::testEventOncePerSearchTerm. It gets 4 events on 2.0.x, 1 with the original MR alone, and the expected 2 with the follow up.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.