I'm using the Search by Page module instead of the stock search box in core, and GA is not picking up any of the search terms. I can see them in the Drupal DBlog as "search_by_page" events; I assumed because the module still used the core search engine GA would still grab it. Does something have to be tweaked? I have GA set up with the "search" query parameter...

Comments

hass’s picture

Project: Google Analytics » Search by Page
Version: 6.x-3.0 » 6.x-1.x-dev
Component: Code » Main Search by Page module
Category: support » bug

Not sure as I'm not using this module, but I've commented in http://drupal.org/node/488762#comment-1695998 how to solve this problem... This may be of help, too.

jhodgdon’s picture

Category: bug » support
Status: Active » Fixed

http://drupal.org/node/488762#comment-1695998 does indeed look helpful. It suggests setting up paths like search/your_search_path, to make the Google Analytics module recognize it as a search.

You can do that in Search by Page also, when you set up your search environment, just like you can in Faceted Search. Of course, if Google Analytics had a flexible way to say "This URL is a search URL", that might also help...

Sounds like a support request, not a bug in Search by Page. Good luck with your site...

flutedrumr’s picture

Hmm. I have Search by Page overriding the default search path, so "search" should have been sufficient, but no cigar; I'll keep playing around with it and post if I find something that works.

jhodgdon’s picture

Status: Fixed » Active

Hmmm indeed. So when you do a search, you should be seeing paths like search/my_search_term, and Google Analytics should be grabbing that as a search, right?

flutedrumr’s picture

Yes to the paths showing up as search/my_search_term, no to Google Analytics grabbing that...

jhodgdon’s picture

Well, I don't know anything about Google Analytics or the GA Drupal module... you might read the rest of this thread:
http://drupal.org/node/488762#comment-3662964
as it talks about people in a similar situation (with the Faceted Search module), and the settings they used at Google to get things working.

If that still doesn't work, then maybe you can move this issue back to the Google Analytics project, and hass can help you out. Good luck!

hass’s picture

Your path need to be something like "search/search-by-page". This will result in a search result url "search/search-by-page/keyword1+keyword2+keyword3". Than GA is able to grab the keywords and forms an url "search/search-by-page?search=keyword1+keyword2+keyword3". Therefore the query param is "search". Hope this makes it finally clear.

flutedrumr’s picture

So hass, you think the problem might lie with overriding the default search path? i.e., using "search" as the path instead of "search/search_by_page"? Also wouldn't the "query parameter" be the name of the input field in the search form, e.g., "keys" or "search_block_form"?

flutedrumr’s picture

Version: 6.x-1.x-dev » 6.x-1.12
Category: support » bug

Okay, in the Google Analytics Site Search Query Parameter, I've tried the following, none of which work. GA keeps reading the URL as a "normal" page:

search
keys
search_by_page
search-by-page
search/search_by_page
search/search-by-page

I did notice one quirk within Drupal's dblog that seems like it's related. I have the core search form "disabled" for anonymous users, and the Search by Page path set to "search" so visitors always get Search by Page results. When I search from the Search by Page block or the page at /search, the dblog saves them as "search_by_page" events, with the search term as the Message. But if I log in and go to /search/search_by_page, then the dblog saves the queries as "search" events with the Message being "search term (Pages)". The later terms also show up on the Top Search Phrases report. Shouldn't these all be getting logged identically?

jhodgdon’s picture

Category: bug » support

If you go via the URL search/(anything), then the core Search module does the logging. If you go by the URL that you've set up in Search by Page, then Search by Page does the logging.

Setting up the Search by Page module to use a URL that is usually reserved for the core Search module might not work perfectly, and there's not much I can do about that. But I don't think it's a bug in Search by Page if you try to do this and you have problems.

Again, I don't know anything about Google Analytics or the Drupal module, sorry!

hass’s picture

Status: Active » Fixed

Last try:

1. Enable Core "Search" module
2. Enable Google Analytics setting to support Search tracking
3. Configure query param in Google Analytics account (google.com/analytics) with "search". Don't try anything else - it will NOT work!
4. Configure a search module of your choice to show it's search results under the folder "search" (e.g. "example.com/search/[my-search-module]/This+are+my+search+keys")
5. search_get_keys() function will grab the keywords from the URL. See http://api.drupal.org/api/drupal/modules--search--search.module/function... how this core function works.
6. Verify if the GA code shows the search URL in pageTracker("search/[my-search-module]?search=This are my search keys")
7. It just works.

Do NOT TRY anything else. I'm out.

FortEZ’s picture

I have tried to implement the solution mentioned by hass, but it seems that: "Path cannot include a /". If you try to use a path like search/custom_search_by_page in the search environment, nothing works properly. I am trying to figure out a way to get the specific search path but nothing seems to work.

Did anyone manage to make GA and Search by Page work together?

hass’s picture

If you cannot configure the path it is a bug in this module and not in GA

jhodgdon’s picture

Title: using Search by Page with GA » Support Google Analytics in Search by Page
Version: 6.x-1.12 » 6.x-1.x-dev
Category: support » feature
Status: Fixed » Active

I took a look at the Google Analytics on-line help, and found this:
http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=75817

It appears that GA is set up so that it assumes the URLs for search on a web site are always something like this:
example.com/?queryparm=keyword
and you can set up GA to accept multiple query params.

The core Search module and Search by Page do not use this URL format. They use a format like:
example.com/search/keyword
example.com/your-sbp-environment-url/keyword

What I gather from hass in #11 is that the Drupal GA module is able to recognize example.com/search/keyword and pass it to Google in the format Google can accept. But the Drupal GA module does not recognize Search by Page URLs in this way to transform them.

So I think that is the root of the problem here. Correct?

Probably someone could write a small integration module that would do the same thing for Search by Page environments as the Drupal GA module is currently doing for core Search URLs. So I am changing this to a feature request.

If someone who's interested in this wants to try to do it, and posts the module here, I'd be happy to add their add-on module to the Search by Page distribution. Or I might get around to it sometime (no guarantees).

Sorry it's not currently working...

FortEZ’s picture

In GA's code, file: googleanalytics.module, line: 107:

if (module_exists('search') && variable_get('googleanalytics_site_search', FALSE) && arg(0) == 'search' && $keys = search_get_keys())

I wonder what will happen if I change arg(0) == 'search' with:

arg(0) == 'custom_search_by_page'

I'll try that in a development server now...

jhodgdon’s picture

That might work for you, but of course, modifying googleanalytics.module means that you'll have trouble updating when they come out with a new version.

FortEZ’s picture

I'm still trying to figure this out. I'll probably implement a form field that will allow you to enter custom search strings (the ones from the "Search by Page" environments. In any case I will post here for any updates on this issue.

jhodgdon’s picture

Ideally, there could be a module that would query Search by Page and/or Faceted Search for the environment paths, and supply that to Google Analytics.

FortEZ’s picture

I made it work by adding an extra function in google analytics module that handles custom search environments from search by page module. However, I want to make my code a bit smarter:

Ideally, there could be a module that would query Search by Page

Can you give me an idea of how I could query the "Search by Page" module so that I would get the URL paths of the various environments in an array?

jhodgdon’s picture

To get the URLs, you would need to do pretty much what is done in search_by_page_menu() :

  $envs = search_by_page_list_environments();
  foreach ($envs as $envid) {
    $path = search_by_page_setting_get('page_path', $envid, 'search_pages');
    // Do whatever you need to do with the path here....
  }

There should be something similar you can use in Faceted Search as well -- I would check the function faceted_search_menu(), because it will need to do something similar to what's shown here to make its paths work in Drupal.

FortEZ’s picture

Thanks a lot. If anyone really needs a solution to integrate "Search by Page" and "Google Analytics" right now, let me know and I will post the changes that I did and worked without any issues.

I will probably release a "bridge" module within the next couple of weeks.

hass’s picture

Before you release a "bridge" module - for something I do not really understand why you need it - let me know what you are doing there, please. Maybe the GA module can do something for you.

FortEZ’s picture

Basically, what I do is the following:

1) Created a custom function:

function googleanalytics_search_get_custom_keys() {
  static $return;
  if (!isset($return)) {
    // Extract keys as remainder of path
    // Note: support old GET format of searches for existing links.
    $path = explode('/', $_GET['q'], 3);
    $keys = empty($_REQUEST['keys']) ? '' : $_REQUEST['keys'];
    $return = count($path) == 2 ? $path[1] : $keys;
  }
  return $return;
}

2) In the footer hook after the site search support:

    // Custom site search tracking support.
    $url_custom = '';
    if (module_exists('search_by_page') && variable_get('googleanalytics_site_search', FALSE) && $keys = googleanalytics_search_get_custom_keys()) {
      $envs = search_by_page_list_environments();
        foreach ($envs as $envid) {
          $path = search_by_page_setting_get('page_path', $envid, 'search_pages');
          if (arg(0) == $path){ 
            $url_custom = drupal_to_js(url('search/'. arg(0), array('query' => 'search='. $keys)));
          }
        }
    }

If something is not clear, let me know.

flutedrumr’s picture

FortEZ, you added #2 to your theme template?

jhodgdon’s picture

Not sure, but I think FortEZ is saying to add #2 to an existing function googleanalytics_footer() or something close to that.

FortEZ’s picture

Yes, I have added this to the googleanalytics.module file. Assuming that you are using the latest version (3.0) of google_analytics module:

- I have added the function googleanalytics_search_get_custom_keys() just before the text:

/**
 * Implementation of hook_footer() to insert JavaScript at the end of the page.
 */

- and the rest of the code after:

    // Site search tracking support.
    $url_custom = '';
    if (module_exists('search') && variable_get('googleanalytics_site_search', FALSE) && arg(0) == 'search' && $keys = search_get_keys()) {
      $url_custom = drupal_to_js(url('search/'. arg(1), array('query' => 'search='. $keys)));
    }
FortEZ’s picture

StatusFileSize
new13.6 KB

I'm attaching the modified version of the module file that works for me (you can use a compare function of a text editor to check the changes to the original file.

On a sidenote, since I'm not sure whether these changes will be implemented in the Google Analytics module, I will work on the bridge module this weekend so that we can have a more "drupalish" solution to the issue and avoid hacking a module's core.

jhodgdon’s picture

A bridge module is probably the right thing to do. I think it should be added to the Search by Page project (since it isn't useful for most GA users), and will be happy to add it if you get it working.

You should be able to create a function your_module_name_footer() to take care of the footer stuff, and rename your googleanalytics_search_get_custom_keys() to your_module_name_get_custom_keys().

Actually, with the work FortEZ has already done (THANKS!), I have enough to go on. I will make a custom module today and post it here. If you all can test it, that will be most helpful.

FortEZ’s picture

(Thank YOU for the Search by Page module :* )

I would be more than glad to test it!

Just out of curiosity (since I'm not very experienced in Drupal development):

You should be able to create a function your_module_name_footer() to take care of the footer stuff,

The Google Analytics module creates some javascript code that is added at the bottom of the page. This custom code normally (if there is no search) looks like this:

siteSegment, 3]);_gaq.push(["_trackPageview"]);(function()

but with the search (the same jscript code segment) looks like this:

siteSegment, 3]);_gaq.push(["_trackPageview", "/search/global_search?search=test search keywords"]);(function()

The only thing that changes is the $url_custom variable (and this is what the pieces of code that I added do). How would you modify the javascript that GA has already created from an external module? Is there an easy way to do that?

I have done some research over this and only found that a solution similar to the javascript alter module can be implemented. Is this close to what you were thinking to do?

jhodgdon’s picture

Status: Active » Needs review
StatusFileSize
new1.47 KB

Hmmm... I think this will work.

What I did was make sure that this little module has a higher weight than GA (meaning its hook_footer() will be executed afterwards), and do the gaq.push() thing there. Which will hopefully override the previous call to gaq.push(). I'm not sure if it will work, but it is at least worth a try... Can you test?

Just unzip this into your Search by Page module directory and enable the module (after removing the changes to Google Analytics that you put in before, otherwise it won't be much of a test).

FortEZ’s picture

Ok, testing it now.

FortEZ’s picture

A white screen of death cause of a typo in sbp_ga.module file,

change:

function spb_ga_get_custom_keys() {

to:

function sbp_ga_get_custom_keys() {

Other than that, it seems to be working fine!!!

I will take a closer look at the exported Javascript code in order to make sure that I'm not missing anything, but it seems promising!

:)))))

jhodgdon’s picture

StatusFileSize
new1.47 KB

Whoops. Sorry about that. I make that particular typo a lot. :)

Here's a new zip file with the typo corrected, in case anyone else wants to test it.

hass’s picture

Status: Needs review » Needs work

This module will cause double trackings.

I still try to understand why you are not going to integrate with the arg(0) path "search" as this would not require any strange "bridge" modules like this.

jhodgdon’s picture

Why will it cause double trackings? It won't register as a standard search, because it's at the wrong URL.

Sorry if that is a dumb question -- I'm not as familiar with the GA API as you are.

And we don't use the arg(0) path of search because the search module does all kinds of weird stuff with it, and people should be free to set their own search URLs.

hass’s picture

As I know from past 2.x API - if you add "_trackPageview" more than once you have n-times of trackings. Not sure if this may have changed. If you add it 10 times to the same page it is executed 10 times (not verified). On the other side this is a feature as you are able to track in more than one account and fake your stats. Additionally you may break tracking with Ubercart.

For a clean integration we need to get #231451: Add hook to alter data before sending it to browser fixed, but as an interims solution you may go with http://drupal.org/node/284599

jhodgdon’s picture

I see, thanks. So what we need to do is to replace the call to gaq.push(["_trackPageview"]); that GA will add, with the new call, rather than calling it twice. That code on http://drupal.org/node/284599 looks like a reasonable (if ugly) approach to making that kind of a modification.

jhodgdon’s picture

Status: Needs work » Needs review
StatusFileSize
new1.18 KB

OK, here's a new zip file that attempts to do this. Note that there is no .install file needed for this version. I have a bit less confidence that this will work than the last one, but we can hope. :)

FortEZ - if you can test, that would be lovely. Use At Your Own Risk... if you can check your GA logs and see if you're getting double tracking, and take a look at the JS added to the page, that would probably be helpful.

haas - any comments you have would be welcome.

FortEZ’s picture

jhodgdon - I am launching a new development instance in EC2 right now, so that I can do as much testing as I want without breaking anything. I will get back to you with the results in a couple of hours.

jhodgdon’s picture

Just getting back to this... Did you test the latest zip file, and if so, what were the results?

sainzu’s picture

Has anyone fixed this issue? Any update would be of great help.

jhodgdon’s picture

RE #41 - you can test the module in the zip file in #38 and see if it works for you. That is the next step needed. I don't personally use Google Analytics so I cannot test it myself.

jhodgdon’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Issue summary: View changes
Status: Needs review » Active

At this point I am unlikely to add anything to this module for 6.x.

For 7.x, the right fix would be to integrate with the existing Google Analytics contrib module. I'd have to investigate how to do that.