Problem/Motivation

When using a view with an ajax-enabled search, the trackSiteSearch event works correctly on the initial page load. However, subsequent ajax search actions on the same page are not tracked. This issue arises because the search tracking code is injected into the html_head, which does not function properly for ajax callbacks.

Steps to reproduce

To reproduce this issue:

  1. Enable ajax functionality for a view containing a search feature.
  2. Visit a page with this view.
  3. Open the browser's network tab.
  4. During the first page load, observe that the trackSiteSearch event is triggered in the network tab.
  5. Enter a search query in the filter and perform an ajax submit.
  6. Notice that no trackSiteSearch event is triggered for the ajax search.

Proposed resolution

To address this problem, it is not feasible to rely on the current html_head approach. One solution is to:

  1. Move the search tracking code to a views element data attribute.
  2. Transfer the tracking search code to a new JavaScript file.
  3. Process the data attribute within the JavaScript code.

This approach will ensure that tracking works seamlessly for both ajax and regular search page loads.

API changes

It's important to note that ViewsPostRenderHookHandler::process() requires some modifications in its method signature and code. Hopefully this method is used only inside of matomo module.

Issue fork matomo-3388363

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

pivica created an issue. See original summary.

pivica’s picture

Status: Active » Needs review
StatusFileSize
new5.52 KB

Here is a first patch.

One important note. The matomo_views_post_render() hook is replaced with matomo_preprocess_views_view() hook because core does not allow injecting attributes for the render element in hook_views_post_render(), it is possible to do it only from hook_views_view().

grimreaper’s picture

Status: Needs review » Needs work

Hi,

Thanks for the issue and the patch.

Please provide the change as a MR.

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

shubham_pareek_19’s picture

Status: Needs work » Needs review

The patch no longer applies cleanly because the code has changed since it was created.
I applied the changes manually on the current branch and created a merge request.