We would like to use apachesolr with google analytics, however in order to do this, we need a query string in the URL.

Is it possible to turn off clean urls for a single menu tree item. /search/apachesolr_search/ becomes ?q=search/apachesolr_search/

CommentFileSizeAuthor
#4 GA_sitesearch_overview.jpg100.48 KBjanusman

Comments

JacobSingh’s picture

hi drupalnuts:

This is in fact pretty stupid on the part of google analytics...

You can of course add a redirect in your htaccess, or write some code in a custom module to redirect the request. I'm sure it is useful for everyone, but I don't think we should break convention here with standard drupal search to support it by default. Also, I think you are right, it's more of general feature to not use clean urls on a given page than it is an apachesolr issue...

Perhaps do some research / asking around to see if this functionality could be supported generically. If you find anything, please update this ticket, and I can put it in the FAQ.

Best,
Jacob

janusman’s picture

I *think* you can use the Google Analytics API: http://code.google.com/apis/analytics/docs/eventTrackerGuide.html

However, I don't think you can tell GA via the API to put data into the existing 'site search' part; but you can either track the Apache Solr search separately as an event, or as a page load.

For now I think you can use either of these functions in the GA API:

  • _trackEvent(category, action, optional_label, optional_value)
  • _trackPageview(pageurl );

I am going to try both approaches and see which work best.... I came up with this code to add to apachesolr_search.module-- and although no javascript errors are showing, and the data seems to be sent to Google, at least for now the data doesn't show up in my Analytics reports.

I going to trying to "fool" GA with fake pageviews for an URL that DOES have a '?q=' part in it, to see if that makes the data show in site search.

/*
 * Implementation of hook_footer()
 */
function apachesolr_search_footer() {
  if (apachesolr_has_searched()) {
    // Get the query and response. Without these no blocks make sense.
    $response =& apachesolr_static_response_cache();
    if (empty($response)) {
      return;
    }
    $query =& apachesolr_drupal_query(); 
    $q = check_plain($query->get_query());
    $total = $response->numFound;
    $js = "
    try { 
      pageTracker._trackEvent('ApacheSolr', 'Search', '{$q}', $total); 
    } catch(e) { }
    try { 
      pageTracker._trackPageview('/apache_solr_search?query=". rawurlencode($q) ."');
    } catch(e) { }";
    drupal_add_js($js, 'inline', 'footer');
  }
}
robertdouglass’s picture

You could also make the rewrite rule in .htaccess conditional to not apply on /search. I'll leave active until people have concluded research, but I see this issue as a "won't fix" in terms of the project.

janusman’s picture

StatusFileSize
new100.48 KB

I can report that my additional javascript works... details:

  • Events are not reported yet in Google Analytics, discussion threads indicate that they will be released "Soon" by Google. I can only presume that they are being logged.
  • Issuing the additional pageTracker._trackPageview('/apache_solr_search?query=XXXXX'); and telling my GA account to look for the "query" parameter WORKS. See attached screenshot.

So this works, but thinking over it this code might be best as an additional module (apachesolr/contrib/apacehsolr_googleanalytics , anyone?) and where I put it =) Maybe this should require /work with the Google Analytics Drupal module. ... up for grabs.

robertdouglass’s picture

robertdouglass’s picture

Status: Active » Fixed

Marking as fixed. If someone wants to write a dedicated google analytics plugin for ApacheSolr, go ahead, but start a new issue.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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