var script2 = document.createElement( 'script' );
script2.value = "var _gaq = _gaq || [];_gaq.push(['_setAccount', 'UA-xxxxxx-1']);_gaq.push(['_setDomainName', 'http://www.widget.devserver2012.com']);_gaq.push(['_setAllowLinker', true]); _gaq.push(['_trackPageview']);(function() {var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})()";
$('head').prepend(script2);
I am using this code to embed Google Analytics in the document head from a javascript widget, users will be embedding on their page. But is Google Analytics is not receiving data.
Use
script2.text = "..."orscript2.innerHTML = "..."instead ofscript2.value = "...".Here’s a jsfiddle that tests using all three assignments with the following code: http://jsfiddle.net/7nsGp/
On both Chrome and Firefox, assigned either
.textor.innerHTML, but not.value.Suggestion: Verify that your script snippet is executing by appending an
alert('script ran')to the end of your script text during testing.