Google Blogoscoped

Forum

Google Analytics Tracker Code Change and More  (View post)

Julia [PersonRank 0]

Friday, December 14, 2007
12 years ago6,235 views

How come that Blogger has problems with parsing this new code?

Mambo [PersonRank 10]

12 years ago #

What's the point in this more bloated code change, then?

Eugene Villar [PersonRank 5]

12 years ago #

If I understand correctly, the first script element just directs to either the secure or non-secure location for the tracker depending on whether the page itself is secure or not. So if your pages are all non-secure (http instead of https), then can we skip this bit of processing and proceed to the intended result?

cast42 [PersonRank 1]

12 years ago #

I also have a problem entering this into Blogger. I tried to unescape it:
<script type="text/javascript"> var gaJsHost = ((&quot;https:&quot; == document.location.protocol) ? &quot;https: //ssl.&quot; : &quot;http: //www.&quot;); document.write(&quot;<script src=&prime;&quot; + gaJsHost + &quot;google-analytics.com/ga.js&prim; type=&prime;text/javascript&prime></script>&quot;); </script> <script type=&quot;text/javascript&quot;> var pageTracker = _gat._getTracker(&quot;UA-1259299-1&quot;); pageTracker._initData(); pageTracker._trackPageview(); </script>
but the XML parser of blogger still barfs on it.

cast42 [PersonRank 1]

12 years ago #

Implementing the suggestion of Eugene Villar :
<script src='http: //www.google-analytics.com/ga.js' type='text/javascript'>
</script>
<script type='text/javascript'>
var pageTracker = _gat._getTracker(&quot;UA-YOURNUMBER-1&quot;); pageTracker._initData(); pageTracker._trackPageview(); </script>

is accepted by Blogger. It's a workaround for me because my blog (castfortwo.blogspot.com) does not need secure http access via https. But I propose that the Google engineers have a last word about this.

Ionut Alex. Chitu [PersonRank 10]

12 years ago #

Weird. My old Blogger layout accepted Google's code, but I removed the unnecessary https check anyway.

Philipp Lenssen [PersonRank 10]

12 years ago #

> but I removed the unnecessary https check anyway.

I guess that's allowed right? Not like with AdSense, where they disapprove of manual fiddling. But the Analytics API does seem to invite any kind of alteration, considering it's an API...

Marco [PersonRank 0]

12 years ago #

There's an error in the code: in the document.write line, the </script> part will cause the browser to think of it as the end of the script.
In fact it will show
");
on my web page
Just escape *only* that occurence as "</script>"

Marco [PersonRank 0]

12 years ago #

Seems like my comment has been unescaped! ;-)

use a backslash '' before the slash '/' in </script>

David Hammond [PersonRank 1]

12 years ago #

`</` is not allowed within CDATA content (the default type of content in an HTML script element). Regardless of what comes after it or what it's contained within, a proper HTML parser is supposed to treat the `</` as the end of the script content. You have to somehow separate those characters, such as changing it to `<"+"/`.

The issue is even more complex for XHTML. Most browsers actually parse XHTML as HTML in most cases, but a proper XHTML/XML parser (such as the W3C markup validator or any legitimate XML application) will treat the content of the script element as PCDATA instead of CDATA by default. That means markup contained in the script element is actually treated like markup, so comment markers actually comment out the data and tags are actually seen as (invalid) tags. One solution to that is to use a CDATA marked section (`<![CDATA[ ...]]>`). Unfortunately, typical HTML browsers which treat the script element content as CDATA by default will instead see that marked section declaration as script content, so you need to escape it with JavaScript comments like `/*<![CDATA[*/ ... /*]]>*/`. And you still have to break up the `<` and `/` characters for the HTML parsers.

But hey, "A" for effort.

Talkrabb [PersonRank 1]

12 years ago #

Warning!

When inserting the new code in a *. asp Active Server Page it will generate an error:
Active Server Pages error 'ASP 0138'
Nested Script Block

The page won't load at all.

Tony Ruscoe [PersonRank 10]

12 years ago #

Talkrabb, you can simply get around that by doing this:

document.write("\<scr"+"ipt src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'>\<\/scr"+"ipt>");

Note: the extra "+" in the <script> and </script> tags.

See the source code of my website – ruscoe.net – for an example.

Philipp Lenssen [PersonRank 10]

12 years ago #

Google decided to actually *validate* their code (after they released it)... whoopie! They now suggest you replace the tracker code once again. You can find the updated code in your Google Analytics (next to your site click Edit -> Check Status) or in the update to the post:
blogoscoped.com/archive/2007-1 ...

[Via analytics.blogspot.com/2007/12 ...]

Tony Ruscoe [PersonRank 10]

12 years ago #

:rolleyes:

I saw this too. I guess it's fine to leave any modified working versions (like mine) as they achieve the same thing. It doesn't give me much faith in the rest of their code when things like this happen though...

This thread is locked as it's old... but you can create a new thread in the forum. 

Forum home

Advertisement

 
Blog  |  Forum     more >> Archive | Feed | Google's blogs | About
Advertisement

 

This site unofficially covers Google™ and more with some rights reserved. Join our forum!