spires Posted October 22, 2008 Share Posted October 22, 2008 Hi Guys I'm trying to add Google Analytics to my website, but I don't want to add the code to the bottom of over 300 pages. So, I was going to add the javascript to the footer function, which will mean that I only have to add it once for it to appear on all 300 pages. However, if I try to echo out the javascript, it does not work. Can some one tell me where i'm going wrong? My Function code: function footer_code(){ echo ' <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-xxxxxx-x"); pageTracker._initData(); pageTracker._trackPageview(); </script> '; } Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/129531-including-javascript-in-a-php-function/ Share on other sites More sharing options...
ratcateme Posted October 22, 2008 Share Posted October 22, 2008 you needed to escape all the 's echo ' <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-xxxxxx-x"); pageTracker._initData(); pageTracker._trackPageview(); </script> '; Should work Scott. Link to comment https://forums.phpfreaks.com/topic/129531-including-javascript-in-a-php-function/#findComment-671600 Share on other sites More sharing options...
spires Posted October 22, 2008 Author Share Posted October 22, 2008 Thats excellent mate I thought it would be something like that. Thanks for your help Link to comment https://forums.phpfreaks.com/topic/129531-including-javascript-in-a-php-function/#findComment-671602 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.