Tom_Droz

PHP and Google Analytics code

Discussion created by Tom_Droz on Jan 30, 2012
Latest reply on Jan 31, 2012 by Tom_Droz

Hi everyone

 

I am trying to have a field that contains code for Google analytics display properly on a web page using php.

 

Typical Google analytics code looks like:

 

<script type="text/javascript">

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

  1. document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

</script>

<script type="text/javascript">

try {

var pageTracker = _gat._getTracker("UA-13186767-1");

  1. pageTracker._trackPageview();

} catch(err) {}</script>

 

When I included this as an echo fieldvalue i get something like:

 

&lt;script type="text/javascript"&gt;

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

  1. document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

&lt;/script&gt;

&lt;script type="text/javascript"&gt;

try {

var pageTracker = _gat._getTracker("UA-13186767-1");

  1. pageTracker._trackPageview();

} catch(err) {}&lt;/script&gt;

 

I am sure I could code this so this one example works but I am looking for a function that can handle the special characters reliably. Has anyone done this and has a solution?

 

Thanks Tom

 

 

 

Outcomes