1

I am attempting to add the Google analytics experiments script to my .xhtml . When I do add it I get the following error .

: The content of elements must consist of well-formed character data or markup

I've tried including CDATA tags which remedies the error , but when I run the google analytics validator I get No experiment code found..

I am thinking the reason is because I added the CDATA tags (so the script was modified slightly), does anyone have any incite on any other possible solutions?


 <!-- Google Analytics Content Experiment code -->
    <script>function utmx_section(){}function utmx(){}(function(){var
    k='4238049-1',d=document,l=d.location,c=d.cookie;
    if(l.search.indexOf('utm_expid='+k)>0)return;
    function f(n){if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.
    indexOf(';',i);return escape(c.substring(i+n.length+1,j<0?c.
    length:j))}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash;d.write(
    '<sc'+'ript src="'+'http'+(l.protocol=='https:'?'s://ssl':
    '://www')+'.google-analytics.com/ga_exp.js?'+'utmxkey='+k+
    '&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='+new Date().
    valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
    '" type="text/javascript" charset="utf-8"><\/sc'+'ript>')})();
    </script><script>utmx('url','A/B');</script>
    <!-- End of Google Analytics Content Experiment code -->
Fabii
  • 3,820
  • 14
  • 51
  • 92

1 Answers1

2

If CDATA doesn't work out, because Google is jerking about it, then there are basically 2 other solutions:

  1. Manually escape XML special characters to make the whole JS code syntactically XML valid. Replace < by &lt;, & by &amp;, > by &gt;, etc.

  2. Put JS code in its own JS file and include by <script src>. Not sure if Google Analytics Validator eats that. Edit: thus not.

I'd at least report an issue to Google Analytics guys that their validator is severely broken.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yeah I stuck theJS in its own JS file and included it like so : . Still the same issue though. I will try manually escaping all the XML special characters. – Fabii Dec 06 '13 at 15:43
  • By the way manually escaping the characters worked.. Thank you :) – Fabii Dec 06 '13 at 21:44