Showing posts with label google analytics. Show all posts
Showing posts with label google analytics. Show all posts

Friday, September 5, 2008

Using Google Analytics to track searches in Drupal

Updated: Added some PHP code to complete the example

Google Analytics needs you to specify part of a query string in order for you to get specialized "Site Search" reports. However in Drupal you might have nice Clean URLs like this:
http://example.com/search/bananas

instead of what GA wants, something like
http://example.com/search?query=bananas


You can still get Site Search reports for searches using Clean URLs, using the Google Analytics API.

You just need to:

  • Use the "new" GA handling code.
  • Add code like this at the end of your page; I recommend putting it inside a Block (be sure to mark the Input Format to be "PHP code"):

    <?php
    if (arg(0) == 'apachesolr_search' || arg(0) == 'search') {
    $keys = search_get_keys();
    ?>
    <script type="text/javascript">
    pageTracker._trackPageview('/site_search?query=<?php echo $keys; ?>');
    </script>
    <?php
    }
    ?>

You then tell your GA report's settings to activate Site Search reporting and to look for "query" as the search argument.

After a day of logging, your GA reports will include the report. See a site searching report sample.