News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Analytics code screws up the forum

Started by glennk, December 19, 2008, 07:22:38 PM

Previous topic - Next topic

glennk

When I insert my google analytics code right before the </body> It screws everything up and I get errors. What could be causing the problem ?

function template_html_below()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;

// The following will be used to let the user know that some AJAX process is running
echo '
<div id="ajax_in_progress" style="display: none;', $context['browser']['is_ie'] && !$context['browser']['is_ie7'] ? 'position: absolute;' : '', '">', $txt['ajax_in_progress'], '</div>';

// And then we're done!
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">
try {
var pageTracker = _gat._getTracker("UA-2033956-4");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>';
}

glennk

Says

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in .../Themes/classic/index.template.php on line 275

this is line 275

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

piers38

I done the following as I had a similar problem to yours.
Try inserting the code via the ad management mod
Just insert your "Google analytic s" code into an ad management code block and display on the footer or header of every page.

or use the global headers and footers mod which essentially does the same thing.


hxxp:askfaq.org/ [nonactive]

glennk

Thanks piers 38. That could be a solution I will use but I'm currently having problems with the ad management mod as it don't work on the classic yabb theme which I am using.

Does anyone know a way I can just insert the code into the template without experiencing this problem ?

CkB

I fell foul of this on 1.1 when I implemented the new code, and it's all about the ' (single quote / apostrophe) characters.

At the last block of code you've got in your quote you've got

       echo '
<HTML CODE HERE>

In the google code you've posted in there are also ' quote marks.  What this means is that the first one in the google code that it comes across ends the PHP quote, and therefore it stops treating it as HTML and goes back to PHP, causing you a syntax error.

Before you paste it into the template, I would suggest you paste the google analytics code into Notepad (or other editor), and do a replace of ' with \' (so put a \ on the front of each of ' quotes).

Then paste that in, and you should be fine!  The \ stops the PHP code from ending the HTML stuff (called quoting, depending on your knowledge of writing scripts/etc).

Good luck

C

glennk

Thanks CKB I shall try that. I have very little knowledge of php so I was really stuck.

Ill let you know how I come on.

Briankbc

#6
Hi

This question comes up so often that I wrote a blog post for it.

Actually, it gave me the idea for the blog itself.

The code is give and the solution is discussed at:
http://analytixspecifix.blogspot.com/2008/12/simple-machines-forum-software-and_08.html [nofollow] (Simple Machines Forum Software and Google Analytics)


I have not tested it on 2.x but see no reason why it should not work. Please leave feedback thru blog comments.

Or just go to http://analytixspecifix.blogspot.com [nofollow]
Brian

greyknight17

glennk, is this issue resolved now?

You definitely need to escape all those single quotes in the Google Analytics code with a backslash.

Advertisement: