Google Analytics

64 views
Skip to first unread message

Charles F. Munat

unread,
Mar 17, 2009, 8:31:59 AM3/17/09
to Lift
Is anyone else using Google Analytics? I am getting this strange
JavaScript error:

operation is not supported" code: "9
var pageTracker = _gat._getTracker("UA-5774043-2");

The Google scripts are supposed to be placed right before the closing
body tag, but Lift inserts its own script in there. I can't imagine how
that would make a difference, but it is the only thing different from
what Google recommends.

Also, the analytics are not working.

Any ideas?

Chas.

Derek Chen-Becker

unread,
Mar 17, 2009, 10:03:09 AM3/17/09
to lif...@googlegroups.com
Can you send the beginning of the XHTML output, up to and including the GA script?

Derek

Charles F. Munat

unread,
Mar 17, 2009, 5:22:40 PM3/17/09
to lif...@googlegroups.com
OK, I have to stop posting this crap when it's 5:30 AM, I've been
working for 18 hours straight, and my brain is fried.

The problem was that I was looking at it on my development machine, not
the production server. The Google Analytics code is tied to the URL. If
the URL is wrong, it doesn't serve the script, so Firebug reports the
error. I are dumb.

Hopefully, though, my posting this will save some other person a hassle
and a little public embarrassment.

Chas.

Xavi Ramirez

unread,
Jul 7, 2009, 4:34:15 PM7/7/09
to lif...@googlegroups.com
Thanks! I ran into this exact problem. Is there any way to get rid
of it? Is there a <lift:production> tag?

Thanks,
Xavi

Xavi Ramirez

unread,
Jul 14, 2009, 10:25:34 PM7/14/09
to lif...@googlegroups.com
Sorry, I didn't replay earlier, but it turns out that the "operation
is not supported code: 9" error is related to the fact that the
default Google Analytics snippet uses document.write(). It turns out
that XHTML does not support document.write(), which causes Firefox to
chock.

Ultimately, I replaced the default Google Analytics snippet with this one:
<script src="http://www.google-analytics.com/ga.js" type="text/javascript" />
<script type="text/javascript">
//<![CDATA[
try { var pageTracker = _gat._getTracker("UA-4384857-1");
pageTracker._trackPageview(); } catch(err) {}
//]]>
</script>

A full write up of my experiences with Google Analytics and Lift can
be found here:
http://www.the-xavi.com/articles/operation-is-not-supported-code-9

~Xavi

Spencer Uresk

unread,
Jul 14, 2009, 11:39:37 PM7/14/09
to Lift
Funny, I was running into similar questions earlier today.

1) The document.write() thing is a common problem. I don't see any
practical benefit to setting the MIME type to xhtml+xml in my apps and
that is what causes document.write() to not work, so I just told lift
to use a plain html mime type. You can do that by adding this to your
Boot class:

LiftRules.useXhtmlMimeType = false

So that is another option for fixing the problem with Google Analytics
(and other things, like Google AdSense)

2) If you only want the analytics code to show up in production, there
isn't a <lift:production /> tag that I'm aware of, but there still is
a pretty easy way to do it. Just make the analytics code a snippet and
use the run mode stuff in Lift to only show the snippet in production.
It would look something like this:

def analytics(in: NodeSeq) : NodeSeq =
net.liftweb.util.Props.productionMode match {
case true => <real analytics code here />
case false => <!–Analytics Code Goes Here–>
}

Then just make sure that your production server is setting the
run.mode property to 'production'. I actually wrote up a short blog
post on this stuff today if you want to know more:

http://www.spenceruresk.com/2009/07/14/using-run-mode-and-properties-in-lift/

- Spencer

On Jul 14, 8:25 pm, Xavi Ramirez <xavi....@gmail.com> wrote:
> Sorry, I didn't replay earlier, but it turns out that the "operation
> is not supported code: 9" error is related to the fact that the
> default Google Analytics snippet uses document.write().  It turns out
> that XHTML does not support document.write(), which causes Firefox to
> chock.
>
> Ultimately, I replaced the default Google Analytics snippet with this one:
> <script src="http://www.google-analytics.com/ga.js" type="text/javascript" />
> <script type="text/javascript">
> //<![CDATA[
>     try { var pageTracker = _gat._getTracker("UA-4384857-1");
> pageTracker._trackPageview(); } catch(err) {}
> //]]>
> </script>
>
> A full write up of my experiences with Google Analytics and Lift can
> be found here:http://www.the-xavi.com/articles/operation-is-not-supported-code-9
>
> ~Xavi
>
> On Tue, Jul 7, 2009 at 4:34 PM, Xavi Ramirez<xavi....@gmail.com> wrote:
> > Thanks!  I ran into this exact problem.  Is there any way to get rid
> > of it?  Is there a <lift:production> tag?
>
> > Thanks,
> > Xavi
>
> > On Tue, Mar 17, 2009 at 5:22 PM, Charles F. Munat<c...@munat.com> wrote:
>
> >> OK, I have to stop posting this crap when it's 5:30 AM, I've been
> >> working for 18 hours straight, and my brain is fried.
>
> >> The problem was that I was looking at it on my development machine, not
> >> the production server. The Google Analytics code is tied to the URL. If
> >> the URL is wrong, it doesn't serve the script, so Firebug reports the
> >> error. I are dumb.
>
> >> Hopefully, though, my posting this will save some other person a hassle
> >> and a little public embarrassment.
>
> >> Chas.
>
> >> Derek Chen-Becker wrote:
> >>> Can you send the beginning of the XHTML output, up to and including the
> >>> GA script?
>
> >>> Derek
>

Richard Dallaway

unread,
Sep 18, 2009, 11:40:40 AM9/18/09
to lif...@googlegroups.com
I'm a bit late to this thread, but found it really useful.

In case this is of any use to future explorers of the thread, I
thought I'd contribute what we ended up doing: we put
<lift:Analytics.google /> (snippet below) before </body> in our
template. I guess this may have been all over-taken by the lift:tail
work.

--
import scala.xml.NodeSeq

import net.liftweb._
import http._
import S._
import util._
import Props.RunModes._

class Analytics {

// To avoid non-XHTML compliant JavaScript we decide if we're https
or not here.
// See e.g., http://happygiraffe.net/blog/2009/06/06/google-analytics-in-xhtml/
def google_script_url = S.request match {
case Full(req) if req.request.scheme == "https" =>
"https://ssl.google-analytics.com/ga.js"
case _ => "http://www.google-analytics.com/ga.js"
}

// Output the Google Analytics JavaScript
def google(xhtml: NodeSeq) = Props.mode match {
case Production | Pilot | Staging =>
<script type="text/javascript" src={google_script_url}></script> ++
<script type="text/javascript">
<![CDATA[try {
var pageTracker = _gat._getTracker('UA-nnnnnnnn-1');
pageTracker._trackPageview();
} catch(err) {}
]]>
</script>
case _ => Nil
}

}
---

...but replacing 'UA-nnnnnnnn-1' with your tracking code.

Warning: I've not tested with a https site yet.

Richard
Reply all
Reply to author
Forward
0 new messages