Jump to content

including Javascript in a PHP function


spires

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.