Like https://drupal.org/node/613648 (http://drupalcode.org/project/google_analytics.git/commitdiff/f976a931a2...) - but for search_api.

The attached patch adds an admin-section if search_api is enabled that allows the user to enable the functionallity and optionally specify which paths should be trigger the reporting.

Upon hitting the search-page a custom-url is generated which contains the search-terms in the form GA expects, and to make custom reporting even easier a ZeroResults event is also reported.

The patch was made against 7.x-1.x and does not apply on 2.x, but as far as I can see it does not clash with any functionallity in 2.x so it should be possible to port it to 2.x as well.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

danquah’s picture

Title: Tracking Zero Result Searches (for search_api) » Site search tracking support for search_api (with Zero Result reporting)
FileSize
4.98 KB
danquah’s picture

Status: Active » Needs review
jaydub’s picture

We have need for this as well. I've taken the patch against 7.1 branch and applied with minor changes to latest dev version of 7.2 branch. Am pushing out on our end to test so will post back in here with results but for now I've attached the patch.

Unfortunately the patch probably won't test successfully in this ticket unless I change the issue version to 7.2 which I didn't want to do per the original poster's patch but can set to 7.2 to test later or maybe a new ticket would be needed.

Status: Needs review » Needs work

The last submitted patch, 3: search_api-result-tracking-2183307-3.patch, failed testing.

hass’s picture

Version: 7.x-1.x-dev » 8.x-2.x-dev
weri’s picture

I could apply patch #3 to 7.2 without problems and it works as expected. I hope this could be added to 7.2 soon.

Agileware’s picture

Updated patch which includes tracking for search API queries, exposed views filters and paths. Required if the website is using Search API module.

arnested’s picture

The patch (all versions of it) is vulnerable to XSS attacks because the search query string is used directly in the output.

I wrapped it in check_plain(), rerolled it against 7.x-2.x, and fixed two minor code style issues in the added code.

Btw. The original patch assumed the search query to be the last element of the path whereas the new patch assumes the search query to be stored in a query parameter. That change actually broke the use case me and @danquah have because the site the patch was written for uses serch pages with an URL structure like /search/my%20search%20term.

It would probably be better to identify the search query string from the SearchApiQuery object. But from Search APIs point of view there is no one search string. There could be several searches on one page (we actually have three) and the search could be build up from facets etc. So I will not change that part now :-)

hass’s picture

Check_plain is incorrect. It is JS abd this require drupal_json_encode(). This version of code looks fully untested. It cannot work as 2.x is universal analytics and I see ga.js code. An code style is not like core, too.

hass’s picture

  1. +++ b/googleanalytics.module
    @@ -184,6 +184,52 @@ function googleanalytics_page_alter(&$page) {
    +          $url_custom = drupal_json_encode(url($search_path,
    +            array(
    +              'query' => array(
    +                'search' => $search_query,
    +              ),
    +            )
    +          ));
    +        }
    

    code style invalid

  2. +++ b/googleanalytics.module
    @@ -184,6 +184,52 @@ function googleanalytics_page_alter(&$page) {
    +          $url_custom = drupal_json_encode(url($search_path,
    +            array(
    +              'query' => array(
    +                'search' => $search_query,
    +                'cat' => 'no-results',
    +              ),
    +            )
    +          ));
    +          // Also generate an event for the result to make it easier to spot.
    

    code style invalid

  3. +++ b/googleanalytics.module
    @@ -184,6 +184,52 @@ function googleanalytics_page_alter(&$page) {
    +          $message_events .= '_gaq.push(["_trackEvent", "ZeroResults", "' . $search_query . '"]);';
    

    ga.js and not analytics.js. Patch has never tested!

joelpittet’s picture

Version: 8.x-2.x-dev » 7.x-2.x-dev
Status: Needs work » Needs review
FileSize
5.92 KB
1.71 KB

Regarding #10.3 I've removed that, if someone wants to implement that later, the docs are here https://developers.google.com/analytics/devguides/collection/analyticsjs...

And I hope I fixed the coding standards and removed the check_plain.

Moving to 7.x-2.x to test, will move back after.

joelpittet’s picture

Version: 7.x-2.x-dev » 8.x-2.x-dev
jelo’s picture

joelpittet’s picture

Status: Needs review » Needs work
+++ b/googleanalytics.module
@@ -178,6 +178,39 @@ function googleanalytics_page_alter(&$page) {
+          $url_options = array('query' => array('search' => $search_query));
...
+          $url_options = array('query' => array('search' => $search_query, 'cat' => 'no-results'));

One thing that caught me a couple of times with this is that "Search result query string" is set to 's' and in google analytics it's set to 's' but it wasn't tracking because we are hardcoding it to 'search' as the query string key.

Probably should leave it as

variable_get('googleanalytics_track_searchapi_query')

?

joelpittet’s picture

AKA


$search_query_key = variable_get('googleanalytics_track_searchapi_query', 'search');
$url_options = array('query' => array($search_query_key => $search_query, 'cat' => 'no-results'));
...
$url_options = array('query' => array($search_query_key => $search_query, 'cat' => 'no-results'));

Thoughts?

kekkis’s picture

Status: Needs work » Needs review
FileSize
5.99 KB
1.32 KB

Rerolled the D7 patch and added @joelpittet's suggested fix for the $search_query_key.

imperator_99’s picture

Hi,

Is there a D8 version of the patch? The version is 8.2x-dev but I can only see what looks to be D7 patches.

Cheers,
Jesse.

brunodbo’s picture

Version: 8.x-2.x-dev » 8.x-3.x-dev
FileSize
9.34 KB

Here's a D8 version of the patch in #16.

rovo’s picture

Since you are able to login to Google Analytics and set the Query Parameters used for your site's search, is this feature really needed in the module? Does enabling this feature via the patch, enhance search query tracking beyond what is done when the correct query parameters are added directly to your Google Analytics View settings?

Greatly appreciate any insight and guidance on this.

Thank you.

japerry’s picture

Version: 8.x-3.x-dev » 4.x-dev
DamienMcKenna’s picture

Status: Needs review » Needs work

The patch needs to be reworked, in particular the changes to google_analytics.module no longer apply.

fengtan’s picture

Re-rolled against 4.x -- one of the services comes from search_api which is not a module dependency and as a result cannot be unconditionnally injected, so for now it is called statically:

class Search implements EventSubscriberInterface {
  public function onCustomPagePath(PagePathEvent $event) {
    ...
    // Detect current search.
    // @todo inject service
    $search_api_results = \Drupal::service('search_api.query_helper')->getAllResults();
    ...
  }
}