Skip to content

A fix for ga.jsp for mobile

October 4, 2012

We encountered a problem after using Google Analytics for mobile on our site. We use to get a continuous stream of IllegalStateException for each page load of the mobile site. The problem was because ga.jsp opens a ServletOutputStream to write a small image:

ServletOutputStream output = response.getOutputStream();
output.write(GIF_DATA);

But after that the container will try to get a PrintWriter from the same Response object. Which will end up in an exception, as you can get either ServletOutPutStream (used to write binary objects) or a PrintWriter (for text/html) but you can’t get both.

After a lot of trial and error, and researching on it. Went for a fix in ga.jsp, where in I force it to return. Added the following dummy code in  the end of the ga.jsp.

int i=1;
if(i==1)
return;

NOTE: I can’t do a return straight away, as the ga.jsp gives a compile time error of unreachable code.

This works well! Wonder, is there a way to report this problem to Google Analytics developers.

Advertisements

From → Uncategorized

2 Comments
  1. dhiraj permalink

    actual where do i paste this return statement code

  2. Hi,
    Okay. Here are the last few lines of my ga.jsp code

    // Let exceptions bubble up to container, for better debugging.
    trackPageView(request, response);

    // patch to handle the IllegalStateException thrown
    // if the code is allowed to run till the end
    // as it tries to open PrintStream by calling response.getWriter()
    // — on 4 Oct 2012
    int i=1;
    if(i==1)
    return;

    %>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: